aboutsummaryrefslogtreecommitdiffstats
path: root/src/state.rs
diff options
context:
space:
mode:
authorLibravatar Titus Wormer <tituswormer@gmail.com>2022-09-07 15:53:06 +0200
committerLibravatar Titus Wormer <tituswormer@gmail.com>2022-09-07 15:53:06 +0200
commit1d92666865b35341e076efbefddf6e73b5e1542e (patch)
tree11c05985ec7679f73473e7ea2c769465698e2f08 /src/state.rs
parente6018e52ee6ad9a8f8a0672b75bf515faf74af1f (diff)
downloadmarkdown-rs-1d92666865b35341e076efbefddf6e73b5e1542e.tar.gz
markdown-rs-1d92666865b35341e076efbefddf6e73b5e1542e.tar.bz2
markdown-rs-1d92666865b35341e076efbefddf6e73b5e1542e.zip
Add support for recoverable syntax errors
Diffstat (limited to '')
-rw-r--r--src/state.rs25
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)]