Skip to content

Commit dd19874

Browse files
committed
expression: introduce "unknown name" error variant
This gets rid of several more instances of errstr.
1 parent 4ae5079 commit dd19874

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

src/expression/error.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ pub enum ParseTreeError {
8383
/// Its byte-index into the string.
8484
pos: usize,
8585
},
86+
/// A node's name was not recognized.
87+
UnknownName {
88+
/// The name that was not recognized.
89+
name: String,
90+
},
8691
}
8792

8893
impl From<checksum::Error> for ParseTreeError {
@@ -147,6 +152,7 @@ impl fmt::Display for ParseTreeError {
147152
ParseTreeError::TrailingCharacter { ch, pos } => {
148153
write!(f, "trailing data `{}...` (position {})", ch, pos)
149154
}
155+
ParseTreeError::UnknownName { name } => write!(f, "unrecognized name '{}'", name),
150156
}
151157
}
152158
}
@@ -163,7 +169,8 @@ impl std::error::Error for ParseTreeError {
163169
| ParseTreeError::IllegalCurlyBrace { .. }
164170
| ParseTreeError::IncorrectName { .. }
165171
| ParseTreeError::IncorrectNumberOfChildren { .. }
166-
| ParseTreeError::TrailingCharacter { .. } => None,
172+
| ParseTreeError::TrailingCharacter { .. }
173+
| ParseTreeError::UnknownName { .. } => None,
167174
}
168175
}
169176
}

src/miniscript/astelem.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,9 @@ impl<Pk: FromStrKey, Ctx: ScriptContext> crate::expression::FromTree for Termina
128128
.map_err(Error::Parse)
129129
})
130130
.map(Terminal::MultiA),
131-
_ => Err(Error::Unexpected(format!(
132-
"{}({} args) while parsing Miniscript",
133-
top.name,
134-
top.args.len(),
135-
))),
131+
x => Err(Error::Parse(crate::ParseError::Tree(crate::ParseTreeError::UnknownName {
132+
name: x.to_owned(),
133+
}))),
136134
}?;
137135
let ms = super::wrap_into_miniscript(unwrapped, frag_wrap)?;
138136
Ok(ms.node)

src/miniscript/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,11 +678,11 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
678678
///
679679
/// Returns the fragment name (right of the `:`) and a list of wrappers
680680
/// (left of the `:`).
681-
fn split_expression_name(name: &str) -> Result<(&str, Cow<str>), Error> {
681+
fn split_expression_name(full_name: &str) -> Result<(&str, Cow<str>), Error> {
682682
let mut aliased_wrap;
683683
let frag_name;
684684
let frag_wrap;
685-
let mut name_split = name.split(':');
685+
let mut name_split = full_name.split(':');
686686
match (name_split.next(), name_split.next(), name_split.next()) {
687687
(None, _, _) => {
688688
frag_name = "";
@@ -702,7 +702,9 @@ fn split_expression_name(name: &str) -> Result<(&str, Cow<str>), Error> {
702702
}
703703
(Some(wrap), Some(name), None) => {
704704
if wrap.is_empty() {
705-
return Err(Error::Unexpected(name.to_owned()));
705+
return Err(Error::Parse(crate::ParseError::Tree(
706+
crate::ParseTreeError::UnknownName { name: full_name.to_owned() },
707+
)));
706708
}
707709
if name == "pk" {
708710
frag_name = "pk_k";
@@ -720,7 +722,7 @@ fn split_expression_name(name: &str) -> Result<(&str, Cow<str>), Error> {
720722
}
721723
}
722724
(Some(_), Some(_), Some(_)) => {
723-
return Err(Error::MultiColon(name.to_owned()));
725+
return Err(Error::MultiColon(full_name.to_owned()));
724726
}
725727
}
726728
Ok((frag_name, frag_wrap))

src/policy/concrete.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use {
1919
core::cmp::Reverse,
2020
};
2121

22+
#[cfg(feature = "compiler")]
23+
use crate::errstr;
2224
use crate::expression::{self, FromTree};
2325
use crate::iter::{Tree, TreeLike};
2426
use crate::miniscript::types::extra_props::TimelockInfo;
@@ -27,8 +29,7 @@ use crate::sync::Arc;
2729
#[cfg(all(doc, not(feature = "compiler")))]
2830
use crate::Descriptor;
2931
use crate::{
30-
errstr, AbsLockTime, Error, ForEachKey, FromStrKey, MiniscriptKey, RelLockTime, Threshold,
31-
Translator,
32+
AbsLockTime, Error, ForEachKey, FromStrKey, MiniscriptKey, RelLockTime, Threshold, Translator,
3233
};
3334

3435
/// Maximum TapLeafs allowed in a compiled TapTree
@@ -940,7 +941,9 @@ impl<Pk: FromStrKey> Policy<Pk> {
940941
.map_err(Error::ParseThreshold)?
941942
.translate_by_index(|i| Policy::from_tree(&top.args[1 + i]).map(Arc::new))
942943
.map(Policy::Thresh),
943-
_ => Err(errstr(top.name)),
944+
x => Err(Error::Parse(crate::ParseError::Tree(crate::ParseTreeError::UnknownName {
945+
name: x.to_owned(),
946+
}))),
944947
}
945948
.map(|res| (frag_prob, res))
946949
}

src/policy/semantic.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@ impl<Pk: FromStrKey> expression::FromTree for Policy<Pk> {
356356
.translate_by_index(|i| Policy::from_tree(&top.args[1 + i]).map(Arc::new))
357357
.map(Policy::Thresh)
358358
}
359-
_ => Err(errstr(top.name)),
359+
x => Err(Error::Parse(crate::ParseError::Tree(crate::ParseTreeError::UnknownName {
360+
name: x.to_owned(),
361+
}))),
360362
}
361363
}
362364
}

0 commit comments

Comments
 (0)