special case numeric literals, and away we go!

This commit is contained in:
🪞👃🪞 2025-01-14 21:07:25 +01:00
parent 228b4bb47c
commit dc45edf7e0
7 changed files with 114 additions and 133 deletions

View file

@ -1,6 +1,15 @@
use crate::*;
/// Implement `EdnProvide` for a type and context
#[macro_export] macro_rules! edn_provide {
// Provide a value to the EDN template
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a> EdnProvide<'a, $type> for $State {
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<$type> {
Some(match edn.to_ref() { $(EdnItem::Sym($pat) => $expr,)* _ => return None })
}
}
};
// Provide a value more generically
($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<$lt> EdnProvide<$lt, $type> for $State {
fn get <S: AsRef<str>> (&$lt $self, edn: &$lt EdnItem<S>) -> Option<$type> {
@ -8,10 +17,15 @@ use crate::*;
}
}
};
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
// Provide a value that may also be a numeric literal in the EDN
(# $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a> EdnProvide<'a, $type> for $State {
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<$type> {
Some(match edn.to_ref() { $(EdnItem::Sym($pat) => $expr,)* _ => return None })
Some(match edn.to_ref() {
$(EdnItem::Sym($pat) => $expr,)*
EdnItem::Num(n) => n as $type,
_ => return None
})
}
}
};