mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
core, input: add flex_trait
This commit is contained in:
parent
360b404b69
commit
8cbd7dd8e8
7 changed files with 201 additions and 98 deletions
|
|
@ -54,20 +54,37 @@ impl<E: Clone + Ord, C> EventMap<E, C> {
|
|||
}
|
||||
/// Create event map from string.
|
||||
pub fn from_source (source: impl AsRef<str>) -> Usually<Self> where E: From<Arc<str>> {
|
||||
Self::from_dsl(source.as_ref())
|
||||
Self::from_dsl(&mut source.as_ref())
|
||||
}
|
||||
/// Create event map from DSL tokenizer.
|
||||
pub fn from_dsl (mut dsl: impl Dsl) -> Usually<Self> where E: From<Arc<str>> {
|
||||
pub fn from_dsl <'s, D: DslExp<'s> + 's> (dsl: &'s mut D) -> Usually<Self> where E: From<Arc<str>> {
|
||||
let mut map: Self = Default::default();
|
||||
while let Some(dsl) = dsl.exp_next()? {
|
||||
if let Some(path) = dsl.text()? {
|
||||
map.0.extend(Self::from_path(PathBuf::from(path.as_ref() as &str))?.0)
|
||||
} else if dsl.exp_head()?.key()? == Some("if") {
|
||||
todo!()
|
||||
//map.add(sym.into(), Binding::from_dsl(dsl.exp_tail())?);
|
||||
} else if let Some(sym) = dsl.exp_head()?.sym()? {
|
||||
todo!()
|
||||
//map.add(sym.into(), Binding::from_dsl(dsl.exp_tail())?);
|
||||
let mut head: Option<D> = dsl.head()?;
|
||||
let mut tail: Option<D> = dsl.tail()?;
|
||||
loop {
|
||||
if let Some(ref token) = head {
|
||||
if let Some(ref text) = token.text()? {
|
||||
map.0.extend(Self::from_path(PathBuf::from(text))?.0);
|
||||
continue
|
||||
}
|
||||
//if let Some(ref exp) = token.exp()? {
|
||||
////_ if let Some(sym) = token.head()?.sym()?.as_ref() => {
|
||||
////todo!()
|
||||
////},
|
||||
////_ if Some(&"if") == token.head()?.key()?.as_ref() => {
|
||||
////todo!()
|
||||
////},
|
||||
//return Err(format!("unexpected: {:?}", token.exp()).into())
|
||||
//}
|
||||
return Err(format!("unexpected: {token:?}").into())
|
||||
} else {
|
||||
break
|
||||
}
|
||||
if let Some(next) = tail {
|
||||
head = next.head()?;
|
||||
tail = next.tail()?;
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
Ok(map)
|
||||
|
|
|
|||
|
|
@ -35,43 +35,48 @@ pub trait Input: Sized {
|
|||
}
|
||||
}
|
||||
|
||||
/// Handle input
|
||||
pub trait Handle<E: Input> {
|
||||
flex_trait_mut!(Handle <E: Input> {
|
||||
fn handle (&mut self, _input: &E) -> Perhaps<E::Handled> {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
impl<E: Input, H: Handle<E>> Handle<E> for &mut H {
|
||||
fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
|
||||
(*self).handle(context)
|
||||
}
|
||||
}
|
||||
impl<E: Input, H: Handle<E>> Handle<E> for Option<H> {
|
||||
fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
|
||||
if let Some(handle) = self {
|
||||
handle.handle(context)
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<H, E: Input> Handle<E> for Mutex<H> where H: Handle<E> {
|
||||
fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
|
||||
self.get_mut().unwrap().handle(context)
|
||||
}
|
||||
}
|
||||
impl<H, E: Input> Handle<E> for Arc<Mutex<H>> where H: Handle<E> {
|
||||
fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
|
||||
self.lock().unwrap().handle(context)
|
||||
}
|
||||
}
|
||||
impl<H, E: Input> Handle<E> for RwLock<H> where H: Handle<E> {
|
||||
fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
|
||||
self.write().unwrap().handle(context)
|
||||
}
|
||||
}
|
||||
impl<H, E: Input> Handle<E> for Arc<RwLock<H>> where H: Handle<E> {
|
||||
fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
|
||||
self.write().unwrap().handle(context)
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//pub trait Handle<E: Input> {
|
||||
//fn handle (&mut self, _input: &E) -> Perhaps<E::Handled> {
|
||||
//Ok(None)
|
||||
//}
|
||||
//}
|
||||
//impl<E: Input, H: Handle<E>> Handle<E> for &mut H {
|
||||
//fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
|
||||
//(*self).handle(context)
|
||||
//}
|
||||
//}
|
||||
//impl<E: Input, H: Handle<E>> Handle<E> for Option<H> {
|
||||
//fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
|
||||
//if let Some(handle) = self {
|
||||
//handle.handle(context)
|
||||
//} else {
|
||||
//Ok(None)
|
||||
//}
|
||||
//}
|
||||
//}
|
||||
//impl<H, E: Input> Handle<E> for Mutex<H> where H: Handle<E> {
|
||||
//fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
|
||||
//self.get_mut().unwrap().handle(context)
|
||||
//}
|
||||
//}
|
||||
//impl<H, E: Input> Handle<E> for Arc<Mutex<H>> where H: Handle<E> {
|
||||
//fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
|
||||
//self.lock().unwrap().handle(context)
|
||||
//}
|
||||
//}
|
||||
//impl<H, E: Input> Handle<E> for RwLock<H> where H: Handle<E> {
|
||||
//fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
|
||||
//self.write().unwrap().handle(context)
|
||||
//}
|
||||
//}
|
||||
//impl<H, E: Input> Handle<E> for Arc<RwLock<H>> where H: Handle<E> {
|
||||
//fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
|
||||
//self.write().unwrap().handle(context)
|
||||
//}
|
||||
//}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue