miniscript/expression/
error.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! Expression-related errors
4
5use core::fmt;
6
7use crate::prelude::*;
8use crate::ThresholdError;
9
10/// Error parsing a threshold expression.
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub enum ParseThresholdError {
13    /// Expression had no children, not even a threshold value.
14    NoChildren,
15    /// The threshold value appeared to be a sub-expression rather than a number.
16    KNotTerminal,
17    /// Failed to parse the threshold value.
18    // FIXME this should be a more specific type. Will be handled in a later PR
19    // that rewrites the expression parsing logic.
20    ParseK(String),
21    /// Threshold parameters were invalid.
22    Threshold(ThresholdError),
23}
24
25impl fmt::Display for ParseThresholdError {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        use ParseThresholdError::*;
28
29        match *self {
30            NoChildren => f.write_str("expected threshold, found terminal"),
31            KNotTerminal => f.write_str("expected positive integer, found expression"),
32            ParseK(ref x) => write!(f, "failed to parse threshold value {}", x),
33            Threshold(ref e) => e.fmt(f),
34        }
35    }
36}
37
38#[cfg(feature = "std")]
39impl std::error::Error for ParseThresholdError {
40    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
41        use ParseThresholdError::*;
42
43        match *self {
44            NoChildren => None,
45            KNotTerminal => None,
46            ParseK(..) => None,
47            Threshold(ref e) => Some(e),
48        }
49    }
50}