mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
wip: make EdnItem work on Arc<str>
This commit is contained in:
parent
d4f962fbfa
commit
1b9da07280
17 changed files with 152 additions and 260 deletions
|
|
@ -1,11 +1,9 @@
|
|||
use crate::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ParseError {
|
||||
Unknown(u8),
|
||||
#[derive(Debug)] pub enum ParseError {
|
||||
Empty,
|
||||
Incomplete,
|
||||
Unexpected(char),
|
||||
Code(u8),
|
||||
}
|
||||
impl std::fmt::Display for ParseError {
|
||||
fn fmt (&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
|
|
@ -13,7 +11,7 @@ impl std::fmt::Display for ParseError {
|
|||
Self::Empty => write!(f, "empty"),
|
||||
Self::Incomplete => write!(f, "incomplete"),
|
||||
Self::Unexpected(c) => write!(f, "unexpected '{c}'"),
|
||||
Self::Unknown(i) => write!(f, "unknown #{i}"),
|
||||
Self::Code(i) => write!(f, "error #{i}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,31 +1,51 @@
|
|||
use crate::*;
|
||||
|
||||
pub enum EdnItem<T> {
|
||||
Nil,
|
||||
use std::sync::Arc;
|
||||
#[derive(Default, Clone, PartialEq)] pub enum EdnItem {
|
||||
#[default] Nil,
|
||||
Num(usize),
|
||||
Sym(T),
|
||||
Key(T),
|
||||
Exp(Vec<EdnItem<T>>),
|
||||
Sym(Arc<str>),
|
||||
Key(Arc<str>),
|
||||
Exp(Vec<EdnItem>),
|
||||
}
|
||||
impl<T> Default for EdnItem<T> {
|
||||
fn default () -> Self {
|
||||
Self::Nil
|
||||
impl EdnItem {
|
||||
pub fn read_all (mut source: &str) -> Result<Vec<Self>, ParseError> {
|
||||
let mut items = vec![];
|
||||
loop {
|
||||
if source.len() == 0 {
|
||||
break
|
||||
}
|
||||
let (remaining, token) = Token::chomp(source)?;
|
||||
match Self::read(token)? { Self::Nil => {}, item => items.push(item) };
|
||||
source = remaining
|
||||
}
|
||||
Ok(items)
|
||||
}
|
||||
pub fn read_one (source: &str) -> Result<(Self, &str), ParseError> {
|
||||
Ok({
|
||||
if source.len() == 0 {
|
||||
return Err(ParseError::Code(5))
|
||||
}
|
||||
let (remaining, token) = Token::chomp(source)?;
|
||||
(Self::read(token)?, remaining)
|
||||
})
|
||||
}
|
||||
pub fn read <'a> (token: Token<'a>) -> Result<Self, ParseError> {
|
||||
use Token::*;
|
||||
Ok(match token {
|
||||
Nil => EdnItem::Nil,
|
||||
Num(chars, index, length) =>
|
||||
Self::Num(Token::number(&chars[index..index+length])),
|
||||
Sym(chars, index, length) =>
|
||||
Self::Sym((&chars[index..index+length]).into()),
|
||||
Key(chars, index, length) =>
|
||||
Self::Key((&chars[index..index+length]).into()),
|
||||
Exp(chars, index, length, 0) =>
|
||||
Self::Exp(Self::read_all(&chars[index+1..(index+length).saturating_sub(1)])?),
|
||||
_ => panic!("unclosed delimiter")
|
||||
})
|
||||
}
|
||||
}
|
||||
impl<T: Debug + Display> Display for EdnItem<T> {
|
||||
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
|
||||
use EdnItem::*;
|
||||
use itertools::join;
|
||||
match self {
|
||||
Nil => write!(f, ""),
|
||||
Num(u) => write!(f, "{u}"),
|
||||
Sym(u) => write!(f, "{u}"),
|
||||
Key(u) => write!(f, "{u}"),
|
||||
Exp(e) => write!(f, "({})", join(e.iter().map(|i|format!("{}", i)), " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: Debug> Debug for EdnItem<T> {
|
||||
impl Debug for EdnItem {
|
||||
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
|
||||
use EdnItem::*;
|
||||
match self {
|
||||
|
|
@ -38,147 +58,31 @@ impl<T: Debug> Debug for EdnItem<T> {
|
|||
}
|
||||
}
|
||||
}
|
||||
impl<T: PartialEq> PartialEq for EdnItem<T> {
|
||||
fn eq (&self, other: &Self) -> bool {
|
||||
use EdnItem::*;
|
||||
match (self, other) {
|
||||
(Nil, Nil) => true,
|
||||
(Num(a), Num(b)) => a == b,
|
||||
(Sym(a), Sym(b)) => a == b,
|
||||
(Key(a), Key(b)) => a == b,
|
||||
(Exp(a), Exp(b)) => a == b,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
}
|
||||
impl EdnItem<&str> {
|
||||
pub fn clone (&self) -> EdnItem<String> {
|
||||
impl Display for EdnItem {
|
||||
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
|
||||
use EdnItem::*;
|
||||
use itertools::join;
|
||||
match self {
|
||||
Nil => Nil,
|
||||
Num(u) => Num(*u),
|
||||
Sym(u) => Sym(u.to_string()),
|
||||
Key(u) => Key(u.to_string()),
|
||||
Exp(e) => Exp(e.iter().map(|i|i.clone()).collect()),
|
||||
Nil => write!(f, ""),
|
||||
Num(u) => write!(f, "{u}"),
|
||||
Sym(u) => write!(f, "{u}"),
|
||||
Key(u) => write!(f, "{u}"),
|
||||
Exp(e) => write!(f, "({})", join(e.iter().map(|i|format!("{}", i)), " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: AsRef<str>> EdnItem<T> {
|
||||
pub fn to_ref (&self) -> EdnItem<&str> {
|
||||
match self {
|
||||
Self::Nil => EdnItem::Nil,
|
||||
Self::Num(x) => EdnItem::Num(*x),
|
||||
Self::Key(x) => EdnItem::Key(x.as_ref()),
|
||||
Self::Sym(x) => EdnItem::Sym(x.as_ref()),
|
||||
Self::Exp(x) => EdnItem::Exp(x.iter().map(|x|x.to_ref()).collect::<Vec<_>>()),
|
||||
}
|
||||
}
|
||||
pub fn to_str (&self) -> &str {
|
||||
match self {
|
||||
Self::Nil => "",
|
||||
Self::Num(_) => "",
|
||||
Self::Key(x) => x.as_ref(),
|
||||
Self::Sym(x) => x.as_ref(),
|
||||
Self::Exp(_) => "",
|
||||
}
|
||||
}
|
||||
pub fn symbols <'a> (&'a self) -> EdnIterator<'a, T> {
|
||||
EdnIterator::new(&self)
|
||||
}
|
||||
}
|
||||
impl EdnItem<String> {
|
||||
pub fn from (other: EdnItem<impl AsRef<str>>) -> EdnItem<String> {
|
||||
use EdnItem::*;
|
||||
match other {
|
||||
Nil => Nil,
|
||||
Key(t) => Key(t.as_ref().to_string()),
|
||||
_ => todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a> TryFrom<Token<'a>> for EdnItem<String> {
|
||||
impl<'a> TryFrom<Token<'a>> for EdnItem {
|
||||
type Error = ParseError;
|
||||
fn try_from (token: Token<'a>) -> Result<Self, Self::Error> {
|
||||
use Token::*;
|
||||
Ok(match token {
|
||||
Nil => Self::Nil,
|
||||
Num(chars, index, length) =>
|
||||
Self::Num(Token::number(&chars[index..index+length])),
|
||||
Sym(chars, index, length) =>
|
||||
Self::Sym(String::from(&chars[index..index+length])),
|
||||
Key(chars, index, length) =>
|
||||
Self::Key(String::from(&chars[index..index+length])),
|
||||
Exp(chars, index, length, 0) =>
|
||||
Self::Exp(Self::read_all(&chars[index+1..(index+length).saturating_sub(1)])?),
|
||||
Num(chars, index, length) => Self::Num(Token::number(&chars[index..index+length])),
|
||||
Sym(chars, index, length) => Self::Sym((&chars[index..index+length]).into()),
|
||||
Key(chars, index, length) => Self::Key((&chars[index..index+length]).into()),
|
||||
Exp(chars, index, length, 0) => Self::Exp(Self::read_all(
|
||||
&chars[index+1..(index+length).saturating_sub(1)])?),
|
||||
_ => panic!("unclosed delimiter")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<Token<'a>> for EdnItem<&'a str> {
|
||||
type Error = ParseError;
|
||||
fn try_from (token: Token<'a>) -> Result<Self, Self::Error> {
|
||||
use Token::*;
|
||||
Ok(match token {
|
||||
Nil => EdnItem::Nil,
|
||||
Num(chars, index, length) =>
|
||||
Self::Num(Token::number(&chars[index..index+length])),
|
||||
Sym(chars, index, length) =>
|
||||
Self::Sym(&chars[index..index+length]),
|
||||
Key(chars, index, length) =>
|
||||
Self::Key(&chars[index..index+length]),
|
||||
Exp(chars, index, length, 0) =>
|
||||
Self::Exp(Self::read_all(&chars[index+1..(index+length).saturating_sub(1)])?),
|
||||
_ => panic!("unclosed delimiter")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: std::fmt::Debug + Clone + Default + PartialEq + From<&'a str>> EdnItem<T> {
|
||||
pub fn read_all (mut source: &'a str) -> Result<Vec<Self>, ParseError> {
|
||||
let mut items = vec![];
|
||||
loop {
|
||||
if source.len() == 0 {
|
||||
break
|
||||
}
|
||||
let (remaining, token) = Token::chomp(source)?;
|
||||
match Self::read(token)? { Self::Nil => {}, item => items.push(item) };
|
||||
source = remaining
|
||||
}
|
||||
Ok(items)
|
||||
}
|
||||
pub fn read_one (source: &'a str) -> Result<(Self, &'a str), ParseError> {
|
||||
Ok({
|
||||
if source.len() == 0 {
|
||||
return Err(ParseError::Unknown(5))
|
||||
}
|
||||
let (remaining, token) = Token::chomp(source)?;
|
||||
(Self::read(token)?, remaining)
|
||||
})
|
||||
}
|
||||
pub fn read (token: Token<'a>) -> Result<Self, ParseError> {
|
||||
use Token::*;
|
||||
Ok(match token {
|
||||
Nil => EdnItem::Nil,
|
||||
Num(chars, index, length) =>
|
||||
Self::Num(Token::number(&chars[index..index+length])),
|
||||
Sym(chars, index, length) =>
|
||||
Self::Sym(T::from(&chars[index..index+length])),
|
||||
Key(chars, index, length) =>
|
||||
Self::Key(T::from(&chars[index..index+length])),
|
||||
Exp(chars, index, length, 0) =>
|
||||
Self::Exp(Self::read_all(&chars[index+1..(index+length).saturating_sub(1)])?),
|
||||
_ => panic!("unclosed delimiter")
|
||||
})
|
||||
}
|
||||
//pub fn to_str <'a> (&'a self) -> EdnItem<&'a str> {
|
||||
//use EdnItem::*;
|
||||
//match self {
|
||||
//Nil => Nil,
|
||||
//Num(n) => Num(*n),
|
||||
//Sym(t) => Sym(t.as_str()),
|
||||
//Key(t) => Key(t.as_str()),
|
||||
//Exp(t) => Exp(t.iter().map(|x|x.to_str()).collect())
|
||||
//}
|
||||
//}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,28 +1,29 @@
|
|||
use crate::*;
|
||||
pub enum EdnIterator<'a, T>{
|
||||
use std::sync::Arc;
|
||||
pub enum EdnIterator {
|
||||
Nil,
|
||||
Sym(&'a T),
|
||||
Exp(Vec<EdnIterator<'a, T>>)
|
||||
Sym(Arc<str>),
|
||||
Exp(Vec<EdnIterator>)
|
||||
}
|
||||
impl<'a, T: AsRef<str>> EdnIterator<'a, T> {
|
||||
pub fn new (item: &'a EdnItem<T>) -> Self {
|
||||
impl EdnIterator {
|
||||
pub fn new (item: &EdnItem) -> Self {
|
||||
use EdnItem::*;
|
||||
match item {
|
||||
Sym(t) => Self::Sym(t),
|
||||
Sym(t) => Self::Sym(t.clone()),
|
||||
Exp(i) => Self::Exp(i.iter().map(EdnIterator::new).collect()),
|
||||
_ => Self::Nil,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a, T: AsRef<str>> Iterator for EdnIterator<'a, T> {
|
||||
type Item = &'a T;
|
||||
impl Iterator for EdnIterator {
|
||||
type Item = EdnItem;
|
||||
fn next (&mut self) -> Option<Self::Item> {
|
||||
use EdnIterator::*;
|
||||
match self {
|
||||
Sym(t) => {
|
||||
let t = *t;
|
||||
*self = Nil;
|
||||
Some(t)
|
||||
Some(Sym(t))
|
||||
},
|
||||
Exp(v) => match v.as_mut_slice() {
|
||||
[a] => if let Some(next) = a.next() {
|
||||
|
|
|
|||
|
|
@ -4,26 +4,26 @@ use crate::*;
|
|||
// 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 })
|
||||
fn get (&'a $self, edn: &'a EdnItem) -> Option<$type> {
|
||||
Some(match EdnItem::<&str>::from(edn) { $(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> {
|
||||
Some(match edn.to_ref() { $(EdnItem::Sym($pat) => $expr,)* _ => return None })
|
||||
fn get (&$lt $self, edn: &$lt EdnItem) -> Option<$type> {
|
||||
Some(match EdnItem::<&str>::from(edn) { $(EdnItem::Sym($pat) => $expr,)* _ => return None })
|
||||
}
|
||||
}
|
||||
};
|
||||
// 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> EdnProvide<'a, $type> for $T {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<$type> {
|
||||
Some(match edn.to_ref() {
|
||||
fn get (&'a $self, edn: &'a EdnItem) -> Option<$type> {
|
||||
Some(match edn {
|
||||
$(EdnItem::Sym($pat) => $expr,)*
|
||||
EdnItem::Num(n) => n as $type,
|
||||
EdnItem::Num(n) => *n as $type,
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
|
|
@ -32,10 +32,10 @@ use crate::*;
|
|||
// 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> EdnProvide<'a, $type> for $State {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<$type> {
|
||||
Some(match edn.to_ref() {
|
||||
fn get (&'a $self, edn: &'a EdnItem) -> Option<$type> {
|
||||
Some(match edn {
|
||||
$(EdnItem::Sym($pat) => $expr,)*
|
||||
EdnItem::Num(n) => n as $type,
|
||||
EdnItem::Num(n) => *n as $type,
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
|
|
@ -44,26 +44,26 @@ use crate::*;
|
|||
}
|
||||
/// Map EDN tokens to parameters of a given type for a given context
|
||||
pub trait EdnProvide<'a, U>: Sized {
|
||||
fn get <S: AsRef<str>> (&'a self, _edn: &'a EdnItem<S>) -> Option<U> {
|
||||
fn get (&'a self, _edn: &'a EdnItem) -> Option<U> {
|
||||
None
|
||||
}
|
||||
fn get_or_fail <S: AsRef<str>> (&'a self, edn: &'a EdnItem<S>) -> U {
|
||||
fn get_or_fail (&'a self, edn: &'a EdnItem) -> U {
|
||||
self.get(edn).expect("no value")
|
||||
}
|
||||
}
|
||||
impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for &T {
|
||||
fn get <S: AsRef<str>> (&'a self, edn: &'a EdnItem<S>) -> Option<U> {
|
||||
fn get (&'a self, edn: &'a EdnItem) -> Option<U> {
|
||||
(*self).get(edn)
|
||||
}
|
||||
fn get_or_fail <S: AsRef<str>> (&'a self, edn: &'a EdnItem<S>) -> U {
|
||||
fn get_or_fail (&'a self, edn: &'a EdnItem) -> U {
|
||||
(*self).get_or_fail(edn)
|
||||
}
|
||||
}
|
||||
impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for Option<T> {
|
||||
fn get <S: AsRef<str>> (&'a self, edn: &'a EdnItem<S>) -> Option<U> {
|
||||
fn get (&'a self, edn: &'a EdnItem) -> Option<U> {
|
||||
self.as_ref().map(|s|s.get(edn)).flatten()
|
||||
}
|
||||
fn get_or_fail <S: AsRef<str>> (&'a self, edn: &'a EdnItem<S>) -> U {
|
||||
fn get_or_fail (&'a self, edn: &'a EdnItem) -> U {
|
||||
self.as_ref().map(|s|s.get_or_fail(edn)).expect("no provider")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,12 @@
|
|||
#![feature(type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_fn_trait_return)]
|
||||
|
||||
pub(crate) use std::{fmt::{Debug, Display, Formatter, Error as FormatError}};
|
||||
|
||||
mod edn_error; pub use self::edn_error::*;
|
||||
mod edn_item; pub use self::edn_item::*;
|
||||
mod edn_iter; pub use self::edn_iter::*;
|
||||
//mod edn_iter; pub use self::edn_iter::*;
|
||||
mod edn_token; pub use self::edn_token::*;
|
||||
mod edn_provide; pub use self::edn_provide::*;
|
||||
mod try_from_edn; pub use self::try_from_edn::*;
|
||||
|
||||
pub(crate) use std::{fmt::{Debug, Display, Formatter, Error as FormatError}};
|
||||
#[cfg(test)] #[test] fn test_edn () -> Result<(), ParseError> {
|
||||
use EdnItem::*;
|
||||
assert_eq!(EdnItem::<String>::read_all("")?,
|
||||
|
|
@ -30,7 +27,6 @@ mod try_from_edn; pub use self::try_from_edn::*;
|
|||
vec![Exp(vec![Key("foo/bar".into()), Sym(":baz".into()), Num(456)])]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)] #[test] fn test_edn_layout () -> Result<(), ParseError> {
|
||||
EdnItem::<String>::read_all(include_str!("../../output/examples/edn01.edn"))?;
|
||||
EdnItem::<String>::read_all(include_str!("../../output/examples/edn02.edn"))?;
|
||||
|
|
@ -38,7 +34,6 @@ mod try_from_edn; pub use self::try_from_edn::*;
|
|||
//let content = <dyn EdnViewData<::tek_engine::tui::Tui>>::from(&layout);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[macro_export] macro_rules! from_edn {
|
||||
($($x:tt)*) => {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::*;
|
||||
|
||||
pub trait TryFromEdn<'a, T>: Sized {
|
||||
fn try_from_edn (state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) ->
|
||||
fn try_from_edn (state: &'a T, head: &EdnItem, tail: &'a [EdnItem]) ->
|
||||
Option<Self>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,14 +2,14 @@ use crate::*;
|
|||
|
||||
pub trait EdnInput: Input {
|
||||
fn matches_edn (&self, token: &str) -> bool;
|
||||
fn get_event <S: AsRef<str>> (_: &EdnItem<S>) -> Option<Self::Event> {
|
||||
fn get_event <S: AsRef<str>> (_: &EdnItem) -> Option<Self::Event> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns an EDN item sequence into a command enum variant.
|
||||
pub trait EdnCommand<C>: Command<C> {
|
||||
fn from_edn <'a> (state: &C, head: &EdnItem<&str>, tail: &'a [EdnItem<&'a str>])
|
||||
fn from_edn <'a> (state: &C, head: &EdnItem, tail: &'a [EdnItem])
|
||||
-> Option<Self>;
|
||||
}
|
||||
|
||||
|
|
@ -39,8 +39,8 @@ pub trait EdnCommand<C>: Command<C> {
|
|||
impl<$T: $Trait> EdnCommand<$T> for $Command {
|
||||
fn from_edn <'a> (
|
||||
$state: &$T,
|
||||
head: &EdnItem<&str>,
|
||||
tail: &'a [EdnItem<&'a str>]
|
||||
head: &EdnItem,
|
||||
tail: &'a [EdnItem]
|
||||
) -> Option<Self> {
|
||||
$(if let (EdnItem::Key($key), [ // if the identifier matches
|
||||
// bind argument ids
|
||||
|
|
@ -81,9 +81,7 @@ pub trait EdnCommand<C>: Command<C> {
|
|||
))* }) => {
|
||||
impl EdnCommand<$State> for $Command {
|
||||
fn from_edn <'a> (
|
||||
$state: &$State,
|
||||
head: &EdnItem<&str>,
|
||||
tail: &'a [EdnItem<&'a str>]
|
||||
$state: &$State, head: &EdnItem, tail: &'a [EdnItem]
|
||||
) -> Option<Self> {
|
||||
$(if let (EdnItem::Key($key), [ // if the identifier matches
|
||||
// bind argument ids
|
||||
|
|
@ -109,11 +107,11 @@ pub trait EdnCommand<C>: Command<C> {
|
|||
};
|
||||
}
|
||||
|
||||
pub struct EdnKeyMapToCommand<'a>(Vec<EdnItem<&'a str>>);
|
||||
impl<'a> EdnKeyMapToCommand<'a> {
|
||||
pub struct EdnKeyMapToCommand(Vec<EdnItem>);
|
||||
impl EdnKeyMapToCommand {
|
||||
/// Construct keymap from source text or fail
|
||||
pub fn new (keymap: &'a str) -> Usually<Self> {
|
||||
Ok(Self(EdnItem::<&str>::read_all(keymap)?))
|
||||
pub fn new (keymap: &str) -> Usually<Self> {
|
||||
Ok(Self(EdnItem::read_all(keymap)?))
|
||||
}
|
||||
/// Try to find a binding matching the currently pressed key
|
||||
pub fn from <T, C> (&self, state: &T, input: &impl EdnInput) -> Option<C>
|
||||
|
|
|
|||
|
|
@ -192,7 +192,7 @@ content!(TuiOut: |self: ClipLength| {
|
|||
impl PoolCommand {
|
||||
pub fn from_tui_event (state: &MidiPool, input: &impl EdnInput) -> Usually<Option<Self>> {
|
||||
use EdnItem::*;
|
||||
let edns: Vec<EdnItem<&str>> = EdnItem::read_all(match state.mode() {
|
||||
let edns: Vec<EdnItem> = EdnItem::read_all(match state.mode() {
|
||||
Some(PoolMode::Rename(..)) => KEYS_RENAME,
|
||||
Some(PoolMode::Length(..)) => KEYS_LENGTH,
|
||||
Some(PoolMode::Import(..)) | Some(PoolMode::Export(..)) => KEYS_FILE,
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ impl<E: Output, A: Content<E>> Content<E> for Align<E, A> {
|
|||
}
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for Align<E, RenderBox<'a, E>> {
|
||||
fn try_from_edn (state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> Option<Self> {
|
||||
fn try_from_edn (state: &'a T, head: &EdnItem, tail: &'a [EdnItem]) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
Some(match (head, tail) {
|
||||
(Key("align/c"), [a]) => Self::c(state.get_content(a).expect("no content")),
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ impl<E: Output, A: Content<E>, B: Content<E>> BspAreas<E, A, B> for Bsp<E, A, B>
|
|||
fn contents (&self) -> (&A, &B) { (&self.2, &self.3) }
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for Bsp<E, RenderBox<'a, E>, RenderBox<'a, E>> {
|
||||
fn try_from_edn (s: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> Option<Self> {
|
||||
fn try_from_edn (s: &'a T, head: &EdnItem, tail: &'a [EdnItem]) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
Some(match (head, tail) {
|
||||
(Key("bsp/n"), [a, b]) => Self::n(
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ use EdnItem::*;
|
|||
}
|
||||
$(
|
||||
impl<'a> EdnProvide<'a, $type> for $App {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<$type> {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem) -> Option<$type> {
|
||||
Some(match edn.to_ref() { $(EdnItem::Sym($sym) => $value,)* _ => return None })
|
||||
}
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@ use EdnItem::*;
|
|||
#[macro_export] macro_rules! edn_provide_content {
|
||||
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a, E: Output> EdnProvide<'a, Box<dyn Render<E> + 'a>> for $State {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
Some(match edn.to_ref() {
|
||||
$(EdnItem::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
|
|
@ -33,7 +33,7 @@ use EdnItem::*;
|
|||
};
|
||||
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> EdnProvide<'a, Box<dyn Render<$Output> + 'a>> for $State {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
Some(match edn.to_ref() {
|
||||
$(EdnItem::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
|
|
@ -45,7 +45,7 @@ use EdnItem::*;
|
|||
/// Renders from EDN source and context.
|
||||
#[derive(Default)] pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> {
|
||||
#[default] Inert,
|
||||
Ok(T, EdnItem<&'a str>),
|
||||
Ok(T, EdnItem),
|
||||
//render: Box<dyn Fn(&'a T)->Box<dyn Render<E> + Send + Sync + 'a> + Send + Sync + 'a>
|
||||
Err(String),
|
||||
_Unused(PhantomData<&'a E>),
|
||||
|
|
@ -76,7 +76,7 @@ impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> EdnView<'a, E, T> {
|
|||
Err(error) => Self::Err(format!("{error} in {source}"))
|
||||
}
|
||||
}
|
||||
pub fn from_items (state: T, items: Vec<EdnItem<&'a str>>) -> Self {
|
||||
pub fn from_items (state: T, items: Vec<EdnItem>) -> Self {
|
||||
Self::Ok(state, EdnItem::Exp(items))
|
||||
}
|
||||
}
|
||||
|
|
@ -92,6 +92,17 @@ impl<E: Output, T: for<'a>EdnViewData<'a, E> + Send + Sync + std::fmt::Debug> Co
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! edn_try_delegate {
|
||||
($s:ident, $e:expr, $T:ty) => {
|
||||
if let [head, tail @ ..] = $e.as_slice() {
|
||||
if let Some(content) = <$T>::try_from_edn($s, head, tail) {
|
||||
return Some(content.boxed())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides values to the template
|
||||
pub trait EdnViewData<'a, E: Output>:
|
||||
EdnProvide<'a, bool> +
|
||||
|
|
@ -99,55 +110,42 @@ pub trait EdnViewData<'a, E: Output>:
|
|||
EdnProvide<'a, E::Unit> +
|
||||
EdnProvide<'a, Box<dyn Render<E> + 'a>>
|
||||
{
|
||||
fn get_bool (&'a self, item: &'a EdnItem<&str>) -> Option<bool> {
|
||||
fn get_bool (&'a self, item: &'a EdnItem) -> Option<bool> {
|
||||
Some(match &item {
|
||||
Sym(":false") | Sym(":f") | Num(0) => false,
|
||||
Sym(":true") | Sym(":t") | Num(_) => true,
|
||||
Sym(s) => match s.as_ref() {
|
||||
":false" | ":f" => false,
|
||||
":true" | ":t" => true,
|
||||
_ => return EdnProvide::get(self, item)
|
||||
},
|
||||
Num(0) => false,
|
||||
Num(_) => true,
|
||||
_ => return EdnProvide::get(self, item)
|
||||
})
|
||||
}
|
||||
fn get_usize (&'a self, item: &'a EdnItem<&str>) -> Option<usize> {
|
||||
fn get_usize (&'a self, item: &'a EdnItem) -> Option<usize> {
|
||||
Some(match &item { Num(n) => *n, _ => return EdnProvide::get(self, item) })
|
||||
}
|
||||
fn get_unit (&'a self, item: &'a EdnItem<&str>) -> Option<E::Unit> {
|
||||
fn get_unit (&'a self, item: &'a EdnItem) -> Option<E::Unit> {
|
||||
Some(match &item { Num(n) => (*n as u16).into(), _ => return EdnProvide::get(self, item) })
|
||||
}
|
||||
fn get_content (&'a self, item: &'a EdnItem<&'a str>) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
||||
fn get_content (&'a self, item: &'a EdnItem) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
||||
Some(match item {
|
||||
Nil => Box::new(()),
|
||||
Exp(e) => if let [head, tail @ ..] = e.as_slice() {
|
||||
if let Some(builtin) = When::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Either::<_, RenderBox<'a, E>, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Align::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Bsp::<_, RenderBox<'a, E>, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Fill::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Fixed::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Min::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Max::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Shrink::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Expand::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Push::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Pull::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Margin::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Padding::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else {
|
||||
EdnProvide::get_or_fail(self, &item)
|
||||
}
|
||||
} else {
|
||||
Exp(ref e) => {
|
||||
edn_try_delegate!(self, e, When::<_, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Either::<_, RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Align::<_, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Bsp::<_, RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Fill::<_, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Fixed::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Min::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Max::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Shrink::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Expand::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Push::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Pull::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Margin::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Padding::<_, _, RenderBox<'a, E>>);
|
||||
EdnProvide::get_or_fail(self, &item)
|
||||
},
|
||||
_ => EdnProvide::get_or_fail(self, &item)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ impl<E, A, B> Either<E, A, B> {
|
|||
}
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for Either<E, RenderBox<'a, E>, RenderBox<'a, E>> {
|
||||
fn try_from_edn (state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> Option<Self> {
|
||||
fn try_from_edn (state: &'a T, head: &EdnItem, tail: &'a [EdnItem]) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
if let (Key("either"), [condition, content, alternative]) = (head, tail) {
|
||||
Some(Self::new(
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ macro_rules! transform_xy {
|
|||
}
|
||||
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for $Enum<E, RenderBox<'a, E>> {
|
||||
fn try_from_edn (
|
||||
state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]
|
||||
state: &'a T, head: &EdnItem, tail: &'a [EdnItem]
|
||||
) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
Some(match (head, tail) {
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ macro_rules! transform_xy_unit {
|
|||
}
|
||||
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for $Enum<E, E::Unit, RenderBox<'a, E>> {
|
||||
fn try_from_edn (
|
||||
state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]
|
||||
state: &'a T, head: &EdnItem, tail: &'a [EdnItem]
|
||||
) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
Some(match (head, tail) {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ impl<E, A> When<E, A> {
|
|||
}
|
||||
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for When<E, RenderBox<'a, E>> {
|
||||
fn try_from_edn (
|
||||
state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]
|
||||
state: &'a T, head: &EdnItem, tail: &'a [EdnItem]
|
||||
) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
if let (Key("when"), [condition, content]) = (head, tail) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
mod plugin; pub use self::plugin::*;
|
||||
mod lv2; pub use self::lv2::*;
|
||||
|
||||
pub(crate) use std::cmp::Ord;
|
||||
pub(crate) use std::fmt::{Debug, Formatter};
|
||||
pub(crate) use std::sync::{Arc, RwLock};
|
||||
pub(crate) use std::thread::JoinHandle;
|
||||
pub(crate) use ::tek_jack::{*, jack::*};
|
||||
pub(crate) use ::tek_tui::{
|
||||
*,
|
||||
|
|
@ -10,8 +13,3 @@ pub(crate) use ::tek_tui::{
|
|||
ratatui::prelude::*,
|
||||
crossterm::event::*,
|
||||
};
|
||||
|
||||
pub(crate) use std::cmp::Ord;
|
||||
pub(crate) use std::fmt::{Debug, Formatter};
|
||||
pub(crate) use std::sync::{Arc, RwLock};
|
||||
pub(crate) use std::thread::JoinHandle;
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ impl EdnInput for TuiIn {
|
|||
false
|
||||
}
|
||||
}
|
||||
fn get_event <S: AsRef<str>> (item: &EdnItem<S>) -> Option<Event> {
|
||||
fn get_event (item: &EdnItem) -> Option<Event> {
|
||||
match item { EdnItem::Sym(s) => KeyMatcher::new(s).build(), _ => None }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue