mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
this trait will NOT have a lifetime
This commit is contained in:
parent
38f69ddd50
commit
5e7b867aba
3 changed files with 67 additions and 64 deletions
109
edn/src/token.rs
109
edn/src/token.rs
|
|
@ -124,7 +124,7 @@ impl<'a> Token<'a> {
|
|||
d => Ok(Self { length: self.length + 1, depth: d - 1, ..self })
|
||||
}
|
||||
}
|
||||
pub const fn to_ref_atom (&'a self) -> Result<RefAtom<'a>, ParseError> {
|
||||
pub const fn to_ref_atom (&'a self) -> Result<RefAtom, ParseError> {
|
||||
Ok(match self.kind {
|
||||
Nil => return Err(ParseError::Empty),
|
||||
Num => match to_number(self.slice()) {
|
||||
|
|
@ -152,10 +152,9 @@ const fn to_digit (c: char) -> Result<usize, ParseError> {
|
|||
_ => return Err(Unexpected(c))
|
||||
})
|
||||
}
|
||||
pub trait Atom<'a>: Sized {
|
||||
pub trait Atom: Sized {
|
||||
fn kind (&self) -> TokenKind;
|
||||
fn text (&self) -> &str;
|
||||
fn read_all (source: &'a str) -> impl Iterator<Item = Result<Self, ParseError>>;
|
||||
}
|
||||
#[derive(Clone, PartialEq)] pub enum RefAtom<'a> {
|
||||
Num(usize),
|
||||
|
|
@ -191,25 +190,10 @@ impl<'a> RefAtom<'a> {
|
|||
pub fn to_arc_atom (&self) -> ArcAtom {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
impl<'a> Atom<'a> for RefAtom<'a> {
|
||||
fn kind (&self) -> TokenKind {
|
||||
match self {
|
||||
Self::Num(_) => TokenKind::Num,
|
||||
Self::Sym(_) => TokenKind::Sym,
|
||||
Self::Key(_) => TokenKind::Key,
|
||||
Self::Exp(_) => TokenKind::Exp,
|
||||
}
|
||||
}
|
||||
fn text (&self) -> &str {
|
||||
match self {
|
||||
Self::Num(_)|Self::Exp(_) => "",
|
||||
Self::Sym(s) => s,
|
||||
Self::Key(k) => k,
|
||||
}
|
||||
}
|
||||
fn read_all (source: &'a str) -> impl Iterator<Item = Result<Self, ParseError>> {
|
||||
TokenIterator::new(source).map(move |result: TokenResult<'a>|match result{
|
||||
pub fn read_all <'b: 'a> (source: &'b str)
|
||||
-> impl Iterator<Item = Result<RefAtom<'b>, ParseError>> + 'b
|
||||
{
|
||||
TokenIterator::new(source).map(move |result|match result{
|
||||
Err(e) => Err(e),
|
||||
Ok(token) => match token.kind {
|
||||
Nil => Err(ParseError::Empty),
|
||||
|
|
@ -226,6 +210,23 @@ impl<'a> Atom<'a> for RefAtom<'a> {
|
|||
})
|
||||
}
|
||||
}
|
||||
impl<'a> Atom for RefAtom<'a> {
|
||||
fn kind (&self) -> TokenKind {
|
||||
match self {
|
||||
Self::Num(_) => TokenKind::Num,
|
||||
Self::Sym(_) => TokenKind::Sym,
|
||||
Self::Key(_) => TokenKind::Key,
|
||||
Self::Exp(_) => TokenKind::Exp,
|
||||
}
|
||||
}
|
||||
fn text (&self) -> &str {
|
||||
match self {
|
||||
Self::Num(_)|Self::Exp(_) => "",
|
||||
Self::Sym(s) => s,
|
||||
Self::Key(k) => k,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a> Debug for RefAtom<'a> {
|
||||
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
|
||||
match self {
|
||||
|
|
@ -242,23 +243,8 @@ impl<'a> Debug for RefAtom<'a> {
|
|||
Key(Arc<str>),
|
||||
Exp(Vec<ArcAtom>),
|
||||
}
|
||||
impl<'a> Atom<'a> for ArcAtom {
|
||||
fn kind (&self) -> TokenKind {
|
||||
match self {
|
||||
Self::Num(_) => TokenKind::Num,
|
||||
Self::Sym(_) => TokenKind::Sym,
|
||||
Self::Key(_) => TokenKind::Key,
|
||||
Self::Exp(_) => TokenKind::Exp,
|
||||
}
|
||||
}
|
||||
fn text (&self) -> &str {
|
||||
match self {
|
||||
Self::Num(_)|Self::Exp(_) => "",
|
||||
Self::Sym(s) => s.as_ref(),
|
||||
Self::Key(k) => k.as_ref(),
|
||||
}
|
||||
}
|
||||
fn read_all (source: &'a str) -> impl Iterator<Item = Result<Self, ParseError>> {
|
||||
impl ArcAtom {
|
||||
pub fn read_all <'a> (source: &'a str) -> impl Iterator<Item = Result<ArcAtom, ParseError>> + 'a {
|
||||
TokenIterator::new(source).map(move |result: TokenResult<'a>|match result{
|
||||
Err(e) => Err(e),
|
||||
Ok(token) => match token.kind {
|
||||
|
|
@ -281,6 +267,23 @@ impl<'a> Atom<'a> for ArcAtom {
|
|||
})
|
||||
}
|
||||
}
|
||||
impl<'a> Atom for ArcAtom {
|
||||
fn kind (&self) -> TokenKind {
|
||||
match self {
|
||||
Self::Num(_) => TokenKind::Num,
|
||||
Self::Sym(_) => TokenKind::Sym,
|
||||
Self::Key(_) => TokenKind::Key,
|
||||
Self::Exp(_) => TokenKind::Exp,
|
||||
}
|
||||
}
|
||||
fn text (&self) -> &str {
|
||||
match self {
|
||||
Self::Num(_)|Self::Exp(_) => "",
|
||||
Self::Sym(s) => s.as_ref(),
|
||||
Self::Key(k) => k.as_ref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a> Debug for ArcAtom {
|
||||
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
|
||||
match self {
|
||||
|
|
@ -303,26 +306,26 @@ impl<'a> Display for ArcAtom {
|
|||
}
|
||||
/// Map EDN tokens to parameters of a given type for a given context
|
||||
pub trait Context<'a, U>: Sized {
|
||||
fn get (&'a self, _edn: &'a impl Atom<'a>) -> Option<U> {
|
||||
fn get (&'a self, _edn: &'a impl Atom) -> Option<U> {
|
||||
None
|
||||
}
|
||||
fn get_or_fail (&'a self, edn: &'a impl Atom<'a>) -> U {
|
||||
fn get_or_fail (&'a self, edn: &'a impl Atom) -> U {
|
||||
self.get(edn).expect("no value")
|
||||
}
|
||||
}
|
||||
impl<'a, T: Context<'a, U>, U> Context<'a, U> for &T {
|
||||
fn get (&'a self, edn: &'a impl Atom<'a>) -> Option<U> {
|
||||
fn get (&'a self, edn: &'a impl Atom) -> Option<U> {
|
||||
(*self).get(edn)
|
||||
}
|
||||
fn get_or_fail (&'a self, edn: &'a impl Atom<'a>) -> U {
|
||||
fn get_or_fail (&'a self, edn: &'a impl Atom) -> U {
|
||||
(*self).get_or_fail(edn)
|
||||
}
|
||||
}
|
||||
impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
|
||||
fn get (&'a self, edn: &'a impl Atom<'a>) -> Option<U> {
|
||||
fn get (&'a self, edn: &'a impl Atom) -> Option<U> {
|
||||
self.as_ref().map(|s|s.get(edn)).flatten()
|
||||
}
|
||||
fn get_or_fail (&'a self, edn: &'a impl Atom<'a>) -> U {
|
||||
fn get_or_fail (&'a self, edn: &'a impl Atom) -> U {
|
||||
self.as_ref().map(|s|s.get_or_fail(edn)).expect("no provider")
|
||||
}
|
||||
}
|
||||
|
|
@ -331,7 +334,7 @@ impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
|
|||
// Provide a value to the EDN template
|
||||
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> Context<'a, $type> for $State {
|
||||
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<$type> {
|
||||
fn get (&'a $self, edn: &'a impl Atom) -> Option<$type> {
|
||||
use RefAtom::*;
|
||||
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* _ => return None })
|
||||
}
|
||||
|
|
@ -340,7 +343,7 @@ impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
|
|||
// Provide a value more generically
|
||||
($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<$lt> Context<$lt, $type> for $State {
|
||||
fn get (&$lt $self, edn: &$lt impl Atom<'a>) -> Option<$type> {
|
||||
fn get (&$lt $self, edn: &$lt impl Atom) -> Option<$type> {
|
||||
use RefAtom::*;
|
||||
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* _ => return None })
|
||||
}
|
||||
|
|
@ -354,7 +357,7 @@ impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
|
|||
// Provide a value that may also be a numeric literal in the EDN, to a generic implementation.
|
||||
($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a, $T: $Trait> Context<'a, $type> for $T {
|
||||
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<$type> {
|
||||
fn get (&'a $self, edn: &'a impl Atom) -> Option<$type> {
|
||||
use RefAtom::*;
|
||||
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* Num(n) => n as $type, _ => return None })
|
||||
}
|
||||
|
|
@ -363,7 +366,7 @@ impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
|
|||
// Provide a value that may also be a numeric literal in the EDN, to a concrete implementation.
|
||||
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> Context<'a, $type> for $State {
|
||||
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<$type> {
|
||||
fn get (&'a $self, edn: &'a impl Atom) -> Option<$type> {
|
||||
use RefAtom::*;
|
||||
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* Num(n) => n as $type, _ => return None })
|
||||
}
|
||||
|
|
@ -376,7 +379,7 @@ impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
|
|||
#[macro_export] macro_rules! provide_content {
|
||||
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State {
|
||||
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
fn get (&'a $self, edn: &'a impl Atom) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
use RefAtom::*;
|
||||
Some(match edn.to_ref() { $(RefAtom::Sym($pat) => $expr),*, _ => return None })
|
||||
}
|
||||
|
|
@ -384,7 +387,7 @@ impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
|
|||
};
|
||||
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State {
|
||||
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
fn get (&'a $self, edn: &'a impl Atom) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
use RefAtom::*;
|
||||
Some(match edn.to_ref() { $(Sym($pat) => $expr),*, _ => return None })
|
||||
}
|
||||
|
|
@ -392,11 +395,11 @@ impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
|
|||
}
|
||||
}
|
||||
pub trait TryFromEdn<'a, T>: Sized {
|
||||
fn try_from_edn (state: &'a T, head: &impl Atom<'a>, tail: &'a [impl Atom<'a>]) ->
|
||||
fn try_from_edn (state: &'a T, head: &impl Atom, tail: &'a [impl Atom]) ->
|
||||
Option<Self>;
|
||||
}
|
||||
|
||||
pub trait TryIntoEdn<'a, T>: Sized {
|
||||
fn try_from_edn (state: &'a T, head: &impl Atom<'a>, tail: &'a [impl Atom<'a>]) ->
|
||||
fn try_from_edn (state: &'a T, head: &impl Atom, tail: &'a [impl Atom]) ->
|
||||
Option<Self>;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue