From ed772b9872572c39dc4afef61729a2ee068ec3a0 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sat, 10 May 2025 15:49:33 +0300 Subject: [PATCH] dsl: extract dsl_error; ParseError -> DslError --- dsl/src/dsl_error.rs | 16 ++++++++++++++++ dsl/src/dsl_parse.rs | 23 ++++------------------- dsl/src/dsl_provide.rs | 11 +++++++++++ dsl/src/lib.rs | 5 +++-- 4 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 dsl/src/dsl_error.rs diff --git a/dsl/src/dsl_error.rs b/dsl/src/dsl_error.rs new file mode 100644 index 0000000..ffb53fd --- /dev/null +++ b/dsl/src/dsl_error.rs @@ -0,0 +1,16 @@ +use crate::*; + +pub type DslResult = Result; + +#[derive(Error, Debug, Copy, Clone, PartialEq)] pub enum DslError { + #[error("parse failed: not implemented")] + Unimplemented, + #[error("parse failed: empty")] + Empty, + #[error("parse failed: incomplete")] + Incomplete, + #[error("parse failed: unexpected character '{0}'")] + Unexpected(char), + #[error("parse failed: error #{0}")] + Code(u8), +} diff --git a/dsl/src/dsl_parse.rs b/dsl/src/dsl_parse.rs index 3923cd4..ee06792 100644 --- a/dsl/src/dsl_parse.rs +++ b/dsl/src/dsl_parse.rs @@ -1,20 +1,5 @@ use crate::*; -pub type ParseResult = Result; - -#[derive(Error, Debug, Copy, Clone, PartialEq)] pub enum ParseError { - #[error("parse failed: not implemented")] - Unimplemented, - #[error("parse failed: empty")] - Empty, - #[error("parse failed: incomplete")] - Incomplete, - #[error("parse failed: unexpected character '{0}'")] - Unexpected(char), - #[error("parse failed: error #{0}")] - Code(u8), -} - /// Implement the const iterator pattern. #[macro_export] macro_rules! const_iter { ($(<$l:lifetime>)?|$self:ident: $Struct:ty| => $Item:ty => $expr:expr) => { @@ -170,7 +155,7 @@ pub const fn peek_src <'a> (source: &'a str) -> Option> { } } -pub const fn to_number (digits: &str) -> Result { +pub const fn to_number (digits: &str) -> DslResult { let mut value = 0; iterate!(char_indices(digits) => (_, c) => match to_digit(c) { Ok(digit) => value = 10 * value + digit, @@ -179,7 +164,7 @@ pub const fn to_number (digits: &str) -> Result { Ok(value) } -pub const fn to_digit (c: char) -> Result { +pub const fn to_digit (c: char) -> DslResult { Ok(match c { '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, @@ -196,7 +181,7 @@ pub const fn to_digit (c: char) -> Result { #[derive(Debug, Copy, Clone, Default, PartialEq)] pub enum Value<'source> { #[default] Nil, - Err(ParseError), + Err(DslError), Num(usize), Sym(&'source str), Key(&'source str), @@ -228,7 +213,7 @@ impl<'source> Token<'source> { pub const fn value (&self) -> Value { self.value } - pub const fn error (self, error: ParseError) -> Self { + pub const fn error (self, error: DslError) -> Self { Self { value: Value::Err(error), ..self } } pub const fn grow (self) -> Self { diff --git a/dsl/src/dsl_provide.rs b/dsl/src/dsl_provide.rs index 209b37c..c22e5d6 100644 --- a/dsl/src/dsl_provide.rs +++ b/dsl/src/dsl_provide.rs @@ -1,10 +1,21 @@ use crate::*; /// Map EDN tokens to parameters of a given type for a given context +/// TODO: Replace both [Context] and [TryFromDsl] with this trait +/// which returns a [Result]. pub trait Dsl: Sized { fn take <'state, 'source> (_: &'state Self, _: &mut TokenIter<'source>) -> Perhaps { Ok(None) } + fn take_or_fail <'state, 'source> ( + state: &'state Self, iter: &mut TokenIter<'source> + ) -> Usually { + if let Some(value) = Self::take(state, iter)? { + Ok(value) + } else { + Result::Err("not found".into()) // TODO add info and error type + } + } } /// Map EDN tokens to parameters of a given type for a given context diff --git a/dsl/src/lib.rs b/dsl/src/lib.rs index c72ff23..9c9b44e 100644 --- a/dsl/src/lib.rs +++ b/dsl/src/lib.rs @@ -43,8 +43,9 @@ pub(crate) use konst::iter::{ConstIntoIter, IsIteratorKind}; pub(crate) use konst::string::{split_at, str_range, char_indices}; pub(crate) use thiserror::Error; pub(crate) use self::Value::*; -pub(crate) use self::ParseError::*; +pub(crate) use self::DslError::*; +mod dsl_error; pub use self::dsl_error::*; mod dsl_parse; pub use self::dsl_parse::*; mod dsl_provide; pub use self::dsl_provide::*; @@ -148,7 +149,7 @@ mod dsl_provide; pub use self::dsl_provide::*; Ok(()) } -//#[cfg(test)] #[test] fn test_examples () -> Result<(), ParseError> { +//#[cfg(test)] #[test] fn test_examples () -> Result<(), DslError> { //// Let's pretend to render some view. //let source = include_str!("../../tek/src/view_arranger.edn"); //// The token iterator allows you to get the tokens represented by the source text.