mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
special case numeric literals, and away we go!
This commit is contained in:
parent
228b4bb47c
commit
dc45edf7e0
7 changed files with 114 additions and 133 deletions
|
|
@ -1,6 +1,15 @@
|
|||
use crate::*;
|
||||
/// Implement `EdnProvide` for a type and context
|
||||
#[macro_export] macro_rules! edn_provide {
|
||||
// Provide a value to the EDN template
|
||||
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> EdnProvide<'a, $type> for $State {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<$type> {
|
||||
Some(match edn.to_ref() { $(EdnItem::Sym($pat) => $expr,)* _ => return None })
|
||||
}
|
||||
}
|
||||
};
|
||||
// Provide a value more generically
|
||||
($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<$lt> EdnProvide<$lt, $type> for $State {
|
||||
fn get <S: AsRef<str>> (&$lt $self, edn: &$lt EdnItem<S>) -> Option<$type> {
|
||||
|
|
@ -8,10 +17,15 @@ use crate::*;
|
|||
}
|
||||
}
|
||||
};
|
||||
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
// Provide a value that may also be a numeric literal in the EDN
|
||||
(# $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> EdnProvide<'a, $type> for $State {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<$type> {
|
||||
Some(match edn.to_ref() { $(EdnItem::Sym($pat) => $expr,)* _ => return None })
|
||||
Some(match edn.to_ref() {
|
||||
$(EdnItem::Sym($pat) => $expr,)*
|
||||
EdnItem::Num(n) => n as $type,
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,58 +0,0 @@
|
|||
use crate::*;
|
||||
/// Turns an EDN item sequence into a command enum variant.
|
||||
pub trait EdnCommand<C>: Command<C> {
|
||||
fn from_edn <'a> (state: &C, head: &EdnItem<&str>, tail: &'a [EdnItem<&'a str>])
|
||||
-> Option<Self>;
|
||||
}
|
||||
/** Implement `EdnCommand` for given `State` and `Command` */
|
||||
#[macro_export] macro_rules! edn_command {
|
||||
($Command:ty : |$state:ident:$State:ty| { $((
|
||||
// identifier
|
||||
$key:literal [
|
||||
// named parameters
|
||||
$(
|
||||
// argument name
|
||||
$arg:ident
|
||||
// if type is not provided defaults to EdnItem
|
||||
$(
|
||||
// type:name separator
|
||||
:
|
||||
// argument type
|
||||
$type:ty
|
||||
)?
|
||||
),*
|
||||
// rest of parameters
|
||||
$(, ..$rest:ident)?
|
||||
]
|
||||
// bound command:
|
||||
$command:expr
|
||||
))* }) => {
|
||||
impl EdnCommand<$State> for $Command {
|
||||
fn from_edn <'a> (
|
||||
$state: &$State,
|
||||
head: &EdnItem<&str>,
|
||||
tail: &'a [EdnItem<&'a str>]
|
||||
) -> Option<Self> {
|
||||
$(if let (EdnItem::Key($key), [ // if the identifier matches
|
||||
// bind argument ids
|
||||
$($arg),*
|
||||
// bind rest parameters
|
||||
$(, $rest @ ..)?
|
||||
]) = (head, tail) {
|
||||
$(
|
||||
$(let $arg: Option<$type> = EdnProvide::<$type>::get($state, $arg);)?
|
||||
)*
|
||||
//$(edn_command!(@bind $state => $arg $(?)? : $type);)*
|
||||
return Some($command)
|
||||
})*
|
||||
None
|
||||
}
|
||||
}
|
||||
};
|
||||
(@bind $state:ident =>$arg:ident ? : $type:ty) => {
|
||||
let $arg: Option<$type> = EdnProvide::<$type>::get($state, $arg);
|
||||
};
|
||||
(@bind $state:ident => $arg:ident : $type:ty) => {
|
||||
let $arg: $type = EdnProvide::<$type>::get_or_fail($state, $arg);
|
||||
};
|
||||
}
|
||||
|
|
@ -6,3 +6,98 @@ pub trait EdnInput: Input {
|
|||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns an EDN item sequence into a command enum variant.
|
||||
pub trait EdnCommand<C>: Command<C> {
|
||||
fn from_edn <'a> (state: &C, head: &EdnItem<&str>, tail: &'a [EdnItem<&'a str>])
|
||||
-> Option<Self>;
|
||||
}
|
||||
|
||||
/** Implement `EdnCommand` for given `State` and `Command` */
|
||||
#[macro_export] macro_rules! edn_command {
|
||||
($Command:ty : |$state:ident:$State:ty| { $((
|
||||
// identifier
|
||||
$key:literal [
|
||||
// named parameters
|
||||
$(
|
||||
// argument name
|
||||
$arg:ident
|
||||
// if type is not provided defaults to EdnItem
|
||||
$(
|
||||
// type:name separator
|
||||
:
|
||||
// argument type
|
||||
$type:ty
|
||||
)?
|
||||
),*
|
||||
// rest of parameters
|
||||
$(, ..$rest:ident)?
|
||||
]
|
||||
// bound command:
|
||||
$command:expr
|
||||
))* }) => {
|
||||
impl EdnCommand<$State> for $Command {
|
||||
fn from_edn <'a> (
|
||||
$state: &$State,
|
||||
head: &EdnItem<&str>,
|
||||
tail: &'a [EdnItem<&'a str>]
|
||||
) -> Option<Self> {
|
||||
$(if let (EdnItem::Key($key), [ // if the identifier matches
|
||||
// bind argument ids
|
||||
$($arg),*
|
||||
// bind rest parameters
|
||||
$(, $rest @ ..)?
|
||||
]) = (head, tail) {
|
||||
$(
|
||||
$(let $arg: Option<$type> = EdnProvide::<$type>::get($state, $arg);)?
|
||||
)*
|
||||
//$(edn_command!(@bind $state => $arg $(?)? : $type);)*
|
||||
return Some($command)
|
||||
})*
|
||||
None
|
||||
}
|
||||
}
|
||||
};
|
||||
(@bind $state:ident =>$arg:ident ? : $type:ty) => {
|
||||
let $arg: Option<$type> = EdnProvide::<$type>::get($state, $arg);
|
||||
};
|
||||
(@bind $state:ident => $arg:ident : $type:ty) => {
|
||||
let $arg: $type = EdnProvide::<$type>::get_or_fail($state, $arg);
|
||||
};
|
||||
}
|
||||
|
||||
pub struct EdnKeyMapToCommand<'a>(Vec<EdnItem<&'a str>>);
|
||||
impl<'a> EdnKeyMapToCommand<'a> {
|
||||
/// Construct keymap from source text or fail
|
||||
pub fn new (keymap: &'a str) -> Usually<Self> {
|
||||
Ok(Self(EdnItem::<&str>::read_all(keymap)?))
|
||||
}
|
||||
/// Try to find a binding matching the currently pressed key
|
||||
pub fn from <T, C> (&self, state: &T, input: &impl EdnInput) -> Option<C>
|
||||
where
|
||||
C: Command<T> + EdnCommand<T>
|
||||
{
|
||||
use EdnItem::*;
|
||||
let mut command: Option<C> = None;
|
||||
for item in self.0.iter() {
|
||||
if let Exp(e) = item {
|
||||
match e.as_slice() {
|
||||
[Sym(key), c, args @ ..] if input.matches_edn(key) => {
|
||||
command = C::from_edn(state, c, args);
|
||||
if command.is_some() {
|
||||
break
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
} else {
|
||||
panic!("invalid config")
|
||||
}
|
||||
}
|
||||
command
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)] #[test] fn test_edn_keymap () {
|
||||
let keymap = EdnKeymapToCommand::new("")?;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,67 +0,0 @@
|
|||
use crate::*;
|
||||
use EdnItem::*;
|
||||
|
||||
pub struct EdnKeyMapToCommand<'a>(Vec<EdnItem<&'a str>>);
|
||||
impl<'a> EdnKeyMapToCommand<'a> {
|
||||
/// Construct keymap from source text or fail
|
||||
pub fn new (keymap: &'a str) -> Usually<Self> {
|
||||
Ok(Self(EdnItem::<&str>::read_all(keymap)?))
|
||||
}
|
||||
/// Try to find a binding matching the currently pressed key
|
||||
pub fn from <T, C> (&self, state: &T, input: &impl EdnInput) -> Option<C>
|
||||
where
|
||||
C: Command<T> + EdnCommand<T>
|
||||
{
|
||||
use EdnItem::*;
|
||||
let mut command: Option<C> = None;
|
||||
for item in self.0.iter() {
|
||||
if let Exp(e) = item {
|
||||
match e.as_slice() {
|
||||
[Sym(key), c, args @ ..] if input.matches_edn(key) => {
|
||||
command = C::from_edn(state, c, args);
|
||||
if command.is_some() {
|
||||
break
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
} else {
|
||||
panic!("invalid config")
|
||||
}
|
||||
}
|
||||
command
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EdnKeymap<'a>(pub Vec<EdnItem<&'a str>>);
|
||||
|
||||
impl<'a> EdnKeymap<'a> {
|
||||
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_edn(a) {
|
||||
return E::from_edn(state, &b.to_ref(), c)
|
||||
},
|
||||
_ => unreachable!()
|
||||
}
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for EdnKeymap<'a> {
|
||||
fn from (source: &'a str) -> Self {
|
||||
let items = EdnItem::<&'a str>::read_all(source.as_ref());
|
||||
Self(items.expect("failed to load keymap"))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)] #[test] fn test_edn_keymap () {
|
||||
let keymap = EdnKeymap::from("");
|
||||
}
|
||||
|
|
@ -4,10 +4,7 @@
|
|||
mod input; pub use self::input::*;
|
||||
mod command; pub use self::command::*;
|
||||
mod event_map; pub use self::event_map::*;
|
||||
|
||||
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_input; pub use self::edn_input::*;
|
||||
|
||||
pub(crate) use ::tek_edn::EdnItem;
|
||||
/// Standard error trait.
|
||||
|
|
|
|||
|
|
@ -68,9 +68,9 @@ has_editor!(|self: App|{
|
|||
editor_h = 15;
|
||||
is_editing = self.editing.load(Relaxed);
|
||||
});
|
||||
edn_provide!(# usize: |self: App| {});
|
||||
edn_view!(TuiOut: |self: App| self.size.of(EdnView::from_source(self, self.edn.as_ref())); {
|
||||
bool {};
|
||||
usize {};
|
||||
isize {};
|
||||
Color {};
|
||||
Selection {};
|
||||
|
|
|
|||
|
|
@ -139,7 +139,7 @@ macro_rules! border {
|
|||
#[derive(Copy, Clone)]
|
||||
pub struct $T(pub Style);
|
||||
impl Content<TuiOut> for $T {
|
||||
fn render (&self, to: &mut TuiOut) { self.draw(to); }
|
||||
fn render (&self, to: &mut TuiOut) { let _ = self.draw(to); }
|
||||
}
|
||||
)+}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue