diff options
Diffstat (limited to 'src/state.rs')
-rw-r--r-- | src/state.rs | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/state.rs b/src/state.rs index 3294a2f..e8bd17a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -2,10 +2,15 @@ use crate::construct; use crate::tokenizer::Tokenizer; +use alloc::string::{String, ToString}; /// Result of a state. -#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq)] pub enum State { + /// Syntax error. + /// + /// Only used by MDX. + Error(String), /// Move to [`Name`][] next. Next(Name), /// Retry in [`Name`][]. @@ -16,6 +21,24 @@ pub enum State { Nok, } +impl State { + /// Turn a final state into a result. + /// + /// This doesn’t work on future states ([`State::Next`], [`State::Retry`]), + /// or on an attempt ([`State::Nok`]). + /// + /// But it turns the final result into an error if crashed. + pub fn to_result(&self) -> Result<(), String> { + match self { + State::Nok | State::Next(_) | State::Retry(_) => { + unreachable!("cannot turn intermediate state into result") + } + State::Ok => Ok(()), + State::Error(x) => Err(x.to_string()), + } + } +} + /// Names of states to move to. #[derive(Clone, Copy, Debug, Eq, PartialEq)] #[allow(clippy::enum_variant_names)] |