-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expression: add method to validate and construct a threshold
- Loading branch information
Showing
4 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// SPDX-License-Identifier: CC0-1.0 | ||
|
||
//! Expression-related errors | ||
|
||
use core::fmt; | ||
|
||
use crate::ThresholdError; | ||
|
||
/// Error parsing a threshold expression. | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub enum ParseThresholdError { | ||
/// Expression had no children, not even a threshold value. | ||
NoChildren, | ||
/// The threshold value appeared to be a sub-expression rather than a number. | ||
KNotTerminal, | ||
/// Failed to parse the threshold value. | ||
// FIXME this should be a more specific type. Will be handled in a later PR | ||
// that rewrites the expression parsing logic. | ||
ParseK(String), | ||
/// Threshold parameters were invalid. | ||
Threshold(ThresholdError), | ||
} | ||
|
||
impl fmt::Display for ParseThresholdError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
use ParseThresholdError::*; | ||
|
||
match *self { | ||
NoChildren => f.write_str("expected threshold, found terminal"), | ||
KNotTerminal => f.write_str("expected positive integer, found expression"), | ||
ParseK(ref x) => write!(f, "failed to parse threshold value {}", x), | ||
Threshold(ref e) => e.fmt(f), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl std::error::Error for ParseThresholdError { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
use ParseThresholdError::*; | ||
|
||
match *self { | ||
NoChildren => None, | ||
KNotTerminal => None, | ||
ParseK(..) => None, | ||
Threshold(ref e) => Some(e), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters