|
@@ -259,7 +259,7 @@ impl Inputs {
|
|
|
impl TxidGenerator {
|
|
|
fn deduce(matches: &getopts::Matches) -> Result<Self, OptionsError> {
|
|
|
if let Some(starting_txid) = matches.opt_str("txid") {
|
|
|
- if let Ok(start) = starting_txid.parse() {
|
|
|
+ if let Some(start) = parse_dec_or_hex(&starting_txid) {
|
|
|
Ok(Self::Sequence(start))
|
|
|
}
|
|
|
else {
|
|
@@ -272,6 +272,31 @@ impl TxidGenerator {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+fn parse_dec_or_hex(input: &str) -> Option<u16> {
|
|
|
+ if input.starts_with("0x") {
|
|
|
+ match u16::from_str_radix(&input[2..], 16) {
|
|
|
+ Ok(num) => {
|
|
|
+ Some(num)
|
|
|
+ }
|
|
|
+ Err(e) => {
|
|
|
+ warn!("Error parsing hex number: {}", e);
|
|
|
+ None
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ match input.parse() {
|
|
|
+ Ok(num) => {
|
|
|
+ Some(num)
|
|
|
+ }
|
|
|
+ Err(e) => {
|
|
|
+ warn!("Error parsing number: {}", e);
|
|
|
+ None
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
impl OutputFormat {
|
|
|
fn deduce(matches: &getopts::Matches) -> Self {
|
|
@@ -642,8 +667,8 @@ mod test {
|
|
|
|
|
|
#[test]
|
|
|
fn invalid_txid() {
|
|
|
- assert_eq!(Options::getopts(&[ "lookup.dog", "--txid=0x1234" ]),
|
|
|
- OptionsResult::InvalidOptions(OptionsError::InvalidTxid("0x1234".into())));
|
|
|
+ assert_eq!(Options::getopts(&[ "lookup.dog", "--txid=0x10000" ]),
|
|
|
+ OptionsResult::InvalidOptions(OptionsError::InvalidTxid("0x10000".into())));
|
|
|
}
|
|
|
|
|
|
#[test]
|
|
@@ -651,4 +676,19 @@ mod test {
|
|
|
assert_eq!(Options::getopts(&[ "OPT", "lookup.dog" ]),
|
|
|
OptionsResult::InvalidOptions(OptionsError::QueryTypeOPT));
|
|
|
}
|
|
|
+
|
|
|
+ // txid tests
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn number_parsing() {
|
|
|
+ assert_eq!(parse_dec_or_hex("1234"), Some(1234));
|
|
|
+ assert_eq!(parse_dec_or_hex("0x1234"), Some(0x1234));
|
|
|
+ assert_eq!(parse_dec_or_hex("0xABcd"), Some(0xABcd));
|
|
|
+
|
|
|
+ assert_eq!(parse_dec_or_hex("65536"), None);
|
|
|
+ assert_eq!(parse_dec_or_hex("0x65536"), None);
|
|
|
+
|
|
|
+ assert_eq!(parse_dec_or_hex(""), None);
|
|
|
+ assert_eq!(parse_dec_or_hex("0x"), None);
|
|
|
+ }
|
|
|
}
|