core, input, output, dsl: factor away 'flexi' traits

This commit is contained in:
🪞👃🪞 2025-07-29 17:03:48 +03:00
parent 8cbd7dd8e8
commit 9f7d0efda5
11 changed files with 355 additions and 339 deletions

View file

@ -6,48 +6,78 @@
extern crate const_panic;
use const_panic::PanicFmt;
use std::fmt::Debug;
pub(crate) use ::tengri_core::*;
pub(crate) use std::error::Error;
pub(crate) use std::sync::Arc;
pub(crate) use konst::string::{str_range, char_indices};
pub(crate) use thiserror::Error;
pub(crate) use ::tengri_core::*;
pub(crate) use self::DslError::*;
mod dsl_conv; pub use self::dsl_conv::*;
mod dsl_types; pub use self::dsl_types::*;
#[cfg(test)] mod dsl_test;
mod dsl_conv;
pub use self::dsl_conv::*;
pub trait Dsl: Debug + Send + Sync + Sized {
fn src (&self) -> &str;
}
mod dsl_parse;
pub(crate) use self::dsl_parse::*;
pub mod parse { pub use crate::dsl_parse::*; }
impl<'s> Dsl for &'s str {
fn src (&self) -> &str { self }
}
#[cfg(test)]
mod dsl_test;
impl Dsl for String {
fn src (&self) -> &str { self.as_str() }
}
impl Dsl for std::sync::Arc<str> {
flex_trait!(Dsl: Debug + Send + Sync + Sized {
fn src (&self) -> &str {
unreachable!("Dsl::src default impl")
}
});
impl Dsl for Arc<str> {
fn src (&self) -> &str { self.as_ref() }
}
impl<D: Dsl> Dsl for &D {
fn src (&self) -> &str { (*self).src() }
impl<'s> Dsl for &'s str {
fn src (&self) -> &str { self.as_ref() }
}
impl<D: Dsl> Dsl for &mut D {
fn src (&self) -> &str { (**self).src() }
}
impl<D: Dsl> Dsl for std::sync::Arc<D> {
fn src (&self) -> &str { (*self).src() }
}
impl<D: Dsl> Dsl for Option<D> {
fn src (&self) -> &str { if let Some(dsl) = self { dsl.src() } else { "" } }
}
impl<D: Dsl> DslExp for D {}
pub trait DslExp: Dsl {
fn exp (&self) -> DslPerhaps<&str> {
Ok(exp_peek(self.src())?)
}
fn head (&self) -> DslPerhaps<&str> {
Ok(peek(&self.src()[1..])?)
}
fn tail (&self) -> DslPerhaps<&str> {
Ok(if let Some((head_start, head_len)) = seek(&self.src()[1..])? {
peek(&self.src()[(1+head_start+head_len)..])?
} else {
None
})
}
}
impl<D: Dsl> DslSym for D {}
pub trait DslSym: Dsl {
fn sym (&self) -> DslPerhaps<&str> { crate::parse::sym_peek(self.src()) }
}
impl<D: Dsl> DslKey for D {}
pub trait DslKey: Dsl {
fn key (&self) -> DslPerhaps<&str> { crate::parse::key_peek(self.src()) }
}
impl<D: Dsl> DslText for D {}
pub trait DslText: Dsl {
fn text (&self) -> DslPerhaps<&str> { crate::parse::text_peek(self.src()) }
}
impl<D: Dsl> DslNum for D {}
pub trait DslNum: Dsl {
fn num (&self) -> DslPerhaps<&str> { crate::parse::num_peek(self.src()) }
}
/// DSL-specific result type.
pub type DslResult<T> = Result<T, DslError>;