forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from whitenois3/feat/builtins
Start builtin command module
- Loading branch information
Showing
5 changed files
with
106 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,59 @@ | ||
// TODO | ||
use crate::env::ChiselEnv; | ||
use ansi_term::Color::{Cyan, Green, Red}; | ||
use std::{error, str::FromStr}; | ||
use strum::{EnumIter, IntoEnumIterator}; | ||
|
||
/// Custom Chisel commands | ||
#[derive(Debug, EnumIter)] | ||
pub enum ChiselCommand { | ||
Help, | ||
Source, | ||
} | ||
|
||
/// A command descriptor type | ||
type CmdDescriptor = (&'static str, &'static str); | ||
|
||
/// Custom Chisel command implementations | ||
#[allow(dead_code, unused)] | ||
impl ChiselCommand { | ||
pub fn dispatch(&self, args: &[&str], env: &ChiselEnv) { | ||
match self { | ||
ChiselCommand::Help => { | ||
println!("{}", Cyan.paint("⚒️ Chisel help")); | ||
ChiselCommand::iter().for_each(|cmd| { | ||
let descriptor = CmdDescriptor::from(cmd); | ||
println!("!{} - {}", Green.paint(descriptor.0), descriptor.1); | ||
}); | ||
} | ||
ChiselCommand::Source => println!("{}", env.contract_source()), | ||
} | ||
} | ||
} | ||
|
||
/// Attempt to convert a string slice to a `ChiselCommand` | ||
impl FromStr for ChiselCommand { | ||
type Err = Box<dyn error::Error>; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s.to_lowercase().as_ref() { | ||
"help" => Ok(ChiselCommand::Help), | ||
"source" => Ok(ChiselCommand::Source), | ||
_ => Err(Red | ||
.paint(format!("Unknown command \"{}\"! See available commands with `!help`.", s)) | ||
.to_string() | ||
.into()), | ||
} | ||
} | ||
} | ||
|
||
/// Convert a `ChiselCommand` into a `CmdDescriptor` tuple | ||
impl From<ChiselCommand> for CmdDescriptor { | ||
fn from(cmd: ChiselCommand) -> Self { | ||
match cmd { | ||
ChiselCommand::Help => ("help", "Display all commands"), | ||
ChiselCommand::Source => { | ||
("source", "Display the source code of the current REPL session") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters