mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
This commit is contained in:
parent
73eb935282
commit
360b404b69
3 changed files with 82 additions and 65 deletions
|
|
@ -7,7 +7,6 @@ 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 self::DslError::*;
|
||||
|
|
@ -24,6 +23,10 @@ impl<'s> Dsl for &'s str {
|
|||
fn src (&self) -> &str { self }
|
||||
}
|
||||
|
||||
impl Dsl for String {
|
||||
fn src (&self) -> &str { self.as_str() }
|
||||
}
|
||||
|
||||
impl Dsl for std::sync::Arc<str> {
|
||||
fn src (&self) -> &str { self.as_ref() }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,12 +11,6 @@ pub trait FromDsl<T>: Sized {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'s, T: FromDsl<U>, U> DslInto<'s, T> for U {
|
||||
fn dsl_into (&self, dsl: &impl Dsl) -> Perhaps<T> {
|
||||
T::from_dsl(self, dsl)
|
||||
}
|
||||
}
|
||||
|
||||
/// `self` + [Dsl] -> `T`
|
||||
pub trait DslInto<'s, T> {
|
||||
fn dsl_into (&'s self, dsl: &impl Dsl) -> Perhaps<T>;
|
||||
|
|
@ -28,6 +22,17 @@ pub trait DslInto<'s, T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// `self` + `T` -> [Dsl]
|
||||
pub trait DslFrom<T> {
|
||||
fn dsl_from (&self, dsl: &T) -> Perhaps<impl Dsl>;
|
||||
fn dsl_from_or (&self, dsl: &T, err: Box<dyn Error>) -> Usually<impl Dsl> {
|
||||
self.dsl_from(dsl)?.ok_or(err)
|
||||
}
|
||||
fn dsl_from_or_else (&self, dsl: &T, err: impl Fn()->Box<dyn Error>) -> Usually<impl Dsl> {
|
||||
self.dsl_from(dsl)?.ok_or_else(err)
|
||||
}
|
||||
}
|
||||
|
||||
/// `self` + `T` + -> [Dsl]
|
||||
pub trait IntoDsl<T> {
|
||||
fn into_dsl (&self, state: &T) -> Perhaps<impl Dsl>;
|
||||
|
|
@ -39,13 +44,14 @@ pub trait IntoDsl<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// `self` + `T` -> [Dsl]
|
||||
pub trait DslFrom<T> {
|
||||
fn dsl_from (&self, dsl: &impl Dsl) -> Perhaps<impl Dsl>;
|
||||
fn dsl_from_or (&self, dsl: &impl Dsl, err: Box<dyn Error>) -> Usually<impl Dsl> {
|
||||
self.dsl_from(dsl)?.ok_or(err)
|
||||
}
|
||||
fn dsl_from_or_else (&self, dsl: &impl Dsl, err: impl Fn()->Box<dyn Error>) -> Usually<impl Dsl> {
|
||||
self.dsl_from(dsl)?.ok_or_else(err)
|
||||
impl<'s, T: FromDsl<U>, U> DslInto<'s, T> for U {
|
||||
fn dsl_into (&self, dsl: &impl Dsl) -> Perhaps<T> {
|
||||
T::from_dsl(self, dsl)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: DslFrom<U>, U> IntoDsl<T> for U {
|
||||
fn into_dsl (&self, state: &T) -> Perhaps<impl Dsl> {
|
||||
T::dsl_from(state, self)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue