Skip to content

Commit 3a56d9f

Browse files
committed
Change parse_num to accept generic
1 parent 0a46419 commit 3a56d9f

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

src/descriptor/sortedmulti.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
7878
if tree.args.is_empty() {
7979
return Err(errstr("no arguments given for sortedmulti"));
8080
}
81-
let k = expression::parse_num(tree.args[0].name)?;
81+
let k = expression::parse_num::<u32>(tree.args[0].name)?;
8282
if k > (tree.args.len() - 1) as u32 {
8383
return Err(errstr(
8484
"higher threshold than there were keys in sortedmulti",

src/expression.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl<'a> Tree<'a> {
230230
}
231231

232232
/// Parse a string as a u32, for timelocks or thresholds
233-
pub fn parse_num(s: &str) -> Result<u32, Error> {
233+
pub fn parse_num<T: FromStr>(s: &str) -> Result<T, Error> {
234234
if s.len() > 1 {
235235
let ch = s.chars().next().unwrap();
236236
if !('1'..='9').contains(&ch) {
@@ -239,7 +239,7 @@ pub fn parse_num(s: &str) -> Result<u32, Error> {
239239
));
240240
}
241241
}
242-
u32::from_str(s).map_err(|_| errstr(s))
242+
T::from_str(s).map_err(|_| errstr(s))
243243
}
244244

245245
/// Attempts to parse a terminal expression
@@ -292,11 +292,11 @@ mod tests {
292292

293293
#[test]
294294
fn test_parse_num() {
295-
assert!(parse_num("0").is_ok());
296-
assert!(parse_num("00").is_err());
297-
assert!(parse_num("0000").is_err());
298-
assert!(parse_num("06").is_err());
299-
assert!(parse_num("+6").is_err());
300-
assert!(parse_num("-6").is_err());
295+
assert!(parse_num::<u32>("0").is_ok());
296+
assert!(parse_num::<u32>("00").is_err());
297+
assert!(parse_num::<u32>("0000").is_err());
298+
assert!(parse_num::<u32>("06").is_err());
299+
assert!(parse_num::<u32>("+6").is_err());
300+
assert!(parse_num::<u32>("-6").is_err());
301301
}
302302
}

src/miniscript/astelem.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,10 +589,10 @@ impl_from_tree!(
589589
expression::terminal(&top.args[0], |x| Pk::Hash::from_str(x).map(Terminal::PkH))
590590
}
591591
("after", 1) => expression::terminal(&top.args[0], |x| {
592-
expression::parse_num(x).map(Terminal::After)
592+
expression::parse_num::<u32>(x).map(Terminal::After)
593593
}),
594594
("older", 1) => expression::terminal(&top.args[0], |x| {
595-
expression::parse_num(x).map(Terminal::Older)
595+
expression::parse_num::<u32>(x).map(Terminal::Older)
596596
}),
597597
("sha256", 1) => expression::terminal(&top.args[0], |x| {
598598
Pk::Sha256::from_str(x).map(Terminal::Sha256)
@@ -634,7 +634,7 @@ impl_from_tree!(
634634
if n == 0 {
635635
return Err(errstr("no arguments given"));
636636
}
637-
let k = expression::terminal(&top.args[0], expression::parse_num)? as usize;
637+
let k = expression::terminal(&top.args[0], expression::parse_num::<u32>)? as usize;
638638
if k > n - 1 {
639639
return Err(errstr("higher threshold than there are subexpressions"));
640640
}
@@ -653,7 +653,7 @@ impl_from_tree!(
653653
if n == 0 {
654654
return Err(errstr("no arguments given"));
655655
}
656-
let k = expression::terminal(&top.args[0], expression::parse_num)? as usize;
656+
let k = expression::terminal(&top.args[0], expression::parse_num::<u32>)? as usize;
657657
if k > n - 1 {
658658
return Err(errstr("higher threshold than there were keys in multi"));
659659
}

src/policy/concrete.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ impl_block_str!(
754754
if !allow_prob {
755755
return Err(Error::AtOutsideOr(top.name.to_owned()));
756756
}
757-
frag_prob = expression::parse_num(prob)? as usize;
757+
frag_prob = expression::parse_num::<u32>(prob)? as usize;
758758
frag_name = name;
759759
}
760760
(Some(_), Some(_), Some(_)) => {
@@ -820,7 +820,7 @@ impl_block_str!(
820820
return Err(Error::PolicyError(PolicyError::IncorrectThresh));
821821
}
822822

823-
let thresh = expression::parse_num(top.args[0].name)?;
823+
let thresh = expression::parse_num::<u32>(top.args[0].name)?;
824824
if thresh >= nsubs || thresh == 0 {
825825
return Err(Error::PolicyError(PolicyError::IncorrectThresh));
826826
}

src/policy/semantic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,10 @@ impl_from_tree!(
333333
Pk::Hash::from_str(pk).map(Policy::KeyHash)
334334
}),
335335
("after", 1) => expression::terminal(&top.args[0], |x| {
336-
expression::parse_num(x).map(Policy::After)
336+
expression::parse_num::<u32>(x).map(Policy::After)
337337
}),
338338
("older", 1) => expression::terminal(&top.args[0], |x| {
339-
expression::parse_num(x).map(Policy::Older)
339+
expression::parse_num::<u32>(x).map(Policy::Older)
340340
}),
341341
("sha256", 1) => expression::terminal(&top.args[0], |x| {
342342
Pk::Sha256::from_str(x).map(Policy::Sha256)
@@ -379,7 +379,7 @@ impl_from_tree!(
379379
return Err(errstr(top.args[0].args[0].name));
380380
}
381381

382-
let thresh = expression::parse_num(top.args[0].name)?;
382+
let thresh = expression::parse_num::<u32>(top.args[0].name)?;
383383

384384
// thresh(1) and thresh(n) are disallowed in semantic policies
385385
if thresh <= 1 || thresh >= (nsubs as u32 - 1) {

0 commit comments

Comments
 (0)