-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
--block
option for call
command
- Loading branch information
1 parent
89690d4
commit 61650d9
Showing
8 changed files
with
90 additions
and
46 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use clap::{builder::TypedValueParser, error::ErrorKind, Arg, Command, Error}; | ||
use regex::Regex; | ||
use starknet::core::types::{BlockId, BlockTag, Felt}; | ||
|
||
#[derive(Clone)] | ||
pub struct BlockIdParser; | ||
|
||
impl TypedValueParser for BlockIdParser { | ||
type Value = BlockId; | ||
|
||
fn parse_ref( | ||
&self, | ||
cmd: &Command, | ||
_arg: Option<&Arg>, | ||
value: &std::ffi::OsStr, | ||
) -> Result<Self::Value, Error> { | ||
if value.is_empty() { | ||
Err(cmd.clone().error(ErrorKind::InvalidValue, "empty block ID")) | ||
} else { | ||
match value.to_str() { | ||
Some(value) => { | ||
let regex_block_number = Regex::new("^[0-9]{1,}$").unwrap(); | ||
|
||
if value == "latest" { | ||
Ok(BlockId::Tag(BlockTag::Latest)) | ||
} else if value == "pending" { | ||
Ok(BlockId::Tag(BlockTag::Pending)) | ||
} else if regex_block_number.is_match(value) { | ||
Ok(BlockId::Number(value.parse::<u64>().map_err(|err| { | ||
cmd.clone().error( | ||
ErrorKind::InvalidValue, | ||
&format!("invalid block number: {}", err), | ||
) | ||
})?)) | ||
} else { | ||
Ok(BlockId::Hash(Felt::from_hex(value).map_err(|err| { | ||
cmd.clone().error( | ||
ErrorKind::InvalidValue, | ||
&format!("invalid block hash: {}", err), | ||
) | ||
})?)) | ||
} | ||
} | ||
None => Err(cmd | ||
.clone() | ||
.error(ErrorKind::InvalidValue, "invalid block ID")), | ||
} | ||
} | ||
} | ||
} |
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
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
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