remove Atom. almost there

This commit is contained in:
🪞👃🪞 2025-01-18 15:37:53 +01:00
parent dc7b713108
commit cf1fd5b45a
20 changed files with 539 additions and 739 deletions

View file

@ -1,10 +0,0 @@
use crate::*;
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub enum AtomType {
#[default] Nil, Num, Sym, Key, Exp
}
pub trait Atom: Sized + Send + Sync {
fn kind (&self) -> TokenKind;
fn text (&self) -> &str;
fn num (&self) -> usize;
fn exp (&self) -> impl Iterator<Item = Self>;
}

View file

@ -1,91 +0,0 @@
use crate::*;
pub type ArcAtomResult = ParseResult<ArcAtom>;
//const_iter!(|self: ArcAtomIter| => ArcAtom =>
//Some(if let Some(token) = Iterator::next(&mut self.0) {
//token.to_arc_atom(self.0.0).unwrap()
//} else {
//return None
//}));
#[derive(Clone, PartialEq)] pub enum ArcAtom {
Num(usize),
Sym(Arc<str>),
Key(Arc<str>),
Exp(Vec<ArcAtom>),
}
impl ArcAtom {
pub fn read_all <'a> (source: &'a str) -> impl Iterator<Item = Result<ArcAtom, ParseError>> + 'a {
TokenResultIterator::new(source).map(move |result: TokenResult<'a>|match result{
Err(e) => Err(e),
Ok(token) => match token.kind() {
Nil => Err(ParseError::Empty),
Num => match to_number(token.slice_source(source).into()) {
Ok(n) => Ok(ArcAtom::Num(n)),
Err(e) => return Err(e)
},
Sym => Ok(ArcAtom::Sym(token.slice_source(source).into())),
Key => Ok(ArcAtom::Key(token.slice_source(source).into())),
Exp => Ok(ArcAtom::Exp({
let mut iter = Self::read_all(token.slice_source(source));
let mut atoms = vec![];
while let Some(atom) = iter.next() {
atoms.push(atom?);
}
atoms
}))
},
})
}
}
impl 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(),
}
}
fn num (&self) -> usize {
match self {
Self::Num(n) => *n,
_ => 0
}
}
fn exp (&self) -> impl Iterator<Item = ArcAtom> {
let cheap_clone = |t: &ArcAtom|t.clone();
if let Self::Exp(e) = self {
return e.iter().map(cheap_clone)
} else {
let empty: &[ArcAtom] = &[];
empty.iter().map(cheap_clone)
}
}
}
impl<'a> Debug for ArcAtom {
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
match self {
Self::Num(n) => write!(f, "(num {n})"),
Self::Sym(t) => write!(f, "(sym {t:?})"),
Self::Key(t) => write!(f, "(key {t:?})"),
Self::Exp(u) => write!(f, "(exp {u:?})"),
}
}
}
impl<'a> Display for ArcAtom {
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
match self {
Self::Num(n) => write!(f, "{n}"),
Self::Sym(t) => write!(f, "{t}"),
Self::Key(t) => write!(f, "{t}"),
Self::Exp(u) => write!(f, "({})", join(u.iter().map(|i|format!("{}", &i)), " "))
}
}
}

View file

@ -1,78 +0,0 @@
use crate::*;
pub type RefAtomResult<'a> = ParseResult<RefAtom<'a>>;
#[derive(Copy, Clone, PartialEq)] pub struct RefAtomIter<'a>(pub TokenIter<'a>);
const_iter!(<'a>|self: RefAtomIter<'a>| => RefAtom<'a> =>
Some(if let Some(token) = Iterator::next(&mut self.0) {
token.to_ref_atom(self.0.0).unwrap()
} else {
return None
}));
impl<'a> From<&'a str> for RefAtomIter<'a> {
fn from (source: &'a str) -> Self { Self::from_source(source) }
}
impl<'a> From<TokenIter<'a>> for RefAtomIter<'a> {
fn from (iter: TokenIter<'a>) -> Self { Self::from_tokens_iter(iter) }
}
impl<'a> RefAtomIter<'a> {
pub const fn from_source (source: &'a str) -> Self { Self(TokenIter(source)) }
pub const fn from_tokens_iter (iter: TokenIter<'a>) -> Self { Self(iter) }
}
#[derive(Copy, Clone, PartialEq)] pub struct RefAtomResultIterator<'a>(pub TokenResultIterator<'a>);
const_iter!(<'a>|self: RefAtomResultIterator<'a>| => RefAtomResult<'a> =>
Some(if let Some(result) = Iterator::next(&mut self.0) {
match result { Ok(token) => token.to_ref_atom(self.0.0), Err(e) => Err(e), }
} else {
return None
}));
impl<'a> From<&'a str> for RefAtomResultIterator<'a> {
fn from (source: &'a str) -> Self { Self::from_source(source) }
}
impl<'a> From<TokenResultIterator<'a>> for RefAtomResultIterator<'a> {
fn from (iter: TokenResultIterator<'a>) -> Self { Self::from_tokens_iter(iter) }
}
impl<'a> RefAtomResultIterator<'a> {
pub const fn from_source (source: &'a str) -> Self { Self(TokenResultIterator(source)) }
pub const fn from_tokens_iter (iter: TokenResultIterator<'a>) -> Self { Self(iter) }
}
#[derive(Copy, Clone, PartialEq)] pub enum RefAtom<'a> {
Num(usize), Sym(&'a str), Key(&'a str), Exp(RefAtomIter<'a>),
}
impl<'a> RefAtom<'a> {
pub fn to_arc_atom (&self) -> ArcAtom { todo!() }
pub fn read_all <'b: 'a> (source: &'b str) -> impl Iterator<Item = ParseResult<RefAtom<'b>>> + 'b {
TokenResultIterator::new(source).map(move |result|match result {
Ok(token) => token.to_ref_atom(source), Err(e) => Err(e),
})
}
}
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,
}
}
fn num (&self) -> usize { match self { Self::Num(n) => *n, _ => 0 } }
fn exp (&self) -> impl Iterator<Item = RefAtom<'a>> {
match self { Self::Exp(e) => *e, _ => RefAtomIter::from("") }
}
}
impl<'a> Debug for RefAtom<'a> {
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
match self {
RefAtom::Num(n) => write!(f, "(num {n})"),
RefAtom::Sym(t) => write!(f, "(sym {t:?})"),
RefAtom::Key(t) => write!(f, "(key {t:?})"),
RefAtom::Exp(u) => write!(f, "(exp {})", join(u.clone().map(|t|format!("{t:?}")), ","))
}
}
}

View file

@ -1,26 +1,38 @@
use crate::*; use crate::*;
/// Map EDN tokens to parameters of a given type for a given context pub trait TryFromAtom<T>: Sized {
pub trait Context<U>: Sized { fn try_from_atom (state: &T, value: Value) -> Option<Self> {
fn get (&self, _atom: &impl Atom) -> Option<U> { if let Value::Exp(0, iter) = value { return Self::try_from_expr(state, iter) }
None None
} }
fn get_or_fail (&self, atom: &impl Atom) -> U { fn try_from_expr (_state: &T, _iter: TokenIter) -> Option<Self> {
None
}
}
pub trait TryIntoAtom<T>: Sized {
fn try_into_atom (&self) -> Option<Value>;
}
/// Map EDN tokens to parameters of a given type for a given context
pub trait Context<U>: Sized {
fn get (&self, _atom: &Value) -> Option<U> {
None
}
fn get_or_fail (&self, atom: &Value) -> U {
self.get(atom).expect("no value") self.get(atom).expect("no value")
} }
} }
impl<T: Context<U>, U> Context<U> for &T { impl<T: Context<U>, U> Context<U> for &T {
fn get (&self, atom: &impl Atom) -> Option<U> { fn get (&self, atom: &Value) -> Option<U> {
(*self).get(atom) (*self).get(atom)
} }
fn get_or_fail (&self, atom: &impl Atom) -> U { fn get_or_fail (&self, atom: &Value) -> U {
(*self).get_or_fail(atom) (*self).get_or_fail(atom)
} }
} }
impl<T: Context<U>, U> Context<U> for Option<T> { impl<T: Context<U>, U> Context<U> for Option<T> {
fn get (&self, atom: &impl Atom) -> Option<U> { fn get (&self, atom: &Value) -> Option<U> {
self.as_ref().map(|s|s.get(atom)).flatten() self.as_ref().map(|s|s.get(atom)).flatten()
} }
fn get_or_fail (&self, atom: &impl Atom) -> U { fn get_or_fail (&self, atom: &Value) -> U {
self.as_ref().map(|s|s.get_or_fail(atom)).expect("no provider") self.as_ref().map(|s|s.get_or_fail(atom)).expect("no provider")
} }
} }
@ -29,9 +41,10 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
// Provide a value to the EDN template // Provide a value to the EDN template
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => { ($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl Context<$type> for $State { impl Context<$type> for $State {
fn get (&$self, atom: &impl Atom) -> Option<$type> { #[allow(unreachable_code)]
Some(match (atom.kind(), atom.text()) { fn get (&$self, atom: &Value) -> Option<$type> {
$((TokenKind::Sym, $pat) => $expr,)* Some(match atom {
$(Value::Sym($pat) => $expr,)*
_ => return None _ => return None
}) })
} }
@ -40,9 +53,10 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
// Provide a value more generically // Provide a value more generically
($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => { ($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<$lt> Context<$lt, $type> for $State { impl<$lt> Context<$lt, $type> for $State {
fn get (&$lt $self, atom: &impl Atom) -> Option<$type> { #[allow(unreachable_code)]
Some(match (atom.kind(), atom.text()) { fn get (&$lt $self, atom: &Value) -> Option<$type> {
$((TokenKind::Sym, $pat) => $expr,)* Some(match atom {
$(Value::Sym($pat) => $expr,)*
_ => return None _ => return None
}) })
} }
@ -56,10 +70,10 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
// Provide a value that may also be a numeric literal in the EDN, to a generic implementation. // 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),* $(,)? }) => { ($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<$T: $Trait> Context<$type> for $T { impl<$T: $Trait> Context<$type> for $T {
fn get (&$self, atom: &impl Atom) -> Option<$type> { fn get (&$self, atom: &Value) -> Option<$type> {
Some(match (atom.kind(), atom.text()) { Some(match atom {
$((TokenKind::Sym, $pat) => $expr,)* $(Value::Sym($pat) => $expr,)*
(TokenKind::Num, _) => atom.num() as $type, Value::Num(n) => *n as $type,
_ => return None _ => return None
}) })
} }
@ -68,10 +82,10 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
// Provide a value that may also be a numeric literal in the EDN, to a concrete implementation. // 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),* $(,)? }) => { ($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl Context<$type> for $State { impl Context<$type> for $State {
fn get (&$self, atom: &impl Atom) -> Option<$type> { fn get (&$self, atom: &Value) -> Option<$type> {
Some(match (atom.kind(), atom.text()) { Some(match atom {
$((TokenKind::Sym, $pat) => $expr,)* $(Value::Sym($pat) => $expr,)*
(TokenKind::Num, _) => atom.num() as $type, Value::Num(n) => *n as $type,
_ => return None _ => return None
}) })
} }
@ -84,17 +98,21 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
#[macro_export] macro_rules! provide_content { #[macro_export] macro_rules! provide_content {
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => { (|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State { impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State {
fn get (&'a $self, atom: &'a impl Atom) -> Option<Box<dyn Render<E> + 'a>> { fn get (&'a $self, atom: &'a Value) -> Option<Box<dyn Render<E> + 'a>> {
use RefAtom::*; Some(match (atom.kind(), atom.text()) {
Some(match atom.to_ref() { $(RefAtom::Sym($pat) => $expr),*, _ => return None }) $(Value::Sym($pat) => $expr,)*
_ => return None
})
} }
} }
}; };
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => { ($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State { impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State {
fn get (&'a $self, atom: &'a impl Atom) -> Option<Box<dyn Render<$Output> + 'a>> { fn get (&'a $self, atom: &'a Value) -> Option<Box<dyn Render<$Output> + 'a>> {
use RefAtom::*; Some(match (atom.kind(), atom.text()) {
Some(match atom.to_ref() { $(Sym($pat) => $expr),*, _ => return None }) $(Value::Sym($pat) => $expr,)*
_ => return None
})
} }
} }
} }

87
edn/src/iter.rs Normal file
View file

@ -0,0 +1,87 @@
use crate::*;
#[derive(Copy, Clone, Debug, PartialEq)] pub struct TokenIter<'a>(pub &'a str);
const_iter!(<'a>|self: TokenIter<'a>| => Token<'a> => self.next_mut().map(|(result, _)|result));
impl<'a> From<&'a str> for TokenIter<'a> {fn from (source: &'a str) -> Self{Self::new(source)}}
impl<'a> TokenIter<'a> {
pub const fn new (source: &'a str) -> Self { Self(source) }
pub const fn chomp (&self, index: usize) -> Self { Self(split_at(self.0, index).1) }
pub const fn next (mut self) -> Option<(Token<'a>, Self)> { Self::next_mut(&mut self) }
pub const fn peek (&self) -> Option<Token<'a>> { peek_src(self.0) }
pub const fn next_mut (&mut self) -> Option<(Token<'a>, Self)> {
match self.peek() {
Some(token) => Some((token, self.chomp(token.end()))),
None => None
}
}
}
pub const fn peek_src <'a> (source: &'a str) -> Option<Token<'a>> {
let mut token: Token<'a> = Token::new(source, Nil, 0, 0);
iterate!(char_indices(source) => (start, c) => token = match token.value() {
Err(_) => return Some(token),
Nil => match c {
' '|'\n'|'\r'|'\t' => token.grow(),
'(' => Token {
source, start, length: 1,
value: Value::Exp(1, TokenIter(str_range(source, start, start + 1))),
},
'0'..='9' => Token {
source, start, length: 1,
value: match to_digit(c) {
Ok(c) => Value::Num(c),
Result::Err(e) => Value::Err(e)
}
},
':'|'@' => Token {
source, start, length: 1,
value: Value::Sym(str_range(source, start, start + 1)),
},
'/'|'a'..='z' => Token {
source, start, length: 1,
value: Value::Key(str_range(source, start, start + 1)),
},
_ => token.error(Unexpected(c))
},
Num(_) => match c {
'0'..='9' => token.grow(),
' '|'\n'|'\r'|'\t' => return Some(token),
_ => token.error(Unexpected(c))
},
Sym(_) => match c {
'a'..='z'|'0'..='9'|'-' => token.grow(),
' '|'\n'|'\r'|'\t' => return Some(token),
_ => token.error(Unexpected(c))
},
Key(_) => match c {
'a'..='z'|'0'..='9'|'-'|'/' => token.grow(),
' '|'\n'|'\r'|'\t' => return Some(token),
_ => token.error(Unexpected(c))
},
Exp(depth, _) => match depth {
0 => return Some(token),
_ => match c {
')' => token.grow_out(),
'(' => token.grow_in(),
_ => token.grow(),
}
},
});
match token.value() {
Nil => None,
_ => Some(token),
}
}
pub const fn to_number (digits: &str) -> Result<usize, ParseError> {
let mut value = 0;
iterate!(char_indices(digits) => (_, c) => match to_digit(c) {
Ok(digit) => value = 10 * value + digit,
Result::Err(e) => return Result::Err(e)
});
Ok(value)
}
pub const fn to_digit (c: char) -> Result<usize, ParseError> {
Ok(match c {
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
'5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
_ => return Result::Err(Unexpected(c))
})
}

View file

@ -1,12 +1,14 @@
#![feature(adt_const_params)] #![feature(adt_const_params)]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_fn_trait_return)] #![feature(impl_trait_in_fn_trait_return)]
mod error; pub use self::error::*; mod error; pub use self::error::*;
mod token; pub use self::token::*; mod token; pub use self::token::*;
mod atom; pub use self::atom::*; mod iter; pub use self::iter::*;
mod atom_ref; pub use self::atom_ref::*; mod context; pub use self::context::*;
mod atom_arc; pub use self::atom_arc::*; //mod atom; pub use self::atom::*;
mod context; pub use self::context::*; //mod atom_ref; pub use self::atom_ref::*;
//mod atom_arc; pub use self::atom_arc::*;
pub(crate) use self::Value::*;
pub(crate) use self::ParseError::*; pub(crate) use self::ParseError::*;
//pub(crate) use self::TokenKind::*; //pub(crate) use self::TokenKind::*;
pub(crate) use std::sync::Arc; pub(crate) use std::sync::Arc;
@ -40,6 +42,17 @@ pub(crate) use std::fmt::{Debug, Display, Formatter, Result as FormatResult, Err
} }
} }
} }
#[cfg(test)] #[test] fn test_token () -> Result<(), Box<dyn std::error::Error>> {
use Token::*;
assert_eq!(Nil, Token::chomp_one("")?);
assert_eq!(Nil, Token::chomp_one(" \n \r \t ")?);
assert_eq!(Num("8", 0, 1), Token::chomp_one("8")?);
assert_eq!(Num(" 8 ", 3, 1), Token::chomp_one(" 8 ")?);
assert_eq!(Sym(":foo", 0, 4), Token::chomp_one(":foo")?);
assert_eq!(Sym("@bar", 0, 4), Token::chomp_one("@bar")?);
assert_eq!(Key("foo/bar", 0, 7), Token::chomp_one("foo/bar")?);
Ok(())
}
#[cfg(test)] #[test] fn test_lang () -> Result<(), ParseError> { #[cfg(test)] #[test] fn test_lang () -> Result<(), ParseError> {
use Atom::*; use Atom::*;
assert_eq!(Atom::read_all("")?, assert_eq!(Atom::read_all("")?,

View file

@ -1,12 +1,12 @@
use crate::*; use crate::*;
use self::TokenKind::*; use self::Value::*;
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct Token<'a> { #[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct Token<'a> {
source: &'a str, pub source: &'a str,
start: usize, pub start: usize,
length: usize, pub length: usize,
kind: TokenKind<'a>, pub value: Value<'a>,
} }
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub enum TokenKind<'a> { #[derive(Debug, Copy, Clone, Default, PartialEq)] pub enum Value<'a> {
#[default] Nil, #[default] Nil,
Err(ParseError), Err(ParseError),
Num(usize), Num(usize),
@ -14,24 +14,9 @@ use self::TokenKind::*;
Key(&'a str), Key(&'a str),
Exp(usize, TokenIter<'a>), Exp(usize, TokenIter<'a>),
} }
#[derive(Copy, Clone, Debug, PartialEq)] pub struct TokenIter<'a>(pub &'a str);
const_iter!(<'a>|self: TokenIter<'a>| => Token<'a> => self.next_mut().map(|(result, _)|result));
impl<'a> From<&'a str> for TokenIter<'a> {fn from (source: &'a str) -> Self{Self::new(source)}}
impl<'a> TokenIter<'a> {
pub const fn new (source: &'a str) -> Self { Self(source) }
pub const fn chomp (&self, index: usize) -> Self { Self(split_at(self.0, index).1) }
pub const fn next (mut self) -> Option<(Token<'a>, Self)> { Self::next_mut(&mut self) }
pub const fn peek (&self) -> Option<Token<'a>> { peek_src(self.0) }
pub const fn next_mut (&mut self) -> Option<(Token<'a>, Self)> {
match self.peek() {
Some(token) => Some((token, self.chomp(token.end()))),
None => None
}
}
}
impl<'a> Token<'a> { impl<'a> Token<'a> {
pub const fn new (source: &'a str, kind: TokenKind<'a>, start: usize, length: usize) -> Self { pub const fn new (source: &'a str, value: Value<'a>, start: usize, length: usize) -> Self {
Self { source, kind, start, length } Self { source, value, start, length }
} }
pub const fn end (&self) -> usize { pub const fn end (&self) -> usize {
self.start + self.length self.start + self.length
@ -43,131 +28,34 @@ impl<'a> Token<'a> {
pub const fn slice_source <'b> (&'a self, source: &'b str) -> &'b str { pub const fn slice_source <'b> (&'a self, source: &'b str) -> &'b str {
str_range(source, self.start, self.end()) str_range(source, self.start, self.end())
} }
pub const fn kind (&self) -> TokenKind { pub const fn value (&self) -> Value {
self.kind self.value
} }
pub const fn error (self, error: ParseError) -> Self { pub const fn error (self, error: ParseError) -> Self {
Self { kind: TokenKind::Err(error), ..self } Self { value: Value::Err(error), ..self }
} }
pub const fn grow (self) -> Self { pub const fn grow (self) -> Self {
Self { length: self.length + 1, ..self } Self { length: self.length + 1, ..self }
} }
pub const fn grow_in (self) -> Self { pub const fn grow_in (self) -> Self {
//Self { length: self.length + 1, depth: self.depth + 1, ..self }; match self.value {
match self.kind { Value::Exp(depth, source) => Self {
TokenKind::Exp(depth, source) => Self { value: Value::Exp(depth + 1, TokenIter(self.grow().slice_source(source.0))),
kind: TokenKind::Exp(depth + 1, TokenIter(self.grow().slice_source(source.0))),
..self.grow() ..self.grow()
}, },
_ => self.grow() _ => self.grow()
} }
} }
pub const fn grow_out (self) -> Self { pub const fn grow_out (self) -> Self {
match self.kind { match self.value {
TokenKind::Exp(depth, source) => match depth { Value::Exp(depth, source) => match depth {
0 => self.error(Unexpected(')')), 0 => self.error(Unexpected(')')),
d => Self { d => Self {
kind: TokenKind::Exp(d - 1, TokenIter(self.grow().slice_source(source.0))), value: Value::Exp(d - 1, TokenIter(self.grow().slice_source(source.0))),
..self.grow() ..self.grow()
} }
}, },
_ => self.grow() _ => self.grow()
} }
} }
//pub const fn to_ref_atom (&self, source: &'a str) -> RefAtom<'a> {
//match self.kind {
//TokenKind::Nil => Err(ParseError::Empty),
//TokenKind::Num => match to_number(self.slice_source(source)) {
//Err(e) => Err(e),
//Ok(n) => Ok(RefAtom::Num(n)),
//},
//TokenKind::Sym => Ok(RefAtom::Sym(self.slice_source(source))),
//TokenKind::Key => Ok(RefAtom::Key(self.slice_source(source))),
//TokenKind::Exp => Ok(
//RefAtom::Exp(RefAtomIterator(TokenIter::new(self.slice_source(source))))
//)
//}
//}
}
pub const fn peek_src <'a> (source: &'a str) -> Option<Token<'a>> {
let mut token: Token<'a> = Token::new(source, Nil, 0, 0);
iterate!(char_indices(source) => (start, c) => token = match token.kind() {
Err(_) => return Some(token),
Nil => match c {
' '|'\n'|'\r'|'\t' => token.grow(),
'(' => Token {
source, start, length: 1,
kind: TokenKind::Exp(1, TokenIter(str_range(source, start, start + 1))),
},
'0'..='9' => Token {
source, start, length: 1,
kind: match to_digit(c) {
Ok(c) => TokenKind::Num(c),
Result::Err(e) => TokenKind::Err(e)
}
},
':'|'@' => Token {
source, start, length: 1,
kind: TokenKind::Sym(str_range(source, start, start + 1)),
},
'/'|'a'..='z' => Token {
source, start, length: 1,
kind: TokenKind::Key(str_range(source, start, start + 1)),
},
_ => token.error(Unexpected(c))
},
Num(_) => match c {
'0'..='9' => token.grow(),
' '|'\n'|'\r'|'\t' => return Some(token),
_ => token.error(Unexpected(c))
},
Sym(_) => match c {
'a'..='z'|'0'..='9'|'-' => token.grow(),
' '|'\n'|'\r'|'\t' => return Some(token),
_ => token.error(Unexpected(c))
},
Key(_) => match c {
'a'..='z'|'0'..='9'|'-'|'/' => token.grow(),
' '|'\n'|'\r'|'\t' => return Some(token),
_ => token.error(Unexpected(c))
},
Exp(depth, _) => match depth {
0 => return Some(token),
_ => match c {
')' => token.grow_out(),
'(' => token.grow_in(),
_ => token.grow(),
}
},
});
match token.kind() {
Nil => None,
_ => Some(token),
}
}
pub const fn to_number (digits: &str) -> Result<usize, ParseError> {
let mut value = 0;
iterate!(char_indices(digits) => (_, c) => match to_digit(c) {
Ok(digit) => value = 10 * value + digit,
Result::Err(e) => return Result::Err(e)
});
Ok(value)
}
pub const fn to_digit (c: char) -> Result<usize, ParseError> {
Ok(match c {
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
'5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
_ => return Result::Err(Unexpected(c))
})
}
#[cfg(test)] #[test] fn test_token () -> Result<(), Box<dyn std::error::Error>> {
use Token::*;
assert_eq!(Nil, Token::chomp_one("")?);
assert_eq!(Nil, Token::chomp_one(" \n \r \t ")?);
assert_eq!(Num("8", 0, 1), Token::chomp_one("8")?);
assert_eq!(Num(" 8 ", 3, 1), Token::chomp_one(" 8 ")?);
assert_eq!(Sym(":foo", 0, 4), Token::chomp_one(":foo")?);
assert_eq!(Sym("@bar", 0, 4), Token::chomp_one("@bar")?);
assert_eq!(Key("foo/bar", 0, 7), Token::chomp_one("foo/bar")?);
Ok(())
} }

View file

@ -1,91 +1,38 @@
use crate::*; use crate::*;
pub trait KeyMap { /// [Input] state that can be matched against an [Atom].
/// Try to find a command that matches the currently pressed key pub trait AtomInput: Input {
fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &impl AtomInput) -> Option<C>; fn matches_atom (&self, token: &str) -> bool;
} }
//pub struct SourceKeyMap<'a>(&'a str); pub trait KeyMap {
//impl<'a> KeyMap for SourceKeyMap<'a> { /// Try to find a command that matches the current input event.
//fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &impl AtomInput) -> Option<C> { fn command <S, C: AtomCommand<S>, I: AtomInput> (&self, state: &S, input: &I) -> Option<C>;
//todo!(); }
//None impl KeyMap for TokenIter<'_> {
//} fn command <S, C: AtomCommand<S>, I: AtomInput> (&self, state: &S, input: &I) -> Option<C> {
//} for token in *self {
//pub struct ParsedKeyMap<'a>(TokensIterator<'a>); if let Value::Exp(0, iter) = token.value() {
//impl<'a> KeyMap for ParsedKeyMap<'a> { if let Some((Token { value: Value::Sym(binding), .. }, _)) = iter.next() {
//fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &impl AtomInput) -> Option<C> { if input.matches_atom(binding) {
//todo!(); if let Some(command) = C::try_from_expr(state, iter.clone()) {
//None return Some(command)
//} }
//} }
//pub struct RefKeyMap<'a>(TokensIterator<'a>); } else {
//impl<'a> KeyMap for RefKeyMap<'a> { panic!("invalid config: {token:?}")
//fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &impl AtomInput) -> Option<C> {
//todo!();
////for token in self.0 {
////match token?.kind() {
////TokenKind::Exp => match atoms.as_slice() {
////[key, command, args @ ..] => match (key.kind(), key.text()) {
////(TokenKind::Sym, key) => {
////if input.matches_atom(key) {
////let command = C::from_atom(state, command, args);
////if command.is_some() {
////return command
////}
////}
////},
////_ => panic!("invalid config: {item}")
////},
////_ => panic!("invalid config: {item}")
////}
////_ => panic!("invalid config: {item}")
////}
////}
//None
//}
//}
pub struct ArcKeyMap(Vec<ArcAtom>);
impl KeyMap for ArcKeyMap {
fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &impl AtomInput) -> Option<C> {
for atom in self.0.iter() {
match atom {
ArcAtom::Exp(atoms) => match atoms.as_slice() {
[key, command, args @ ..] => match (key.kind(), key.text()) {
(TokenKind::Sym, key) => {
if input.matches_atom(key) {
let command = C::from_atom(state, command, args);
if command.is_some() {
return command
}
}
},
_ => panic!("invalid config: {atom}")
},
_ => panic!("invalid config: {atom}")
} }
_ => panic!("invalid config: {atom}") } else {
panic!("invalid config: {token:?}")
} }
} }
None None
} }
} }
/// [Input] state that can be matched against an [Atom]. /// A [Command] that can be constructed from a [Token].
pub trait AtomInput: Input { pub trait AtomCommand<C>: TryFromAtom<C> + Command<C> {}
fn matches_atom (&self, token: &str) -> bool; impl<C, T: TryFromAtom<C> + Command<C>> AtomCommand<C> for T {}
fn get_event <'a> (_: &impl Atom) -> Option<Self::Event> {
None
}
}
/// Turns an EDN item sequence into a command enum variant.
pub trait AtomCommand<C>: Command<C> {
fn from_atom <'a> (
state: &C,
head: &impl Atom,
tail: &'a [impl Atom]
) -> Option<Self>;
}
/** Implement `AtomCommand` for given `State` and `Command` */ /** Implement `AtomCommand` for given `State` and `Command` */
#[macro_export] macro_rules! atom_command { #[macro_export] macro_rules! atom_command {
($Command:ty : |$state:ident:<$T:ident: $Trait:path>| { $(( ($Command:ty : |$state:ident:<$State:ident: $Trait:path>| { $((
// identifier // identifier
$key:literal [ $key:literal [
// named parameters // named parameters
@ -106,22 +53,22 @@ pub trait AtomCommand<C>: Command<C> {
// bound command: // bound command:
$command:expr $command:expr
))* }) => { ))* }) => {
impl<$T: $Trait> AtomCommand<$T> for $Command { impl<$State: $Trait> TryFromAtom<$State> for $Command {
fn from_atom <'a> ( fn try_from_expr ($state: &$State, iter: TokenIter) -> Option<Self> {
$state: &$T, head: &impl Atom, tail: &'a [impl Atom] match $iter.next() {
) -> Option<Self> { $(Some((Token { value: Value::Key($key), .. }, _)) => {
$(if let (TokenKind::Key, $key, [ // if the identifier matches let iter = iter.clone();
// bind argument ids $(
$($arg),* let next = iter.next();
// bind rest parameters if next.is_none() { panic!("no argument: {}", stringify!($arg)); }
$(, $rest @ ..)? let ($arg, _) = next.unwrap();
]) = (head.kind(), head.text(), tail) { $(let $arg: Option<$type> = Context::<$type>::get($state, &$arg.value);)?
$( )*
$(let $arg: Option<$type> = Context::<$type>::get($state, $arg);)? $(let $rest = iter.clone();)?
)* return Some($command)
//$(atom_command!(@bind $state => $arg $(?)? : $type);)* },)*
return Some($command) _ => None
})* }
None None
} }
} }
@ -147,25 +94,22 @@ pub trait AtomCommand<C>: Command<C> {
// bound command: // bound command:
$command:expr $command:expr
))* }) => { ))* }) => {
impl AtomCommand<$State> for $Command { impl TryFromAtom<$State> for $Command {
fn from_atom <'a> ( fn try_from_expr ($state: &$State, iter: TokenIter) -> Option<Self> {
$state: &$State, match iter.next() {
head: &impl Atom, $(Some((Token { value: Value::Key($key), .. }, _)) => {
tail: &'a [impl Atom], let iter = iter.clone();
) -> Option<Self> { $(
$(if let (TokenKind::Key, $key, [ // if the identifier matches let next = iter.next();
// bind argument ids if next.is_none() { panic!("no argument: {}", stringify!($arg)); }
$($arg),* let ($arg, _) = next.unwrap();
// bind rest parameters $(let $arg: Option<$type> = Context::<$type>::get($state, &$arg.value);)?
$(, $rest @ ..)? )*
]) = (head.kind(), head.text(), tail) { $(let $rest = iter.clone();)?
$( return Some($command)
$(let $arg: Option<$type> = Context::<$type>::get($state, $arg);)? }),*
)* _ => None
//$(atom_command!(@bind $state => $arg $(?)? : $type);)* }
return Some($command)
})*
None
} }
} }
}; };
@ -176,6 +120,71 @@ pub trait AtomCommand<C>: Command<C> {
let $arg: $type = Context::<$type>::get_or_fail($state, $arg); let $arg: $type = Context::<$type>::get_or_fail($state, $arg);
}; };
} }
//pub struct SourceKeyMap<'a>(&'a str);
//impl<'a> KeyMap for SourceKeyMap<'a> {
//fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &AtomInput) -> Option<C> {
//todo!();
//None
//}
//}
//pub struct ParsedKeyMap<'a>(TokensIterator<'a>);
//impl<'a> KeyMap for ParsedKeyMap<'a> {
//fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &AtomInput) -> Option<C> {
//todo!();
//None
//}
//}
//pub struct RefKeyMap<'a>(TokensIterator<'a>);
//impl<'a> KeyMap for RefKeyMap<'a> {
//fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &AtomInput) -> Option<C> {
//todo!();
////for token in self.0 {
////match token?.kind() {
////TokenKind::Exp => match atoms.as_slice() {
////[key, command, args @ ..] => match (key.kind(), key.text()) {
////(TokenKind::Sym, key) => {
////if input.matches_atom(key) {
////let command = C::from_atom(state, command, args);
////if command.is_some() {
////return command
////}
////}
////},
////_ => panic!("invalid config: {item}")
////},
////_ => panic!("invalid config: {item}")
////}
////_ => panic!("invalid config: {item}")
////}
////}
//None
//}
//}
//pub struct ArcKeyMap(Vec<ArcAtom>);
//impl KeyMap for ArcKeyMap {
//fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &AtomInput) -> Option<C> {
//for atom in self.0.iter() {
//match atom {
//ArcAtom::Exp(atoms) => match atoms.as_slice() {
//[key, command, args @ ..] => match (key.kind(), key.text()) {
//(TokenKind::Sym, key) => {
//if input.matches_atom(key) {
//let command = C::from_atom(state, command, args);
//if command.is_some() {
//return command
//}
//}
//},
//_ => panic!("invalid config: {atom}")
//},
//_ => panic!("invalid config: {atom}")
//}
//_ => panic!("invalid config: {atom}")
//}
//}
//None
//}
//}
#[cfg(test)] #[test] fn test_atom_keymap () -> Usually<()> { #[cfg(test)] #[test] fn test_atom_keymap () -> Usually<()> {
let keymap = KeyMap::new("")?; let keymap = KeyMap::new("")?;
Ok(()) Ok(())

View file

@ -1,4 +1,5 @@
use crate::*; use crate::*;
use Value::*;
pub trait HasEditor { pub trait HasEditor {
fn editor (&self) -> &Option<MidiEditor>; fn editor (&self) -> &Option<MidiEditor>;
fn editor_mut (&mut self) -> &Option<MidiEditor>; fn editor_mut (&mut self) -> &Option<MidiEditor>;
@ -31,6 +32,7 @@ pub trait HasEditor {
pub struct MidiEditor { pub struct MidiEditor {
pub mode: PianoHorizontal, pub mode: PianoHorizontal,
pub size: Measure<TuiOut>, pub size: Measure<TuiOut>,
keys: TokenIter<'static>
} }
has_size!(<TuiOut>|self: MidiEditor|&self.size); has_size!(<TuiOut>|self: MidiEditor|&self.size);
content!(TuiOut: |self: MidiEditor| { content!(TuiOut: |self: MidiEditor| {
@ -90,6 +92,7 @@ impl Default for MidiEditor {
Self { Self {
mode: PianoHorizontal::new(None), mode: PianoHorizontal::new(None),
size: Measure::new(), size: Measure::new(),
keys: TokenIter(KEYS_EDIT),
} }
} }
} }
@ -207,27 +210,8 @@ atom_command!(MidiEditCommand: |state: MidiEditor| {
SetTimeLock(bool), SetTimeLock(bool),
Show(Option<Arc<RwLock<MidiClip>>>), Show(Option<Arc<RwLock<MidiClip>>>),
} }
impl MidiEditCommand {
fn from_tui_event (state: &MidiEditor, input: &impl AtomInput) -> Usually<Option<Self>> {
use AtomItem::*;
let atoms = AtomItem::read_all(KEYS_EDIT)?;
for atom in atoms.iter() {
if let Exp(e) = atom {
match e.as_slice() {
[Sym(key), command, args @ ..] if input.matches_atom(key) => {
return Ok(MidiEditCommand::from_atom(state, command, args))
}
_ => {}
}
} else {
panic!("invalid config")
}
}
Ok(None)
}
}
handle!(TuiIn: |self: MidiEditor, input|{ handle!(TuiIn: |self: MidiEditor, input|{
Ok(if let Some(command) = MidiEditCommand::from_tui_event(self, input)? { Ok(if let Some(command) = self.keys.command::<_, MidiEditCommand, _>(self, input) {
let _undo = command.execute(self)?; let _undo = command.execute(self)?;
Some(true) Some(true)
} else { } else {

View file

@ -30,6 +30,11 @@ pub struct MidiPool {
pub clip: AtomicUsize, pub clip: AtomicUsize,
/// Mode switch /// Mode switch
pub mode: Option<PoolMode>, pub mode: Option<PoolMode>,
keys: TokenIter<'static>,
keys_rename: TokenIter<'static>,
keys_length: TokenIter<'static>,
keys_file: TokenIter<'static>,
} }
/// Modes for clip pool /// Modes for clip pool
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -50,6 +55,10 @@ impl Default for MidiPool {
clips: Arc::from(RwLock::from(vec![])), clips: Arc::from(RwLock::from(vec![])),
clip: 0.into(), clip: 0.into(),
mode: None, mode: None,
keys: TokenIter(KEYS_POOL),
keys_rename: TokenIter(KEYS_RENAME),
keys_length: TokenIter(KEYS_LENGTH),
keys_file: TokenIter(KEYS_FILE),
} }
} }
} }
@ -189,31 +198,13 @@ content!(TuiOut: |self: ClipLength| {
row!(" ", bars(), ".", beats(), "[", ticks()), row!(" ", bars(), ".", beats(), "[", ticks()),
} }
}); });
impl PoolCommand {
pub fn from_tui_event (state: &MidiPool, input: &impl AtomInput) -> Usually<Option<Self>> {
use AtomItem::*;
let atoms: Vec<AtomItem<_>> = AtomItem::read_all(match state.mode() {
Some(PoolMode::Rename(..)) => KEYS_RENAME,
Some(PoolMode::Length(..)) => KEYS_LENGTH,
Some(PoolMode::Import(..)) | Some(PoolMode::Export(..)) => KEYS_FILE,
_ => KEYS_POOL
})?;
for item in atoms {
match item {
Exp(e) => match e.as_slice() {
[Sym(key), command, args @ ..] if input.matches_atom(key) => {
return Ok(PoolCommand::from_atom(state, command, args))
}
_ => {}
},
_ => panic!("invalid config")
}
}
Ok(None)
}
}
handle!(TuiIn: |self: MidiPool, input|{ handle!(TuiIn: |self: MidiPool, input|{
Ok(if let Some(command) = PoolCommand::from_tui_event(self, input)? { Ok(if let Some(command) = match self.mode() {
Some(PoolMode::Rename(..)) => self.keys_rename,
Some(PoolMode::Length(..)) => self.keys_length,
Some(PoolMode::Import(..)) | Some(PoolMode::Export(..)) => self.keys_file,
_ => self.keys
}.command::<Self, PoolCommand, TuiIn>(self, input) {
let _undo = command.execute(self)?; let _undo = command.execute(self)?;
Some(true) Some(true)
} else { } else {
@ -276,11 +267,11 @@ provide!(ItemColor: |self: MidiPool| {
atom_command!(PoolCommand: |state: MidiPool| { atom_command!(PoolCommand: |state: MidiPool| {
("show" [a: bool] Self::Show(a.expect("no flag"))) ("show" [a: bool] Self::Show(a.expect("no flag")))
("select" [i: usize] Self::Select(i.expect("no index"))) ("select" [i: usize] Self::Select(i.expect("no index")))
("rename" [a, ..b] ClipRenameCommand::from_atom(state, &a.to_ref(), b).map(Self::Rename).expect("invalid command")) ("rename" [,..a] ClipRenameCommand::try_from_expr(state, a).map(Self::Rename).expect("invalid command"))
("length" [a, ..b] ClipLengthCommand::from_atom(state, &a.to_ref(), b).map(Self::Length).expect("invalid command")) ("length" [,..a] ClipLengthCommand::try_from_expr(state, a).map(Self::Length).expect("invalid command"))
("import" [a, ..b] FileBrowserCommand::from_atom(state, &a.to_ref(), b).map(Self::Import).expect("invalid command")) ("import" [,..a] FileBrowserCommand::try_from_expr(state, a).map(Self::Import).expect("invalid command"))
("export" [a, ..b] FileBrowserCommand::from_atom(state, &a.to_ref(), b).map(Self::Export).expect("invalid command")) ("export" [,..a] FileBrowserCommand::try_from_expr(state, a).map(Self::Export).expect("invalid command"))
("clip" [a, ..b] PoolClipCommand::from_atom(state, &a.to_ref(), b).map(Self::Clip).expect("invalid command")) ("clip" [,..a] PoolClipCommand::try_from_expr(state, a).map(Self::Clip).expect("invalid command"))
}); });
command!(|self: PoolCommand, state: MidiPool|{ command!(|self: PoolCommand, state: MidiPool|{
use PoolCommand::*; use PoolCommand::*;

View file

@ -1,22 +1,22 @@
use crate::*; use crate::*;
#[derive(Debug, Copy, Clone, Default)] pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W } #[derive(Debug, Copy, Clone, Default)] pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
pub struct Align<A>(Alignment, A); pub struct Align<A>(Alignment, A);
try_from_atoms!(<'a, E>: Align<RenderBox<'a, E>>: |state, atoms| { try_from_expr!(<'a, E>: Align<RenderBox<'a, E>>: |state, iter| {
let head = atoms.next()?; if let Some((Token { value: Value::Key(key), .. }, _)) = iter.next() {
if head.kind() != TokenKind::Key { return None } match key {
match head.text() { "align/c" => return Some(Self::c(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/c" => return Some(Self::c(state.get_content(&atoms.next()?).expect("no content"))), "align/x" => return Some(Self::x(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/x" => return Some(Self::x(state.get_content(&atoms.next()?).expect("no content"))), "align/y" => return Some(Self::y(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/y" => return Some(Self::y(state.get_content(&atoms.next()?).expect("no content"))), "align/n" => return Some(Self::n(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/n" => return Some(Self::n(state.get_content(&atoms.next()?).expect("no content"))), "align/s" => return Some(Self::s(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/s" => return Some(Self::s(state.get_content(&atoms.next()?).expect("no content"))), "align/e" => return Some(Self::e(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/e" => return Some(Self::e(state.get_content(&atoms.next()?).expect("no content"))), "align/w" => return Some(Self::w(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/w" => return Some(Self::w(state.get_content(&atoms.next()?).expect("no content"))), "align/nw" => return Some(Self::nw(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/nw" => return Some(Self::nw(state.get_content(&atoms.next()?).expect("no content"))), "align/ne" => return Some(Self::ne(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/ne" => return Some(Self::ne(state.get_content(&atoms.next()?).expect("no content"))), "align/sw" => return Some(Self::sw(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/sw" => return Some(Self::sw(state.get_content(&atoms.next()?).expect("no content"))), "align/se" => return Some(Self::se(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/se" => return Some(Self::se(state.get_content(&atoms.next()?).expect("no content"))), _ => {}
_ => {} }
} }
}); });
impl<A> Align<A> { impl<A> Align<A> {

View file

@ -31,35 +31,35 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
} }
} }
} }
try_from_atoms!(<'a, E>: Bsp<RenderBox<'a, E>, RenderBox<'a, E>>: |state, atoms| { try_from_expr!(<'a, E>: Bsp<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
let head = atoms.next()?; if let Some((Token { value: Value::Key(key), .. }, _)) = iter.next() {
if head.kind() != TokenKind::Key { return None } match key {
match head.text() { "bsp/n" => return Some(Self::n(
"bsp/n" => return Some(Self::n( state.get_content(&iter.next()?.0.value).expect("no south"),
state.get_content(&atoms.next()?).expect("no south"), state.get_content(&iter.next()?.0.value).expect("no north")
state.get_content(&atoms.next()?).expect("no north") )),
)), "bsp/s" => return Some(Self::s(
"bsp/s" => return Some(Self::s( state.get_content(&iter.next()?.0.value).expect("no north"),
state.get_content(&atoms.next()?).expect("no north"), state.get_content(&iter.next()?.0.value).expect("no south")
state.get_content(&atoms.next()?).expect("no south") )),
)), "bsp/e" => return Some(Self::e(
"bsp/e" => return Some(Self::e( state.get_content(&iter.next()?.0.value).expect("no west"),
state.get_content(&atoms.next()?).expect("no west"), state.get_content(&iter.next()?.0.value).expect("no east")
state.get_content(&atoms.next()?).expect("no east") )),
)), "bsp/w" => return Some(Self::w(
"bsp/w" => return Some(Self::w( state.get_content(&iter.next()?.0.value).expect("no east"),
state.get_content(&atoms.next()?).expect("no east"), state.get_content(&iter.next()?.0.value).expect("no west")
state.get_content(&atoms.next()?).expect("no west") )),
)), "bsp/a" => return Some(Self::a(
"bsp/a" => return Some(Self::a( state.get_content(&iter.next()?.0.value).expect("no above"),
state.get_content(&atoms.next()?).expect("no above"), state.get_content(&iter.next()?.0.value).expect("no below")
state.get_content(&atoms.next()?).expect("no below") )),
)), "bsp/b" => return Some(Self::b(
"bsp/b" => return Some(Self::b( state.get_content(&iter.next()?.0.value).expect("no above"),
state.get_content(&atoms.next()?).expect("no above"), state.get_content(&iter.next()?.0.value).expect("no below")
state.get_content(&atoms.next()?).expect("no below") )),
)), _ => {}
_ => {} }
} }
}); });
impl<A, B> Bsp<A, B> { impl<A, B> Bsp<A, B> {

View file

@ -5,29 +5,29 @@ impl<A> When<A> { pub fn new (c: bool, a: A) -> Self { Self(c, a) } }
/// Show one item if a condition is true and another if the condition is false /// Show one item if a condition is true and another if the condition is false
pub struct Either<A, B>(pub bool, pub A, pub B); pub struct Either<A, B>(pub bool, pub A, pub B);
impl<A, B> Either<A, B> { pub fn new (c: bool, a: A, b: B) -> Self { Self(c, a, b) } } impl<A, B> Either<A, B> { pub fn new (c: bool, a: A, b: B) -> Self { Self(c, a, b) } }
try_from_atoms!(<'a, E>: When<RenderBox<'a, E>>: |state, atoms| { try_from_expr!(<'a, E>: When<RenderBox<'a, E>>: |state, iter| {
let head = atoms.next()?; if let Some((Token { value: Value::Key("when"), .. }, _)) = iter.next() {
if (head.kind(), head.text()) == (TokenKind::Key, "when") { let iter = iter.clone();
let condition = atoms.next(); let condition = iter.next();
if let Some(ref condition) = condition { if let Some((ref condition, _)) = condition {
let condition = state.get_bool(condition).expect("no condition"); let condition = state.get_bool(&condition.value).expect("no condition");
if let Some(ref content) = atoms.next() { if let Some((ref content, _)) = iter.next() {
let content = state.get_content(content).expect("no atom"); let content = state.get_content(&content.value).expect("no atom");
return Some(Self(condition, content)) return Some(Self(condition, content))
} }
} }
} }
}); });
try_from_atoms!(<'a, E>: Either<RenderBox<'a, E>, RenderBox<'a, E>>: |state, atoms| { try_from_expr!(<'a, E>: Either<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
let head = atoms.next()?; if let Some((Token { value: Value::Key("either"), .. }, _)) = iter.next() {
if (head.kind(), head.text()) == (TokenKind::Key, "either") { let iter = iter.clone();
let condition = atoms.next(); let condition = iter.next();
if let Some(ref condition) = condition { if let Some((ref condition, _)) = condition {
let condition = state.get_bool(condition).expect("no condition"); let condition = state.get_bool(&condition.value).expect("no condition");
if let Some(ref content1) = atoms.next() { if let Some((ref content1, _)) = iter.next() {
let content1 = state.get_content(content1).expect("no content1"); let content1 = state.get_content(&content1.value).expect("no content1");
if let Some(ref content2) = atoms.next() { if let Some((ref content2, _)) = iter.next() {
let content2 = state.get_content(content2).expect("no content2"); let content2 = state.get_content(&content2.value).expect("no content2");
return Some(Self(condition, content1, content2)) return Some(Self(condition, content1, content2))
} }
} }

View file

@ -1,5 +1,4 @@
use crate::*; use crate::*;
use RefAtom::*;
pub fn map_south<O: Output>( pub fn map_south<O: Output>(
item_offset: O::Unit, item_offset: O::Unit,
item_height: O::Unit, item_height: O::Unit,

View file

@ -1,5 +1,4 @@
use crate::*; use crate::*;
use RefAtom::*;
/// Defines an enum that transforms its content /// Defines an enum that transforms its content
/// along either the X axis, the Y axis, or both. /// along either the X axis, the Y axis, or both.
/// ///
@ -13,21 +12,17 @@ macro_rules! transform_xy {
pub fn y (item: T) -> Self { Self::Y(item) } pub fn y (item: T) -> Self { Self::Y(item) }
pub fn xy (item: T) -> Self { Self::XY(item) } pub fn xy (item: T) -> Self { Self::XY(item) }
} }
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<RenderBox<'a, E>> { impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<T>
fn try_from_atoms (state: &'a T, mut atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self> { for $Enum<RenderBox<'a, E>> {
let head = atoms.next()?; fn try_from_expr (state: &T, iter: TokenIter) -> Option<Self> {
if head.kind() != TokenKind::Key { return None } Some(if let Some((Token { value: Value::Key($x), .. }, _)) = iter.next() {
Some(match head.text() { Self::x(state.get_content(&iter.next().expect("no content").0.value).expect("no content"))
$x => Self::x( } else if let Some((Token { value: Value::Key($y), .. }, _)) = iter.next() {
state.get_content(&atoms.next().expect("no content")).expect("no content") Self::y(state.get_content(&iter.next().expect("no content").0.value).expect("no content"))
), } else if let Some((Token { value: Value::Key($xy), .. }, _)) = iter.next() {
$y => Self::y( Self::xy(state.get_content(&iter.next().expect("no content").0.value).expect("no content"))
state.get_content(&atoms.next().expect("no content")).expect("no content") } else {
), return None
$xy => Self::xy(
state.get_content(&atoms.next().expect("no content")).expect("no content")
),
_ => return None
}) })
} }
} }
@ -56,25 +51,24 @@ macro_rules! transform_xy_unit {
pub fn y (y: U, item: T) -> Self { Self::Y(y, item) } pub fn y (y: U, item: T) -> Self { Self::Y(y, item) }
pub fn xy (x: U, y: U, item: T) -> Self { Self::XY(x, y, item) } pub fn xy (x: U, y: U, item: T) -> Self { Self::XY(x, y, item) }
} }
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<E::Unit, RenderBox<'a, E>> { impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<T>
fn try_from_atoms (state: &'a T, mut atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self> { for $Enum<E::Unit, RenderBox<'a, E>> {
let head = atoms.next()?; fn try_from_expr (state: &T, iter: TokenIter) -> Option<Self> {
if head.kind() != TokenKind::Key { return None } Some(if let Some((Token { value: Value::Key($x), .. }, _)) = iter.next() {
Some(match head.text() { let x = state.get_unit(&iter.next().expect("no x").0.value).expect("no x");
$x => Self::x( let c = state.get_content(&iter.next().expect("no content").0.value).expect("no content");
state.get_unit(&atoms.next().expect("no x")).expect("no x"), Self::x(x, c)
state.get_content(&atoms.next().expect("no content")).expect("no content") } else if let Some((Token { value: Value::Key($y), .. }, _)) = iter.next() {
), let y = state.get_unit(&iter.next().expect("no y").0.value).expect("no y");
$y => Self::y( let c = state.get_content(&iter.next().expect("no content").0.value).expect("no content");
state.get_unit(&atoms.next().expect("no y")).expect("no y"), Self::y(y, c)
state.get_content(&atoms.next().expect("no content")).expect("no content") } else if let Some((Token { value: Value::Key($xy), .. }, _)) = iter.next() {
), let x = state.get_unit(&iter.next().expect("no x").0.value).expect("no x");
$xy => Self::xy( let y = state.get_unit(&iter.next().expect("no y").0.value).expect("no y");
state.get_unit(&atoms.next().expect("no x")).expect("no x"), let c = state.get_content(&iter.next().expect("no content").0.value).expect("no content");
state.get_unit(&atoms.next().expect("no y")).expect("no y"), Self::xy(x, y, c)
state.get_content(&atoms.next().expect("no content")).expect("no content"), } else {
), return None
_ => return None
}) })
} }
} }

View file

@ -1,6 +1,6 @@
use crate::*; use crate::*;
use std::{sync::Arc, fmt::Debug}; use std::{sync::Arc, fmt::Debug};
use TokenKind::*; use Value::*;
/// Define an EDN-backed view. /// Define an EDN-backed view.
/// ///
/// This consists of: /// This consists of:
@ -14,8 +14,8 @@ use TokenKind::*;
fn content(&$self) -> impl Render<$Output> { $content } fn content(&$self) -> impl Render<$Output> { $content }
} }
$( $(
impl<'a> Context<'a, $type> for $App { impl Context<$type> for $App {
fn get (&'a $self, atom: &'a impl Atom) -> Option<$type> { fn get (&$self, atom: &Value) -> Option<$type> {
Some(match atom.to_ref() { $(Atom::Sym($sym) => $value,)* _ => return None }) Some(match atom.to_ref() { $(Atom::Sym($sym) => $value,)* _ => return None })
} }
} }
@ -26,7 +26,7 @@ use TokenKind::*;
#[macro_export] macro_rules! provide_content { #[macro_export] macro_rules! provide_content {
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => { (|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State { impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State {
fn get (&'a $self, atom: &'a impl Atom) -> Option<Box<dyn Render<E> + 'a>> { fn get (&$self, atom: &Value) -> Option<Box<dyn Render<E> + 'a>> {
Some(match atom.to_ref() { Some(match atom.to_ref() {
$(Atom::Sym($pat) => $expr),*, $(Atom::Sym($pat) => $expr),*,
_ => return None _ => return None
@ -36,7 +36,7 @@ use TokenKind::*;
}; };
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => { ($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State { impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State {
fn get (&'a $self, atom: &'a impl Atom) -> Option<Box<dyn Render<$Output> + 'a>> { fn get (&$self, atom: &Value) -> Option<Box<dyn Render<$Output> + 'a>> {
Some(match atom.to_ref() { Some(match atom.to_ref() {
$(Atom::Sym($pat) => $expr),*, $(Atom::Sym($pat) => $expr),*,
_ => return None _ => return None
@ -79,7 +79,7 @@ where S: Debug, A: Debug {
} }
} }
impl<'a, O, S, A> Content<O> for AtomView<'a, O, S, A> impl<'a, O, S, A> Content<O> for AtomView<'a, O, S, A>
where O: Output, S: ViewContext<'a, O>, A: Atom { where O: Output, S: ViewContext<'a, O>, A: Send + Sync {
fn content (&self) -> impl Render<O> { fn content (&self) -> impl Render<O> {
match self { match self {
Self::Inert => { panic!("inert rendered") }, Self::Inert => { panic!("inert rendered") },
@ -103,49 +103,43 @@ pub trait ViewContext<'a, E: Output>: Sized + Send + Sync
+ Context<E::Unit> + Context<E::Unit>
+ Context<Box<dyn Render<E> + 'a>> + Context<Box<dyn Render<E> + 'a>>
{ {
fn get_bool (&self, atom: &impl Atom) -> Option<bool> { fn get_bool (&self, atom: &Value) -> Option<bool> {
Some(match atom.kind() { Some(match atom {
Num => match atom.num() { 0 => false, _ => true }, Num(n) => match *n { 0 => false, _ => true },
Sym => match atom.text() { Sym(x) => match *x {
":false" | ":f" => false, ":true" | ":t" => true, ":false" | ":f" => false, ":true" | ":t" => true,
_ => return Context::get(self, atom) _ => return Context::get(self, atom)
}, },
_ => return Context::get(self, atom) _ => return Context::get(self, atom)
}) })
} }
fn get_usize (&self, atom: &impl Atom) -> Option<usize> { fn get_usize (&self, atom: &Value) -> Option<usize> {
Some(match atom.kind() { Some(match atom {Num(n) => *n, _ => return Context::get(self, atom)})
Num => atom.num(),
_ => return Context::get(self, atom)
})
} }
fn get_unit (&self, atom: &impl Atom) -> Option<E::Unit> { fn get_unit (&self, atom: &Value) -> Option<E::Unit> {
Some(match atom.kind() { Some(match atom {Num(n) => E::Unit::from(*n as u16), _ => return Context::get(self, atom)})
Num => E::Unit::from(atom.num() as u16),
_ => return Context::get(self, atom)
})
} }
fn get_content (&self, atom: &impl Atom) -> Option<Box<dyn Render<E> + 'a>> where E: 'a { fn get_content (&self, atom: &Value) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
try_delegate!(self, atom, When::<RenderBox<'a, E>>); try_delegate!(self, *atom, When::<RenderBox<'a, E>>);
try_delegate!(self, atom, Either::<RenderBox<'a, E>, RenderBox<'a, E>>); try_delegate!(self, *atom, Either::<RenderBox<'a, E>, RenderBox<'a, E>>);
try_delegate!(self, atom, Align::<RenderBox<'a, E>>); try_delegate!(self, *atom, Align::<RenderBox<'a, E>>);
try_delegate!(self, atom, Bsp::<RenderBox<'a, E>, RenderBox<'a, E>>); try_delegate!(self, *atom, Bsp::<RenderBox<'a, E>, RenderBox<'a, E>>);
try_delegate!(self, atom, Fill::<RenderBox<'a, E>>); try_delegate!(self, *atom, Fill::<RenderBox<'a, E>>);
try_delegate!(self, atom, Fixed::<_, RenderBox<'a, E>>); try_delegate!(self, *atom, Fixed::<_, RenderBox<'a, E>>);
try_delegate!(self, atom, Min::<_, RenderBox<'a, E>>); try_delegate!(self, *atom, Min::<_, RenderBox<'a, E>>);
try_delegate!(self, atom, Max::<_, RenderBox<'a, E>>); try_delegate!(self, *atom, Max::<_, RenderBox<'a, E>>);
try_delegate!(self, atom, Shrink::<_, RenderBox<'a, E>>); try_delegate!(self, *atom, Shrink::<_, RenderBox<'a, E>>);
try_delegate!(self, atom, Expand::<_, RenderBox<'a, E>>); try_delegate!(self, *atom, Expand::<_, RenderBox<'a, E>>);
try_delegate!(self, atom, Push::<_, RenderBox<'a, E>>); try_delegate!(self, *atom, Push::<_, RenderBox<'a, E>>);
try_delegate!(self, atom, Pull::<_, RenderBox<'a, E>>); try_delegate!(self, *atom, Pull::<_, RenderBox<'a, E>>);
try_delegate!(self, atom, Margin::<_, RenderBox<'a, E>>); try_delegate!(self, *atom, Margin::<_, RenderBox<'a, E>>);
try_delegate!(self, atom, Padding::<_, RenderBox<'a, E>>); try_delegate!(self, *atom, Padding::<_, RenderBox<'a, E>>);
Some(Context::get_or_fail(self, atom)) Some(Context::get_or_fail(self, atom))
} }
} }
#[macro_export] macro_rules! try_delegate { #[macro_export] macro_rules! try_delegate {
($s:ident, $atom:expr, $T:ty) => { ($s:ident, $atom:expr, $T:ty) => {
if let Some(value) = <$T>::try_from_atoms($s, $atom) { if let Some(value) = <$T>::try_from_atom($s, $atom) {
return Some(value.boxed()) return Some(value.boxed())
} }
} }
@ -157,17 +151,13 @@ pub type AtomCallback<'a, O, State> =
pub type AtomRenderCallback<'a, O, State> = pub type AtomRenderCallback<'a, O, State> =
Box<AtomCallback<'a, O, State>>; Box<AtomCallback<'a, O, State>>;
pub trait TryFromAtom<'a, T>: Sized { #[macro_export] macro_rules! try_from_expr {
fn try_from_atoms (state: &'a T, atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self>; (<$l:lifetime, $E:ident>: $Struct:ty: |$state:ident, $atoms:ident|$body:expr) => {
} impl<$l, $E: Output + $l, T: ViewContext<$l, $E>> TryFromAtom<T> for $Struct {
pub trait TryIntoAtom<'a, T>: Sized { fn try_from_atom ($state: &T, atom: Value) -> Option<Self> {
fn try_into_atoms (&self) -> Option<RefAtomsIterator<'a>>; if let Value::Exp(0, $atoms) = atom {
} $body;
#[macro_export] macro_rules! try_from_atoms { }
(<$l:lifetime, $E:ident>: $Struct:ty: |$state:ident,$atoms:ident|$body:expr) => {
impl<$l, $E: Output + $l, T: ViewContext<$l, $E>> TryFromAtom<$l, T> for $Struct {
fn try_from_atoms ($state: &$l T, mut $atoms: impl Iterator<Item = RefAtom<$l>> + $l) -> Option<Self> {
$body;
None None
} }
} }

View file

@ -257,19 +257,19 @@ fn draw_header (state: &Plugin, to: &mut TuiOut, x: u16, y: u16, w: u16) {
//} //}
//}); //});
from_atom!("plugin/lv2" => |jack: &Arc<RwLock<JackConnection>>, args| -> Plugin { //from_atom!("plugin/lv2" => |jack: &Arc<RwLock<JackConnection>>, args| -> Plugin {
let mut name = String::new(); //let mut name = String::new();
let mut path = String::new(); //let mut path = String::new();
atom!(atom in args { //atom!(atom in args {
Atom::Map(map) => { //Atom::Map(map) => {
if let Some(Atom::Str(n)) = map.get(&Atom::Key(":name")) { //if let Some(Atom::Str(n)) = map.get(&Atom::Key(":name")) {
name = String::from(*n); //name = String::from(*n);
} //}
if let Some(Atom::Str(p)) = map.get(&Atom::Key(":path")) { //if let Some(Atom::Str(p)) = map.get(&Atom::Key(":path")) {
path = String::from(*p); //path = String::from(*p);
} //}
}, //},
_ => panic!("unexpected in lv2 '{name}'"), //_ => panic!("unexpected in lv2 '{name}'"),
}); //});
Plugin::new_lv2(jack, &name, &path) //Plugin::new_lv2(jack, &name, &path)
}); //});

View file

@ -349,66 +349,66 @@ impl Sampler {
} }
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
type MidiSample = (Option<u7>, Arc<RwLock<crate::Sample>>); type MidiSample = (Option<u7>, Arc<RwLock<crate::Sample>>);
from_atom!("sampler" => |jack: &Arc<RwLock<JackConnection>>, args| -> crate::Sampler { //from_atom!("sampler" => |jack: &Arc<RwLock<JackConnection>>, args| -> crate::Sampler {
let mut name = String::new(); //let mut name = String::new();
let mut dir = String::new(); //let mut dir = String::new();
let mut samples = BTreeMap::new(); //let mut samples = BTreeMap::new();
atom!(atom in args { //atom!(atom in args {
Atom::Map(map) => { //Atom::Map(map) => {
if let Some(Atom::Str(n)) = map.get(&Atom::Key(":name")) { //if let Some(Atom::Str(n)) = map.get(&Atom::Key(":name")) {
name = String::from(*n); //name = String::from(*n);
} //}
if let Some(Atom::Str(n)) = map.get(&Atom::Key(":dir")) { //if let Some(Atom::Str(n)) = map.get(&Atom::Key(":dir")) {
dir = String::from(*n); //dir = String::from(*n);
} //}
}, //},
Atom::List(args) => match args.first() { //Atom::List(args) => match args.first() {
Some(Atom::Symbol("sample")) => { //Some(Atom::Symbol("sample")) => {
let (midi, sample) = MidiSample::from_atom((jack, &dir), &args[1..])?; //let (midi, sample) = MidiSample::from_atom((jack, &dir), &args[1..])?;
if let Some(midi) = midi { //if let Some(midi) = midi {
samples.insert(midi, sample); //samples.insert(midi, sample);
} else { //} else {
panic!("sample without midi binding: {}", sample.read().unwrap().name); //panic!("sample without midi binding: {}", sample.read().unwrap().name);
} //}
}, //},
_ => panic!("unexpected in sampler {name}: {args:?}") //_ => panic!("unexpected in sampler {name}: {args:?}")
}, //},
_ => panic!("unexpected in sampler {name}: {atom:?}") //_ => panic!("unexpected in sampler {name}: {atom:?}")
}); //});
Self::new(jack, &name) //Self::new(jack, &name)
}); //});
from_atom!("sample" => |(_jack, dir): (&Arc<RwLock<JackConnection>>, &str), args| -> MidiSample { //from_atom!("sample" => |(_jack, dir): (&Arc<RwLock<JackConnection>>, &str), args| -> MidiSample {
let mut name = String::new(); //let mut name = String::new();
let mut file = String::new(); //let mut file = String::new();
let mut midi = None; //let mut midi = None;
let mut start = 0usize; //let mut start = 0usize;
atom!(atom in args { //atom!(atom in args {
Atom::Map(map) => { //Atom::Map(map) => {
if let Some(Atom::Str(n)) = map.get(&Atom::Key(":name")) { //if let Some(Atom::Str(n)) = map.get(&Atom::Key(":name")) {
name = String::from(*n); //name = String::from(*n);
} //}
if let Some(Atom::Str(f)) = map.get(&Atom::Key(":file")) { //if let Some(Atom::Str(f)) = map.get(&Atom::Key(":file")) {
file = String::from(*f); //file = String::from(*f);
} //}
if let Some(Atom::Int(i)) = map.get(&Atom::Key(":start")) { //if let Some(Atom::Int(i)) = map.get(&Atom::Key(":start")) {
start = *i as usize; //start = *i as usize;
} //}
if let Some(Atom::Int(m)) = map.get(&Atom::Key(":midi")) { //if let Some(Atom::Int(m)) = map.get(&Atom::Key(":midi")) {
midi = Some(u7::from(*m as u8)); //midi = Some(u7::from(*m as u8));
} //}
}, //},
_ => panic!("unexpected in sample {name}"), //_ => panic!("unexpected in sample {name}"),
}); //});
let (end, data) = Sample::read_data(&format!("{dir}/{file}"))?; //let (end, data) = Sample::read_data(&format!("{dir}/{file}"))?;
Ok((midi, Arc::new(RwLock::new(crate::Sample { //Ok((midi, Arc::new(RwLock::new(crate::Sample {
name, //name,
start, //start,
end, //end,
channels: data, //channels: data,
rate: None, //rate: None,
gain: 1.0 //gain: 1.0
})))) //}))))
}); //});
impl Iterator for Voice { impl Iterator for Voice {
type Item = [f32;2]; type Item = [f32;2];
fn next (&mut self) -> Option<Self::Item> { fn next (&mut self) -> Option<Self::Item> {
@ -663,12 +663,12 @@ pub enum SamplerMode {
} }
atom_command!(SamplerTuiCommand: |state: SamplerTui| { atom_command!(SamplerTuiCommand: |state: SamplerTui| {
("select" [i: usize] Self::Select(i.expect("no index"))) ("select" [i: usize] Self::Select(i.expect("no index")))
("import" [a, ..b] if let Some(command) = FileBrowserCommand::from_atom(state, a, b) { ("import" [,..a] if let Some(command) = FileBrowserCommand::try_from_expr(state, a) {
Self::Import(command) Self::Import(command)
} else { } else {
return None return None
}) })
("sample" [a, ..b] if let Some(command) = SamplerCommand::from_atom(&state.state, a, b) { ("sample" [,..a] if let Some(command) = SamplerCommand::try_from_expr(&state.state, a) {
Self::Sample(command) Self::Sample(command)
} else { } else {
return None return None
@ -717,7 +717,7 @@ provide!(u7: |self: Sampler| {});
provide!(Option<Arc<RwLock<Sample>>>: |self: Sampler| {}); provide!(Option<Arc<RwLock<Sample>>>: |self: Sampler| {});
provide!(usize: |self: Sampler| {}); provide!(usize: |self: Sampler| {});
provide!(f32: |self: Sampler| {}); provide!(f32: |self: Sampler| {});
input_to_command!(FileBrowserCommand: |state:SamplerTui, input: Event|match input { _ => return None }); //input_to_command!(FileBrowserCommand: |state:SamplerTui, input: Event|match input { _ => return None });
command!(|self: FileBrowserCommand,state:SamplerTui|match self { _ => todo!() }); command!(|self: FileBrowserCommand,state:SamplerTui|match self { _ => todo!() });
//input_to_command!(SamplerTuiCommand: |state: SamplerTui, input: Event|match state.mode{ //input_to_command!(SamplerTuiCommand: |state: SamplerTui, input: Event|match state.mode{
//Some(SamplerMode::Import(..)) => Self::Import( //Some(SamplerMode::Import(..)) => Self::Import(

View file

@ -142,6 +142,12 @@ impl TekCli {
pub perf: PerfModel, pub perf: PerfModel,
pub editing: AtomicBool, pub editing: AtomicBool,
pub history: Vec<TekCommand>, pub history: Vec<TekCommand>,
keys: TokenIter<'static>,
keys_clip: TokenIter<'static>,
keys_track: TokenIter<'static>,
keys_scene: TokenIter<'static>,
keys_mix: TokenIter<'static>,
} }
has_size!(<TuiOut>|self: Tek|&self.size); has_size!(<TuiOut>|self: Tek|&self.size);
has_clock!(|self: Tek|self.clock); has_clock!(|self: Tek|self.clock);
@ -643,19 +649,19 @@ atom_command!(TekCommand: |app: Tek| {
(0, s) => Self::Select(Selection::Scene(s)), (0, s) => Self::Select(Selection::Scene(s)),
(t, s) => Self::Select(Selection::Clip(t, s)), (t, s) => Self::Select(Selection::Clip(t, s)),
}) })
("clip" [a, ..b] Self::Clip(ClipCommand::from_atom(app, &a.to_ref(), b) ("clip" [,..a] Self::Clip(ClipCommand::try_from_expr(app, a)
.expect("invalid command"))) .expect("invalid command")))
("clock" [a, ..b] Self::Clock(ClockCommand::from_atom(app.clock(), &a.to_ref(), b) ("clock" [,..a] Self::Clock(ClockCommand::try_from_expr(app.clock(), a)
.expect("invalid command"))) .expect("invalid command")))
("editor" [a, ..b] Self::Editor(MidiEditCommand::from_atom(app.editor.as_ref().expect("no editor"), &a.to_ref(), b) ("editor" [,..a] Self::Editor(MidiEditCommand::try_from_expr(app.editor.as_ref().expect("no editor"), a)
.expect("invalid command"))) .expect("invalid command")))
("pool" [a, ..b] Self::Pool(PoolCommand::from_atom(app.pool.as_ref().expect("no pool"), &a.to_ref(), b) ("pool" [,..a] Self::Pool(PoolCommand::try_from_expr(app.pool.as_ref().expect("no pool"), a)
.expect("invalid command"))) .expect("invalid command")))
("sampler" [a, ..b] Self::Sampler(SamplerCommand::from_atom(app.sampler.as_ref().expect("no sampler"), &a.to_ref(), b) ("sampler" [,..a] Self::Sampler(SamplerCommand::try_from_expr(app.sampler.as_ref().expect("no sampler"), a)
.expect("invalid command"))) .expect("invalid command")))
("scene" [a, ..b] Self::Scene(SceneCommand::from_atom(app, &a.to_ref(), b) ("scene" [,..a] Self::Scene(SceneCommand::try_from_expr(app, a)
.expect("invalid command"))) .expect("invalid command")))
("track" [a, ..b] Self::Track(TrackCommand::from_atom(app, &a.to_ref(), b) ("track" [,..a] Self::Track(TrackCommand::try_from_expr(app, a)
.expect("invalid command"))) .expect("invalid command")))
}); });
command!(|self: TekCommand, app: Tek|match self { command!(|self: TekCommand, app: Tek|match self {

View file

@ -54,9 +54,9 @@ impl AtomInput for TuiIn {
false false
} }
} }
fn get_event (item: &AtomItem<impl AsRef<str>>) -> Option<Event> { //fn get_event (item: &AtomItem<impl AsRef<str>>) -> Option<Event> {
match item { AtomItem::Sym(s) => KeyMatcher::new(s).build(), _ => None } //match item { AtomItem::Sym(s) => KeyMatcher::new(s).build(), _ => None }
} //}
} }
struct KeyMatcher { struct KeyMatcher {
valid: bool, valid: bool,