EdnProvide

This commit is contained in:
🪞👃🪞 2025-01-12 13:32:11 +01:00
parent 794d4210c6
commit fc06fb863b
7 changed files with 126 additions and 61 deletions

View file

@ -1,44 +0,0 @@
use crate::*;
use EdnItem::*;
use std::marker::PhantomData;
/// Turns an EDN symbol sequence into a command enum variant.
pub trait EdnCommand<C>: Command<C> {
fn from_edn <'a> (state: &C, head: &EdnItem<&str>, tail: &'a [EdnItem<String>]) -> Self;
fn get_isize (state: &C, item: &EdnItem<&str>) -> Option<isize> {
None
}
fn get_usize (state: &C, item: &EdnItem<&str>) -> Option<usize> {
None
}
fn get_bool (state: &C, item: &EdnItem<&str>) -> Option<usize> {
None
}
}
pub trait EdnInput: Input {
fn matches (&self, token: &str) -> bool;
fn get_event <S: AsRef<str>> (_: &EdnItem<S>) -> Option<Self::Event> {
None
}
}
pub struct EdnKeymap(pub Vec<EdnItem<String>>);
impl EdnKeymap {
pub fn command <C, D: Command<C>, E: EdnCommand<C>, I: EdnInput> (&self, state: &C, input: &I) -> Option<E> {
for item in self.0.iter() {
if let Exp(items) = item {
match items.as_slice() {
[Sym(a), b, c @ ..] => if input.matches(a.as_str()) {
return Some(E::from_edn(state, &b.to_ref(), c))
},
_ => unreachable!()
}
} else {
unreachable!()
}
}
None
}
}

34
input/src/edn_command.rs Normal file
View file

@ -0,0 +1,34 @@
use crate::*;
use EdnItem::*;
#[macro_export] macro_rules! edn_command {
($Command:ty : |$state:ident:$State:ty| {
$(($key:literal [$($arg:ident : $type:ty),*] $command:expr))*
}) => {
impl EdnCommand<$State> for $Command {
fn from_edn <'a> ($state: &$State, head: &EdnItem<&str>, tail: &'a [EdnItem<String>]) -> Self {
match (head, tail) {
$((EdnItem::Key($key), [$($arg),*]) => {
$(let $arg: $type = EdnProvide::<$type>::get_or_fail($state, $arg);)*
$command
},)*
_ => panic!("no such command")
}
}
}
}
}
/// Turns an EDN symbol sequence into a command enum variant.
pub trait EdnCommand<C>: Command<C> {
fn from_edn <'a> (state: &C, head: &EdnItem<&str>, tail: &'a [EdnItem<String>]) -> Self;
fn get_isize (state: &C, item: &EdnItem<&str>) -> Option<isize> {
None
}
fn get_usize (state: &C, item: &EdnItem<&str>) -> Option<usize> {
None
}
fn get_bool (state: &C, item: &EdnItem<&str>) -> Option<usize> {
None
}
}

9
input/src/edn_input.rs Normal file
View file

@ -0,0 +1,9 @@
use crate::*;
use EdnItem::*;
pub trait EdnInput: Input {
fn matches (&self, token: &str) -> bool;
fn get_event <S: AsRef<str>> (_: &EdnItem<S>) -> Option<Self::Event> {
None
}
}

22
input/src/edn_keymap.rs Normal file
View file

@ -0,0 +1,22 @@
use crate::*;
use EdnItem::*;
pub struct EdnKeymap(pub Vec<EdnItem<String>>);
impl EdnKeymap {
pub fn command <C, D: Command<C>, E: EdnCommand<C>, I: EdnInput> (&self, state: &C, input: &I) -> Option<E> {
for item in self.0.iter() {
if let Exp(items) = item {
match items.as_slice() {
[Sym(a), b, c @ ..] => if input.matches(a.as_str()) {
return Some(E::from_edn(state, &b.to_ref(), c))
},
_ => unreachable!()
}
} else {
unreachable!()
}
}
None
}
}

25
input/src/edn_provide.rs Normal file
View file

@ -0,0 +1,25 @@
use crate::*;
/// Implement `EdnProvide` for a type and context
#[macro_export] macro_rules! edn_provide {
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl EdnProvide<$type> for $State {
fn get <S: AsRef<str>> (&$self, edn: &EdnItem<S>) -> Option<$type> {
Some(match edn.to_ref() {
$(EdnItem::Sym($pat) => $expr),*,
_ => return None
})
}
}
}
}
/// Map EDN tokens to parameters of a given type for a given context
pub trait EdnProvide<U> {
fn get <S: AsRef<str>> (&self, _edn: &EdnItem<S>) -> Option<U> {
None
}
fn get_or_fail <S: AsRef<str>> (&self, edn: &EdnItem<S>) -> U {
self.get(edn).expect("no value")
}
}

View file

@ -5,7 +5,11 @@ mod input; pub use self::input::*;
mod handle; pub use self::handle::*;
mod command; pub use self::command::*;
mod event_map; pub use self::event_map::*;
mod edn_cmd; pub use self::edn_cmd::*;
mod edn_command; pub use self::edn_command::*;
mod edn_input; pub use self::edn_input::*;
mod edn_keymap; pub use self::edn_keymap::*;
mod edn_provide; pub use self::edn_provide::*;
pub(crate) use ::tek_edn::EdnItem;
/// Standard error trait.

View file

@ -1,22 +1,37 @@
use crate::*;
use self::MidiEditCommand::*;
use KeyCode::*;
impl EdnCommand<MidiEditor> for MidiEditCommand {
fn from_edn <'a> (state: &MidiEditor, head: &EdnItem<&str>, tail: &'a [EdnItem<String>]) -> Self {
use EdnItem::*;
match (head, tail) {
(Key("note/put"), [a]) => Self::PutNote,
(Key("note/del"), [a]) => Self::AppendNote,
(Key("note/dur"), [a]) => Self::AppendNote,
(Key("note/range"), [a]) => Self::AppendNote,
(Key("note/pos"), [a]) => Self::AppendNote,
(Key("time/pos"), [a]) => Self::AppendNote,
(Key("time/zoom"), [a]) => Self::AppendNote,
(Key("time/lock"), [a]) => Self::AppendNote,
_ => todo!()
}
}
}
edn_provide!(bool: |self: MidiEditor|{
":true" => true,
":false" => false
});
edn_provide!(usize: |self: MidiEditor|{
":note-length" => self.note_len(),
":note-point" => self.note_point(),
":time-point" => self.time_point(),
":time-zoom" => self.time_zoom().get(),
});
edn_command!(MidiEditCommand: |state: MidiEditor| {
("note/put" [a: bool] Self::PutNote)
("note/del" [a: bool] Self::PutNote)
("note/dur" [a: usize] Self::SetNoteCursor(a))
});
//impl EdnCommand<MidiEditor> for MidiEditCommand {
//fn from_edn <'a> (state: &MidiEditor, head: &EdnItem<&str>, tail: &'a [EdnItem<String>]) -> Self {
//use EdnItem::*;
//match (head, tail) {
//(Key("note/put"), [a]) => Self::PutNote,
//(Key("note/del"), [a]) => Self::AppendNote,
//(Key("note/dur"), [a]) => Self::AppendNote,
//(Key("note/range"), [a]) => Self::AppendNote,
//(Key("note/pos"), [a]) => Self::AppendNote,
//(Key("time/pos"), [a]) => Self::AppendNote,
//(Key("time/zoom"), [a]) => Self::AppendNote,
//(Key("time/lock"), [a]) => Self::AppendNote,
//_ => todo!()
//}
//}
//}
#[derive(Clone, Debug)] pub enum MidiEditCommand {
// TODO: 1-9 seek markers that by default start every 8th of the clip
AppendNote,