Skip to content

Commit f7cb701

Browse files
committed
expression: add "illegal and/or" for thresholds; drop errstr
This gets rid of the horrible `errstr` function :).
1 parent b3d1b17 commit f7cb701

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/expression/error.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ pub enum ParseThresholdError {
212212
NoChildren,
213213
/// The threshold value appeared to be a sub-expression rather than a number.
214214
KNotTerminal,
215+
/// A 1-of-n threshold was used in a context it was not allowed.
216+
IllegalOr,
217+
/// A n-of-n threshold was used in a context it was not allowed.
218+
IllegalAnd,
215219
/// Failed to parse the threshold value.
216220
ParseK(ParseNumError),
217221
/// Threshold parameters were invalid.
@@ -225,6 +229,12 @@ impl fmt::Display for ParseThresholdError {
225229
match *self {
226230
NoChildren => f.write_str("expected threshold, found terminal"),
227231
KNotTerminal => f.write_str("expected positive integer, found expression"),
232+
IllegalOr => f.write_str(
233+
"1-of-n thresholds not allowed here; please use an 'or' fragment instead",
234+
),
235+
IllegalAnd => f.write_str(
236+
"n-of-n thresholds not allowed here; please use an 'and' fragment instead",
237+
),
228238
ParseK(ref x) => write!(f, "failed to parse threshold value: {}", x),
229239
Threshold(ref e) => e.fmt(f),
230240
}
@@ -237,8 +247,7 @@ impl std::error::Error for ParseThresholdError {
237247
use ParseThresholdError::*;
238248

239249
match *self {
240-
NoChildren => None,
241-
KNotTerminal => None,
250+
NoChildren | KNotTerminal | IllegalOr | IllegalAnd => None,
242251
ParseK(ref e) => Some(e),
243252
Threshold(ref e) => Some(e),
244253
}

src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,6 @@ impl From<crate::policy::compiler::CompilerError> for Error {
628628
fn from(e: crate::policy::compiler::CompilerError) -> Error { Error::CompilerError(e) }
629629
}
630630

631-
fn errstr(s: &str) -> Error { Error::Unexpected(s.to_owned()) }
632-
633631
/// The size of an encoding of a number in Script
634632
pub fn script_num_size(n: usize) -> usize {
635633
match n {

src/policy/semantic.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::iter::{Tree, TreeLike};
1414
use crate::prelude::*;
1515
use crate::sync::Arc;
1616
use crate::{
17-
errstr, expression, AbsLockTime, Error, ForEachKey, FromStrKey, MiniscriptKey, RelLockTime,
18-
Threshold, Translator,
17+
expression, AbsLockTime, Error, ForEachKey, FromStrKey, MiniscriptKey, RelLockTime, Threshold,
18+
Translator,
1919
};
2020

2121
/// Abstract policy which corresponds to the semantics of a miniscript and
@@ -346,10 +346,11 @@ impl<Pk: FromStrKey> expression::FromTree for Policy<Pk> {
346346
let thresh = top.to_null_threshold().map_err(Error::ParseThreshold)?;
347347

348348
// thresh(1) and thresh(n) are disallowed in semantic policies
349-
if thresh.is_or() || thresh.is_and() {
350-
return Err(errstr(
351-
"Semantic Policy thresh cannot have k = 1 or k = n, use `and`/`or` instead",
352-
));
349+
if thresh.is_or() {
350+
return Err(Error::ParseThreshold(crate::ParseThresholdError::IllegalOr));
351+
}
352+
if thresh.is_and() {
353+
return Err(Error::ParseThreshold(crate::ParseThresholdError::IllegalAnd));
353354
}
354355

355356
thresh

0 commit comments

Comments
 (0)