mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
remove Atom. almost there
This commit is contained in:
parent
dc7b713108
commit
cf1fd5b45a
20 changed files with 539 additions and 739 deletions
|
|
@ -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>;
|
||||
}
|
||||
|
|
@ -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)), " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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:?}")), ","))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,26 +1,38 @@
|
|||
use crate::*;
|
||||
/// Map EDN tokens to parameters of a given type for a given context
|
||||
pub trait Context<U>: Sized {
|
||||
fn get (&self, _atom: &impl Atom) -> Option<U> {
|
||||
pub trait TryFromAtom<T>: Sized {
|
||||
fn try_from_atom (state: &T, value: Value) -> Option<Self> {
|
||||
if let Value::Exp(0, iter) = value { return Self::try_from_expr(state, iter) }
|
||||
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")
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
fn get_or_fail (&self, atom: &impl Atom) -> U {
|
||||
fn get_or_fail (&self, atom: &Value) -> U {
|
||||
(*self).get_or_fail(atom)
|
||||
}
|
||||
}
|
||||
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()
|
||||
}
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
|
@ -29,9 +41,10 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
|
|||
// Provide a value to the EDN template
|
||||
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl Context<$type> for $State {
|
||||
fn get (&$self, atom: &impl Atom) -> Option<$type> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$((TokenKind::Sym, $pat) => $expr,)*
|
||||
#[allow(unreachable_code)]
|
||||
fn get (&$self, atom: &Value) -> Option<$type> {
|
||||
Some(match atom {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
|
|
@ -40,9 +53,10 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
|
|||
// Provide a value more generically
|
||||
($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<$lt> Context<$lt, $type> for $State {
|
||||
fn get (&$lt $self, atom: &impl Atom) -> Option<$type> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$((TokenKind::Sym, $pat) => $expr,)*
|
||||
#[allow(unreachable_code)]
|
||||
fn get (&$lt $self, atom: &Value) -> Option<$type> {
|
||||
Some(match atom {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
_ => 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.
|
||||
($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<$T: $Trait> Context<$type> for $T {
|
||||
fn get (&$self, atom: &impl Atom) -> Option<$type> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$((TokenKind::Sym, $pat) => $expr,)*
|
||||
(TokenKind::Num, _) => atom.num() as $type,
|
||||
fn get (&$self, atom: &Value) -> Option<$type> {
|
||||
Some(match atom {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
Value::Num(n) => *n as $type,
|
||||
_ => 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.
|
||||
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl Context<$type> for $State {
|
||||
fn get (&$self, atom: &impl Atom) -> Option<$type> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$((TokenKind::Sym, $pat) => $expr,)*
|
||||
(TokenKind::Num, _) => atom.num() as $type,
|
||||
fn get (&$self, atom: &Value) -> Option<$type> {
|
||||
Some(match atom {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
Value::Num(n) => *n as $type,
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
|
|
@ -84,17 +98,21 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
|
|||
#[macro_export] macro_rules! provide_content {
|
||||
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State {
|
||||
fn get (&'a $self, atom: &'a impl Atom) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
use RefAtom::*;
|
||||
Some(match atom.to_ref() { $(RefAtom::Sym($pat) => $expr),*, _ => return None })
|
||||
fn get (&'a $self, atom: &'a Value) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
};
|
||||
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State {
|
||||
fn get (&'a $self, atom: &'a impl Atom) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
use RefAtom::*;
|
||||
Some(match atom.to_ref() { $(Sym($pat) => $expr),*, _ => return None })
|
||||
fn get (&'a $self, atom: &'a Value) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$(Value::Sym($pat) => $expr,)*
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
87
edn/src/iter.rs
Normal file
87
edn/src/iter.rs
Normal 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))
|
||||
})
|
||||
}
|
||||
|
|
@ -1,12 +1,14 @@
|
|||
#![feature(adt_const_params)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_fn_trait_return)]
|
||||
mod error; pub use self::error::*;
|
||||
mod token; pub use self::token::*;
|
||||
mod atom; pub use self::atom::*;
|
||||
mod atom_ref; pub use self::atom_ref::*;
|
||||
mod atom_arc; pub use self::atom_arc::*;
|
||||
mod context; pub use self::context::*;
|
||||
mod error; pub use self::error::*;
|
||||
mod token; pub use self::token::*;
|
||||
mod iter; pub use self::iter::*;
|
||||
mod context; pub use self::context::*;
|
||||
//mod atom; pub use self::atom::*;
|
||||
//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::TokenKind::*;
|
||||
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> {
|
||||
use Atom::*;
|
||||
assert_eq!(Atom::read_all("")?,
|
||||
|
|
|
|||
146
edn/src/token.rs
146
edn/src/token.rs
|
|
@ -1,12 +1,12 @@
|
|||
use crate::*;
|
||||
use self::TokenKind::*;
|
||||
use self::Value::*;
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct Token<'a> {
|
||||
source: &'a str,
|
||||
start: usize,
|
||||
length: usize,
|
||||
kind: TokenKind<'a>,
|
||||
pub source: &'a str,
|
||||
pub start: usize,
|
||||
pub length: usize,
|
||||
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,
|
||||
Err(ParseError),
|
||||
Num(usize),
|
||||
|
|
@ -14,24 +14,9 @@ use self::TokenKind::*;
|
|||
Key(&'a str),
|
||||
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> {
|
||||
pub const fn new (source: &'a str, kind: TokenKind<'a>, start: usize, length: usize) -> Self {
|
||||
Self { source, kind, start, length }
|
||||
pub const fn new (source: &'a str, value: Value<'a>, start: usize, length: usize) -> Self {
|
||||
Self { source, value, start, length }
|
||||
}
|
||||
pub const fn end (&self) -> usize {
|
||||
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 {
|
||||
str_range(source, self.start, self.end())
|
||||
}
|
||||
pub const fn kind (&self) -> TokenKind {
|
||||
self.kind
|
||||
pub const fn value (&self) -> Value {
|
||||
self.value
|
||||
}
|
||||
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 {
|
||||
Self { length: self.length + 1, ..self }
|
||||
}
|
||||
pub const fn grow_in (self) -> Self {
|
||||
//Self { length: self.length + 1, depth: self.depth + 1, ..self };
|
||||
match self.kind {
|
||||
TokenKind::Exp(depth, source) => Self {
|
||||
kind: TokenKind::Exp(depth + 1, TokenIter(self.grow().slice_source(source.0))),
|
||||
match self.value {
|
||||
Value::Exp(depth, source) => Self {
|
||||
value: Value::Exp(depth + 1, TokenIter(self.grow().slice_source(source.0))),
|
||||
..self.grow()
|
||||
},
|
||||
_ => self.grow()
|
||||
}
|
||||
}
|
||||
pub const fn grow_out (self) -> Self {
|
||||
match self.kind {
|
||||
TokenKind::Exp(depth, source) => match depth {
|
||||
match self.value {
|
||||
Value::Exp(depth, source) => match depth {
|
||||
0 => self.error(Unexpected(')')),
|
||||
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()
|
||||
}
|
||||
}
|
||||
//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(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,91 +1,38 @@
|
|||
use crate::*;
|
||||
pub trait KeyMap {
|
||||
/// Try to find a command that matches the currently pressed key
|
||||
fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &impl AtomInput) -> Option<C>;
|
||||
/// [Input] state that can be matched against an [Atom].
|
||||
pub trait AtomInput: Input {
|
||||
fn matches_atom (&self, token: &str) -> bool;
|
||||
}
|
||||
//pub struct SourceKeyMap<'a>(&'a str);
|
||||
//impl<'a> KeyMap for SourceKeyMap<'a> {
|
||||
//fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &impl 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: &impl 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: &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}")
|
||||
pub trait KeyMap {
|
||||
/// Try to find a command that matches the current input event.
|
||||
fn command <S, C: AtomCommand<S>, I: AtomInput> (&self, state: &S, input: &I) -> Option<C>;
|
||||
}
|
||||
impl KeyMap for TokenIter<'_> {
|
||||
fn command <S, C: AtomCommand<S>, I: AtomInput> (&self, state: &S, input: &I) -> Option<C> {
|
||||
for token in *self {
|
||||
if let Value::Exp(0, iter) = token.value() {
|
||||
if let Some((Token { value: Value::Sym(binding), .. }, _)) = iter.next() {
|
||||
if input.matches_atom(binding) {
|
||||
if let Some(command) = C::try_from_expr(state, iter.clone()) {
|
||||
return Some(command)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
panic!("invalid config: {token:?}")
|
||||
}
|
||||
_ => panic!("invalid config: {atom}")
|
||||
} else {
|
||||
panic!("invalid config: {token:?}")
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
/// [Input] state that can be matched against an [Atom].
|
||||
pub trait AtomInput: Input {
|
||||
fn matches_atom (&self, token: &str) -> bool;
|
||||
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>;
|
||||
}
|
||||
/// A [Command] that can be constructed from a [Token].
|
||||
pub trait AtomCommand<C>: TryFromAtom<C> + Command<C> {}
|
||||
impl<C, T: TryFromAtom<C> + Command<C>> AtomCommand<C> for T {}
|
||||
/** Implement `AtomCommand` for given `State` and `Command` */
|
||||
#[macro_export] macro_rules! atom_command {
|
||||
($Command:ty : |$state:ident:<$T:ident: $Trait:path>| { $((
|
||||
($Command:ty : |$state:ident:<$State:ident: $Trait:path>| { $((
|
||||
// identifier
|
||||
$key:literal [
|
||||
// named parameters
|
||||
|
|
@ -106,22 +53,22 @@ pub trait AtomCommand<C>: Command<C> {
|
|||
// bound command:
|
||||
$command:expr
|
||||
))* }) => {
|
||||
impl<$T: $Trait> AtomCommand<$T> for $Command {
|
||||
fn from_atom <'a> (
|
||||
$state: &$T, head: &impl Atom, tail: &'a [impl Atom]
|
||||
) -> Option<Self> {
|
||||
$(if let (TokenKind::Key, $key, [ // if the identifier matches
|
||||
// bind argument ids
|
||||
$($arg),*
|
||||
// bind rest parameters
|
||||
$(, $rest @ ..)?
|
||||
]) = (head.kind(), head.text(), tail) {
|
||||
$(
|
||||
$(let $arg: Option<$type> = Context::<$type>::get($state, $arg);)?
|
||||
)*
|
||||
//$(atom_command!(@bind $state => $arg $(?)? : $type);)*
|
||||
return Some($command)
|
||||
})*
|
||||
impl<$State: $Trait> TryFromAtom<$State> for $Command {
|
||||
fn try_from_expr ($state: &$State, iter: TokenIter) -> Option<Self> {
|
||||
match $iter.next() {
|
||||
$(Some((Token { value: Value::Key($key), .. }, _)) => {
|
||||
let iter = iter.clone();
|
||||
$(
|
||||
let next = iter.next();
|
||||
if next.is_none() { panic!("no argument: {}", stringify!($arg)); }
|
||||
let ($arg, _) = next.unwrap();
|
||||
$(let $arg: Option<$type> = Context::<$type>::get($state, &$arg.value);)?
|
||||
)*
|
||||
$(let $rest = iter.clone();)?
|
||||
return Some($command)
|
||||
},)*
|
||||
_ => None
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
@ -147,25 +94,22 @@ pub trait AtomCommand<C>: Command<C> {
|
|||
// bound command:
|
||||
$command:expr
|
||||
))* }) => {
|
||||
impl AtomCommand<$State> for $Command {
|
||||
fn from_atom <'a> (
|
||||
$state: &$State,
|
||||
head: &impl Atom,
|
||||
tail: &'a [impl Atom],
|
||||
) -> Option<Self> {
|
||||
$(if let (TokenKind::Key, $key, [ // if the identifier matches
|
||||
// bind argument ids
|
||||
$($arg),*
|
||||
// bind rest parameters
|
||||
$(, $rest @ ..)?
|
||||
]) = (head.kind(), head.text(), tail) {
|
||||
$(
|
||||
$(let $arg: Option<$type> = Context::<$type>::get($state, $arg);)?
|
||||
)*
|
||||
//$(atom_command!(@bind $state => $arg $(?)? : $type);)*
|
||||
return Some($command)
|
||||
})*
|
||||
None
|
||||
impl TryFromAtom<$State> for $Command {
|
||||
fn try_from_expr ($state: &$State, iter: TokenIter) -> Option<Self> {
|
||||
match iter.next() {
|
||||
$(Some((Token { value: Value::Key($key), .. }, _)) => {
|
||||
let iter = iter.clone();
|
||||
$(
|
||||
let next = iter.next();
|
||||
if next.is_none() { panic!("no argument: {}", stringify!($arg)); }
|
||||
let ($arg, _) = next.unwrap();
|
||||
$(let $arg: Option<$type> = Context::<$type>::get($state, &$arg.value);)?
|
||||
)*
|
||||
$(let $rest = iter.clone();)?
|
||||
return Some($command)
|
||||
}),*
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -176,6 +120,71 @@ pub trait AtomCommand<C>: Command<C> {
|
|||
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<()> {
|
||||
let keymap = KeyMap::new("")?;
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use crate::*;
|
||||
use Value::*;
|
||||
pub trait HasEditor {
|
||||
fn editor (&self) -> &Option<MidiEditor>;
|
||||
fn editor_mut (&mut self) -> &Option<MidiEditor>;
|
||||
|
|
@ -31,6 +32,7 @@ pub trait HasEditor {
|
|||
pub struct MidiEditor {
|
||||
pub mode: PianoHorizontal,
|
||||
pub size: Measure<TuiOut>,
|
||||
keys: TokenIter<'static>
|
||||
}
|
||||
has_size!(<TuiOut>|self: MidiEditor|&self.size);
|
||||
content!(TuiOut: |self: MidiEditor| {
|
||||
|
|
@ -90,6 +92,7 @@ impl Default for MidiEditor {
|
|||
Self {
|
||||
mode: PianoHorizontal::new(None),
|
||||
size: Measure::new(),
|
||||
keys: TokenIter(KEYS_EDIT),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -207,27 +210,8 @@ atom_command!(MidiEditCommand: |state: MidiEditor| {
|
|||
SetTimeLock(bool),
|
||||
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|{
|
||||
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)?;
|
||||
Some(true)
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -30,6 +30,11 @@ pub struct MidiPool {
|
|||
pub clip: AtomicUsize,
|
||||
/// Mode switch
|
||||
pub mode: Option<PoolMode>,
|
||||
|
||||
keys: TokenIter<'static>,
|
||||
keys_rename: TokenIter<'static>,
|
||||
keys_length: TokenIter<'static>,
|
||||
keys_file: TokenIter<'static>,
|
||||
}
|
||||
/// Modes for clip pool
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
@ -50,6 +55,10 @@ impl Default for MidiPool {
|
|||
clips: Arc::from(RwLock::from(vec![])),
|
||||
clip: 0.into(),
|
||||
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()),
|
||||
}
|
||||
});
|
||||
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|{
|
||||
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)?;
|
||||
Some(true)
|
||||
} else {
|
||||
|
|
@ -276,11 +267,11 @@ provide!(ItemColor: |self: MidiPool| {
|
|||
atom_command!(PoolCommand: |state: MidiPool| {
|
||||
("show" [a: bool] Self::Show(a.expect("no flag")))
|
||||
("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"))
|
||||
("length" [a, ..b] ClipLengthCommand::from_atom(state, &a.to_ref(), b).map(Self::Length).expect("invalid command"))
|
||||
("import" [a, ..b] FileBrowserCommand::from_atom(state, &a.to_ref(), b).map(Self::Import).expect("invalid command"))
|
||||
("export" [a, ..b] FileBrowserCommand::from_atom(state, &a.to_ref(), b).map(Self::Export).expect("invalid command"))
|
||||
("clip" [a, ..b] PoolClipCommand::from_atom(state, &a.to_ref(), b).map(Self::Clip).expect("invalid command"))
|
||||
("rename" [,..a] ClipRenameCommand::try_from_expr(state, a).map(Self::Rename).expect("invalid command"))
|
||||
("length" [,..a] ClipLengthCommand::try_from_expr(state, a).map(Self::Length).expect("invalid command"))
|
||||
("import" [,..a] FileBrowserCommand::try_from_expr(state, a).map(Self::Import).expect("invalid command"))
|
||||
("export" [,..a] FileBrowserCommand::try_from_expr(state, a).map(Self::Export).expect("invalid command"))
|
||||
("clip" [,..a] PoolClipCommand::try_from_expr(state, a).map(Self::Clip).expect("invalid command"))
|
||||
});
|
||||
command!(|self: PoolCommand, state: MidiPool|{
|
||||
use PoolCommand::*;
|
||||
|
|
|
|||
|
|
@ -1,22 +1,22 @@
|
|||
use crate::*;
|
||||
#[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);
|
||||
try_from_atoms!(<'a, E>: Align<RenderBox<'a, E>>: |state, atoms| {
|
||||
let head = atoms.next()?;
|
||||
if head.kind() != TokenKind::Key { return None }
|
||||
match head.text() {
|
||||
"align/c" => return Some(Self::c(state.get_content(&atoms.next()?).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(&atoms.next()?).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(&atoms.next()?).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(&atoms.next()?).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(&atoms.next()?).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(&atoms.next()?).expect("no content"))),
|
||||
_ => {}
|
||||
try_from_expr!(<'a, E>: Align<RenderBox<'a, E>>: |state, iter| {
|
||||
if let Some((Token { value: Value::Key(key), .. }, _)) = iter.next() {
|
||||
match key {
|
||||
"align/c" => return Some(Self::c(state.get_content(&iter.next()?.0.value).expect("no content"))),
|
||||
"align/x" => return Some(Self::x(state.get_content(&iter.next()?.0.value).expect("no content"))),
|
||||
"align/y" => return Some(Self::y(state.get_content(&iter.next()?.0.value).expect("no content"))),
|
||||
"align/n" => return Some(Self::n(state.get_content(&iter.next()?.0.value).expect("no content"))),
|
||||
"align/s" => return Some(Self::s(state.get_content(&iter.next()?.0.value).expect("no content"))),
|
||||
"align/e" => return Some(Self::e(state.get_content(&iter.next()?.0.value).expect("no content"))),
|
||||
"align/w" => return Some(Self::w(state.get_content(&iter.next()?.0.value).expect("no content"))),
|
||||
"align/nw" => return Some(Self::nw(state.get_content(&iter.next()?.0.value).expect("no content"))),
|
||||
"align/ne" => return Some(Self::ne(state.get_content(&iter.next()?.0.value).expect("no content"))),
|
||||
"align/sw" => return Some(Self::sw(state.get_content(&iter.next()?.0.value).expect("no content"))),
|
||||
"align/se" => return Some(Self::se(state.get_content(&iter.next()?.0.value).expect("no content"))),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
impl<A> Align<A> {
|
||||
|
|
|
|||
|
|
@ -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| {
|
||||
let head = atoms.next()?;
|
||||
if head.kind() != TokenKind::Key { return None }
|
||||
match head.text() {
|
||||
"bsp/n" => return Some(Self::n(
|
||||
state.get_content(&atoms.next()?).expect("no south"),
|
||||
state.get_content(&atoms.next()?).expect("no north")
|
||||
)),
|
||||
"bsp/s" => return Some(Self::s(
|
||||
state.get_content(&atoms.next()?).expect("no north"),
|
||||
state.get_content(&atoms.next()?).expect("no south")
|
||||
)),
|
||||
"bsp/e" => return Some(Self::e(
|
||||
state.get_content(&atoms.next()?).expect("no west"),
|
||||
state.get_content(&atoms.next()?).expect("no east")
|
||||
)),
|
||||
"bsp/w" => return Some(Self::w(
|
||||
state.get_content(&atoms.next()?).expect("no east"),
|
||||
state.get_content(&atoms.next()?).expect("no west")
|
||||
)),
|
||||
"bsp/a" => return Some(Self::a(
|
||||
state.get_content(&atoms.next()?).expect("no above"),
|
||||
state.get_content(&atoms.next()?).expect("no below")
|
||||
)),
|
||||
"bsp/b" => return Some(Self::b(
|
||||
state.get_content(&atoms.next()?).expect("no above"),
|
||||
state.get_content(&atoms.next()?).expect("no below")
|
||||
)),
|
||||
_ => {}
|
||||
try_from_expr!(<'a, E>: Bsp<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
|
||||
if let Some((Token { value: Value::Key(key), .. }, _)) = iter.next() {
|
||||
match key {
|
||||
"bsp/n" => return Some(Self::n(
|
||||
state.get_content(&iter.next()?.0.value).expect("no south"),
|
||||
state.get_content(&iter.next()?.0.value).expect("no north")
|
||||
)),
|
||||
"bsp/s" => return Some(Self::s(
|
||||
state.get_content(&iter.next()?.0.value).expect("no north"),
|
||||
state.get_content(&iter.next()?.0.value).expect("no south")
|
||||
)),
|
||||
"bsp/e" => return Some(Self::e(
|
||||
state.get_content(&iter.next()?.0.value).expect("no west"),
|
||||
state.get_content(&iter.next()?.0.value).expect("no east")
|
||||
)),
|
||||
"bsp/w" => return Some(Self::w(
|
||||
state.get_content(&iter.next()?.0.value).expect("no east"),
|
||||
state.get_content(&iter.next()?.0.value).expect("no west")
|
||||
)),
|
||||
"bsp/a" => return Some(Self::a(
|
||||
state.get_content(&iter.next()?.0.value).expect("no above"),
|
||||
state.get_content(&iter.next()?.0.value).expect("no below")
|
||||
)),
|
||||
"bsp/b" => return Some(Self::b(
|
||||
state.get_content(&iter.next()?.0.value).expect("no above"),
|
||||
state.get_content(&iter.next()?.0.value).expect("no below")
|
||||
)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
impl<A, B> Bsp<A, B> {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
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) } }
|
||||
try_from_atoms!(<'a, E>: When<RenderBox<'a, E>>: |state, atoms| {
|
||||
let head = atoms.next()?;
|
||||
if (head.kind(), head.text()) == (TokenKind::Key, "when") {
|
||||
let condition = atoms.next();
|
||||
if let Some(ref condition) = condition {
|
||||
let condition = state.get_bool(condition).expect("no condition");
|
||||
if let Some(ref content) = atoms.next() {
|
||||
let content = state.get_content(content).expect("no atom");
|
||||
try_from_expr!(<'a, E>: When<RenderBox<'a, E>>: |state, iter| {
|
||||
if let Some((Token { value: Value::Key("when"), .. }, _)) = iter.next() {
|
||||
let iter = iter.clone();
|
||||
let condition = iter.next();
|
||||
if let Some((ref condition, _)) = condition {
|
||||
let condition = state.get_bool(&condition.value).expect("no condition");
|
||||
if let Some((ref content, _)) = iter.next() {
|
||||
let content = state.get_content(&content.value).expect("no atom");
|
||||
return Some(Self(condition, content))
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
try_from_atoms!(<'a, E>: Either<RenderBox<'a, E>, RenderBox<'a, E>>: |state, atoms| {
|
||||
let head = atoms.next()?;
|
||||
if (head.kind(), head.text()) == (TokenKind::Key, "either") {
|
||||
let condition = atoms.next();
|
||||
if let Some(ref condition) = condition {
|
||||
let condition = state.get_bool(condition).expect("no condition");
|
||||
if let Some(ref content1) = atoms.next() {
|
||||
let content1 = state.get_content(content1).expect("no content1");
|
||||
if let Some(ref content2) = atoms.next() {
|
||||
let content2 = state.get_content(content2).expect("no content2");
|
||||
try_from_expr!(<'a, E>: Either<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
|
||||
if let Some((Token { value: Value::Key("either"), .. }, _)) = iter.next() {
|
||||
let iter = iter.clone();
|
||||
let condition = iter.next();
|
||||
if let Some((ref condition, _)) = condition {
|
||||
let condition = state.get_bool(&condition.value).expect("no condition");
|
||||
if let Some((ref content1, _)) = iter.next() {
|
||||
let content1 = state.get_content(&content1.value).expect("no content1");
|
||||
if let Some((ref content2, _)) = iter.next() {
|
||||
let content2 = state.get_content(&content2.value).expect("no content2");
|
||||
return Some(Self(condition, content1, content2))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
use crate::*;
|
||||
use RefAtom::*;
|
||||
pub fn map_south<O: Output>(
|
||||
item_offset: O::Unit,
|
||||
item_height: O::Unit,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
use crate::*;
|
||||
use RefAtom::*;
|
||||
/// Defines an enum that transforms its content
|
||||
/// 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 xy (item: T) -> Self { Self::XY(item) }
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<RenderBox<'a, E>> {
|
||||
fn try_from_atoms (state: &'a T, mut atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self> {
|
||||
let head = atoms.next()?;
|
||||
if head.kind() != TokenKind::Key { return None }
|
||||
Some(match head.text() {
|
||||
$x => Self::x(
|
||||
state.get_content(&atoms.next().expect("no content")).expect("no content")
|
||||
),
|
||||
$y => Self::y(
|
||||
state.get_content(&atoms.next().expect("no content")).expect("no content")
|
||||
),
|
||||
$xy => Self::xy(
|
||||
state.get_content(&atoms.next().expect("no content")).expect("no content")
|
||||
),
|
||||
_ => return None
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<T>
|
||||
for $Enum<RenderBox<'a, E>> {
|
||||
fn try_from_expr (state: &T, iter: TokenIter) -> Option<Self> {
|
||||
Some(if let Some((Token { value: Value::Key($x), .. }, _)) = iter.next() {
|
||||
Self::x(state.get_content(&iter.next().expect("no content").0.value).expect("no content"))
|
||||
} else if let Some((Token { value: Value::Key($y), .. }, _)) = iter.next() {
|
||||
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() {
|
||||
Self::xy(state.get_content(&iter.next().expect("no content").0.value).expect("no content"))
|
||||
} else {
|
||||
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 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>> {
|
||||
fn try_from_atoms (state: &'a T, mut atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self> {
|
||||
let head = atoms.next()?;
|
||||
if head.kind() != TokenKind::Key { return None }
|
||||
Some(match head.text() {
|
||||
$x => Self::x(
|
||||
state.get_unit(&atoms.next().expect("no x")).expect("no x"),
|
||||
state.get_content(&atoms.next().expect("no content")).expect("no content")
|
||||
),
|
||||
$y => Self::y(
|
||||
state.get_unit(&atoms.next().expect("no y")).expect("no y"),
|
||||
state.get_content(&atoms.next().expect("no content")).expect("no content")
|
||||
),
|
||||
$xy => Self::xy(
|
||||
state.get_unit(&atoms.next().expect("no x")).expect("no x"),
|
||||
state.get_unit(&atoms.next().expect("no y")).expect("no y"),
|
||||
state.get_content(&atoms.next().expect("no content")).expect("no content"),
|
||||
),
|
||||
_ => return None
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<T>
|
||||
for $Enum<E::Unit, RenderBox<'a, E>> {
|
||||
fn try_from_expr (state: &T, iter: TokenIter) -> Option<Self> {
|
||||
Some(if let Some((Token { value: Value::Key($x), .. }, _)) = iter.next() {
|
||||
let x = state.get_unit(&iter.next().expect("no x").0.value).expect("no x");
|
||||
let c = state.get_content(&iter.next().expect("no content").0.value).expect("no content");
|
||||
Self::x(x, c)
|
||||
} 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");
|
||||
let c = state.get_content(&iter.next().expect("no content").0.value).expect("no content");
|
||||
Self::y(y, c)
|
||||
} 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");
|
||||
let y = state.get_unit(&iter.next().expect("no y").0.value).expect("no y");
|
||||
let c = state.get_content(&iter.next().expect("no content").0.value).expect("no content");
|
||||
Self::xy(x, y, c)
|
||||
} else {
|
||||
return None
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::*;
|
||||
use std::{sync::Arc, fmt::Debug};
|
||||
use TokenKind::*;
|
||||
use Value::*;
|
||||
/// Define an EDN-backed view.
|
||||
///
|
||||
/// This consists of:
|
||||
|
|
@ -14,8 +14,8 @@ use TokenKind::*;
|
|||
fn content(&$self) -> impl Render<$Output> { $content }
|
||||
}
|
||||
$(
|
||||
impl<'a> Context<'a, $type> for $App {
|
||||
fn get (&'a $self, atom: &'a impl Atom) -> Option<$type> {
|
||||
impl Context<$type> for $App {
|
||||
fn get (&$self, atom: &Value) -> Option<$type> {
|
||||
Some(match atom.to_ref() { $(Atom::Sym($sym) => $value,)* _ => return None })
|
||||
}
|
||||
}
|
||||
|
|
@ -26,7 +26,7 @@ use TokenKind::*;
|
|||
#[macro_export] macro_rules! provide_content {
|
||||
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State {
|
||||
fn get (&'a $self, 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() {
|
||||
$(Atom::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
|
|
@ -36,7 +36,7 @@ use TokenKind::*;
|
|||
};
|
||||
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State {
|
||||
fn get (&'a $self, 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() {
|
||||
$(Atom::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
|
|
@ -79,7 +79,7 @@ where S: Debug, A: Debug {
|
|||
}
|
||||
}
|
||||
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> {
|
||||
match self {
|
||||
Self::Inert => { panic!("inert rendered") },
|
||||
|
|
@ -103,49 +103,43 @@ pub trait ViewContext<'a, E: Output>: Sized + Send + Sync
|
|||
+ Context<E::Unit>
|
||||
+ Context<Box<dyn Render<E> + 'a>>
|
||||
{
|
||||
fn get_bool (&self, atom: &impl Atom) -> Option<bool> {
|
||||
Some(match atom.kind() {
|
||||
Num => match atom.num() { 0 => false, _ => true },
|
||||
Sym => match atom.text() {
|
||||
fn get_bool (&self, atom: &Value) -> Option<bool> {
|
||||
Some(match atom {
|
||||
Num(n) => match *n { 0 => false, _ => true },
|
||||
Sym(x) => match *x {
|
||||
":false" | ":f" => false, ":true" | ":t" => true,
|
||||
_ => return Context::get(self, atom)
|
||||
},
|
||||
_ => return Context::get(self, atom)
|
||||
})
|
||||
}
|
||||
fn get_usize (&self, atom: &impl Atom) -> Option<usize> {
|
||||
Some(match atom.kind() {
|
||||
Num => atom.num(),
|
||||
_ => return Context::get(self, atom)
|
||||
})
|
||||
fn get_usize (&self, atom: &Value) -> Option<usize> {
|
||||
Some(match atom {Num(n) => *n, _ => return Context::get(self, atom)})
|
||||
}
|
||||
fn get_unit (&self, atom: &impl Atom) -> Option<E::Unit> {
|
||||
Some(match atom.kind() {
|
||||
Num => E::Unit::from(atom.num() as u16),
|
||||
_ => return Context::get(self, atom)
|
||||
})
|
||||
fn get_unit (&self, atom: &Value) -> Option<E::Unit> {
|
||||
Some(match atom {Num(n) => E::Unit::from(*n as u16), _ => return Context::get(self, atom)})
|
||||
}
|
||||
fn get_content (&self, atom: &impl Atom) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
||||
try_delegate!(self, atom, When::<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, Bsp::<RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Fill::<RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Fixed::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Min::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Max::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Shrink::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Expand::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Push::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Pull::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Margin::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Padding::<_, RenderBox<'a, E>>);
|
||||
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, Either::<RenderBox<'a, E>, 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, Fill::<RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Fixed::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Min::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Max::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Shrink::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Expand::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Push::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Pull::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Margin::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *atom, Padding::<_, RenderBox<'a, E>>);
|
||||
Some(Context::get_or_fail(self, atom))
|
||||
}
|
||||
}
|
||||
#[macro_export] macro_rules! try_delegate {
|
||||
($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())
|
||||
}
|
||||
}
|
||||
|
|
@ -157,17 +151,13 @@ pub type AtomCallback<'a, O, State> =
|
|||
pub type AtomRenderCallback<'a, O, State> =
|
||||
Box<AtomCallback<'a, O, State>>;
|
||||
|
||||
pub trait TryFromAtom<'a, T>: Sized {
|
||||
fn try_from_atoms (state: &'a T, atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self>;
|
||||
}
|
||||
pub trait TryIntoAtom<'a, T>: Sized {
|
||||
fn try_into_atoms (&self) -> Option<RefAtomsIterator<'a>>;
|
||||
}
|
||||
#[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;
|
||||
#[macro_export] macro_rules! try_from_expr {
|
||||
(<$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 {
|
||||
fn try_from_atom ($state: &T, atom: Value) -> Option<Self> {
|
||||
if let Value::Exp(0, $atoms) = atom {
|
||||
$body;
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
let mut name = String::new();
|
||||
let mut path = String::new();
|
||||
atom!(atom in args {
|
||||
Atom::Map(map) => {
|
||||
if let Some(Atom::Str(n)) = map.get(&Atom::Key(":name")) {
|
||||
name = String::from(*n);
|
||||
}
|
||||
if let Some(Atom::Str(p)) = map.get(&Atom::Key(":path")) {
|
||||
path = String::from(*p);
|
||||
}
|
||||
},
|
||||
_ => panic!("unexpected in lv2 '{name}'"),
|
||||
});
|
||||
Plugin::new_lv2(jack, &name, &path)
|
||||
});
|
||||
//from_atom!("plugin/lv2" => |jack: &Arc<RwLock<JackConnection>>, args| -> Plugin {
|
||||
//let mut name = String::new();
|
||||
//let mut path = String::new();
|
||||
//atom!(atom in args {
|
||||
//Atom::Map(map) => {
|
||||
//if let Some(Atom::Str(n)) = map.get(&Atom::Key(":name")) {
|
||||
//name = String::from(*n);
|
||||
//}
|
||||
//if let Some(Atom::Str(p)) = map.get(&Atom::Key(":path")) {
|
||||
//path = String::from(*p);
|
||||
//}
|
||||
//},
|
||||
//_ => panic!("unexpected in lv2 '{name}'"),
|
||||
//});
|
||||
//Plugin::new_lv2(jack, &name, &path)
|
||||
//});
|
||||
|
|
|
|||
|
|
@ -349,66 +349,66 @@ impl Sampler {
|
|||
}
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
type MidiSample = (Option<u7>, Arc<RwLock<crate::Sample>>);
|
||||
from_atom!("sampler" => |jack: &Arc<RwLock<JackConnection>>, args| -> crate::Sampler {
|
||||
let mut name = String::new();
|
||||
let mut dir = String::new();
|
||||
let mut samples = BTreeMap::new();
|
||||
atom!(atom in args {
|
||||
Atom::Map(map) => {
|
||||
if let Some(Atom::Str(n)) = map.get(&Atom::Key(":name")) {
|
||||
name = String::from(*n);
|
||||
}
|
||||
if let Some(Atom::Str(n)) = map.get(&Atom::Key(":dir")) {
|
||||
dir = String::from(*n);
|
||||
}
|
||||
},
|
||||
Atom::List(args) => match args.first() {
|
||||
Some(Atom::Symbol("sample")) => {
|
||||
let (midi, sample) = MidiSample::from_atom((jack, &dir), &args[1..])?;
|
||||
if let Some(midi) = midi {
|
||||
samples.insert(midi, sample);
|
||||
} else {
|
||||
panic!("sample without midi binding: {}", sample.read().unwrap().name);
|
||||
}
|
||||
},
|
||||
_ => panic!("unexpected in sampler {name}: {args:?}")
|
||||
},
|
||||
_ => panic!("unexpected in sampler {name}: {atom:?}")
|
||||
});
|
||||
Self::new(jack, &name)
|
||||
});
|
||||
from_atom!("sample" => |(_jack, dir): (&Arc<RwLock<JackConnection>>, &str), args| -> MidiSample {
|
||||
let mut name = String::new();
|
||||
let mut file = String::new();
|
||||
let mut midi = None;
|
||||
let mut start = 0usize;
|
||||
atom!(atom in args {
|
||||
Atom::Map(map) => {
|
||||
if let Some(Atom::Str(n)) = map.get(&Atom::Key(":name")) {
|
||||
name = String::from(*n);
|
||||
}
|
||||
if let Some(Atom::Str(f)) = map.get(&Atom::Key(":file")) {
|
||||
file = String::from(*f);
|
||||
}
|
||||
if let Some(Atom::Int(i)) = map.get(&Atom::Key(":start")) {
|
||||
start = *i as usize;
|
||||
}
|
||||
if let Some(Atom::Int(m)) = map.get(&Atom::Key(":midi")) {
|
||||
midi = Some(u7::from(*m as u8));
|
||||
}
|
||||
},
|
||||
_ => panic!("unexpected in sample {name}"),
|
||||
});
|
||||
let (end, data) = Sample::read_data(&format!("{dir}/{file}"))?;
|
||||
Ok((midi, Arc::new(RwLock::new(crate::Sample {
|
||||
name,
|
||||
start,
|
||||
end,
|
||||
channels: data,
|
||||
rate: None,
|
||||
gain: 1.0
|
||||
}))))
|
||||
});
|
||||
//from_atom!("sampler" => |jack: &Arc<RwLock<JackConnection>>, args| -> crate::Sampler {
|
||||
//let mut name = String::new();
|
||||
//let mut dir = String::new();
|
||||
//let mut samples = BTreeMap::new();
|
||||
//atom!(atom in args {
|
||||
//Atom::Map(map) => {
|
||||
//if let Some(Atom::Str(n)) = map.get(&Atom::Key(":name")) {
|
||||
//name = String::from(*n);
|
||||
//}
|
||||
//if let Some(Atom::Str(n)) = map.get(&Atom::Key(":dir")) {
|
||||
//dir = String::from(*n);
|
||||
//}
|
||||
//},
|
||||
//Atom::List(args) => match args.first() {
|
||||
//Some(Atom::Symbol("sample")) => {
|
||||
//let (midi, sample) = MidiSample::from_atom((jack, &dir), &args[1..])?;
|
||||
//if let Some(midi) = midi {
|
||||
//samples.insert(midi, sample);
|
||||
//} else {
|
||||
//panic!("sample without midi binding: {}", sample.read().unwrap().name);
|
||||
//}
|
||||
//},
|
||||
//_ => panic!("unexpected in sampler {name}: {args:?}")
|
||||
//},
|
||||
//_ => panic!("unexpected in sampler {name}: {atom:?}")
|
||||
//});
|
||||
//Self::new(jack, &name)
|
||||
//});
|
||||
//from_atom!("sample" => |(_jack, dir): (&Arc<RwLock<JackConnection>>, &str), args| -> MidiSample {
|
||||
//let mut name = String::new();
|
||||
//let mut file = String::new();
|
||||
//let mut midi = None;
|
||||
//let mut start = 0usize;
|
||||
//atom!(atom in args {
|
||||
//Atom::Map(map) => {
|
||||
//if let Some(Atom::Str(n)) = map.get(&Atom::Key(":name")) {
|
||||
//name = String::from(*n);
|
||||
//}
|
||||
//if let Some(Atom::Str(f)) = map.get(&Atom::Key(":file")) {
|
||||
//file = String::from(*f);
|
||||
//}
|
||||
//if let Some(Atom::Int(i)) = map.get(&Atom::Key(":start")) {
|
||||
//start = *i as usize;
|
||||
//}
|
||||
//if let Some(Atom::Int(m)) = map.get(&Atom::Key(":midi")) {
|
||||
//midi = Some(u7::from(*m as u8));
|
||||
//}
|
||||
//},
|
||||
//_ => panic!("unexpected in sample {name}"),
|
||||
//});
|
||||
//let (end, data) = Sample::read_data(&format!("{dir}/{file}"))?;
|
||||
//Ok((midi, Arc::new(RwLock::new(crate::Sample {
|
||||
//name,
|
||||
//start,
|
||||
//end,
|
||||
//channels: data,
|
||||
//rate: None,
|
||||
//gain: 1.0
|
||||
//}))))
|
||||
//});
|
||||
impl Iterator for Voice {
|
||||
type Item = [f32;2];
|
||||
fn next (&mut self) -> Option<Self::Item> {
|
||||
|
|
@ -663,12 +663,12 @@ pub enum SamplerMode {
|
|||
}
|
||||
atom_command!(SamplerTuiCommand: |state: SamplerTui| {
|
||||
("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)
|
||||
} else {
|
||||
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)
|
||||
} else {
|
||||
return None
|
||||
|
|
@ -717,7 +717,7 @@ provide!(u7: |self: Sampler| {});
|
|||
provide!(Option<Arc<RwLock<Sample>>>: |self: Sampler| {});
|
||||
provide!(usize: |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!() });
|
||||
//input_to_command!(SamplerTuiCommand: |state: SamplerTui, input: Event|match state.mode{
|
||||
//Some(SamplerMode::Import(..)) => Self::Import(
|
||||
|
|
|
|||
|
|
@ -142,6 +142,12 @@ impl TekCli {
|
|||
pub perf: PerfModel,
|
||||
pub editing: AtomicBool,
|
||||
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_clock!(|self: Tek|self.clock);
|
||||
|
|
@ -643,19 +649,19 @@ atom_command!(TekCommand: |app: Tek| {
|
|||
(0, s) => Self::Select(Selection::Scene(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")))
|
||||
("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")))
|
||||
("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")))
|
||||
("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")))
|
||||
("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")))
|
||||
("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")))
|
||||
("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")))
|
||||
});
|
||||
command!(|self: TekCommand, app: Tek|match self {
|
||||
|
|
|
|||
|
|
@ -54,9 +54,9 @@ impl AtomInput for TuiIn {
|
|||
false
|
||||
}
|
||||
}
|
||||
fn get_event (item: &AtomItem<impl AsRef<str>>) -> Option<Event> {
|
||||
match item { AtomItem::Sym(s) => KeyMatcher::new(s).build(), _ => None }
|
||||
}
|
||||
//fn get_event (item: &AtomItem<impl AsRef<str>>) -> Option<Event> {
|
||||
//match item { AtomItem::Sym(s) => KeyMatcher::new(s).build(), _ => None }
|
||||
//}
|
||||
}
|
||||
struct KeyMatcher {
|
||||
valid: bool,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue