mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 19:56:44 +01:00
This commit is contained in:
parent
b52c1f5828
commit
24ac52d807
10 changed files with 238 additions and 203 deletions
|
|
@ -14,7 +14,7 @@ type EventMapImpl<E, C> = BTreeMap<E, Vec<Binding<C>>>;
|
|||
/// When the first non-conditional or true conditional binding is executed,
|
||||
/// that .event()binding's value is returned.
|
||||
#[derive(Debug)]
|
||||
pub struct EventMap<E, C>(EventMapImpl<E, C>);
|
||||
pub struct EventMap<E, C>(pub EventMapImpl<E, C>);
|
||||
/// An input binding.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Binding<C> {
|
||||
|
|
@ -60,59 +60,63 @@ impl<E: Clone + Ord, C> EventMap<E, C> {
|
|||
.flatten()
|
||||
}
|
||||
/// Create event map from path to text file.
|
||||
pub fn from_path <P: AsRef<Path>> (path: P) -> Usually<Self> where E: From<Arc<str>> {
|
||||
pub fn load_from_path <'s> (
|
||||
&'s mut self, path: impl AsRef<Path>
|
||||
) -> Usually<&mut Self> where Self: DslInto<C> + DslInto<E> {
|
||||
if exists(path.as_ref())? {
|
||||
Self::from_source(read_to_string(path)?)
|
||||
let source = read_to_string(&path)?;
|
||||
let path: Arc<PathBuf> = Arc::new(path.as_ref().into());
|
||||
self.load_from_source(&source, &Some(&path))
|
||||
} else {
|
||||
return Err(format!("(e5) not found: {:?}", path.as_ref()).into())
|
||||
}
|
||||
}
|
||||
/// Create event map from string.
|
||||
pub fn from_source (source: impl AsRef<str>) -> Usually<Self> where E: From<Arc<str>> {
|
||||
Self::from_dsl(&mut source.as_ref())
|
||||
}
|
||||
/// Create event map from DSL tokenizer.
|
||||
pub fn from_dsl <'s, > (dsl: &'s mut impl Dsl) -> Usually<Self> where E: From<Arc<str>> {
|
||||
let mut map: Self = Default::default();
|
||||
if let Some(dsl) = dsl.exp()? {
|
||||
let mut head = dsl.head()?;
|
||||
let mut tail = 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
|
||||
}
|
||||
}
|
||||
} else if let Some(text) = dsl.text()? {
|
||||
todo!("load from file path")
|
||||
} else {
|
||||
todo!("return error for invalid input")
|
||||
}
|
||||
Ok(map)
|
||||
pub fn load_from_source (
|
||||
&mut self, dsl: impl Dsl, path: &Option<&Arc<PathBuf>>
|
||||
) -> Usually<&mut Self> where Self: DslInto<C> + DslInto<E> {
|
||||
dsl.each(|dsl|self.load_from_source_one(&dsl, path).map(move|_|()))?;
|
||||
Ok(self)
|
||||
}
|
||||
/// Load one event binding into the event map.
|
||||
pub fn load_from_source_one <'s> (
|
||||
&'s mut self, dsl: impl Dsl, path: &Option<&Arc<PathBuf>>
|
||||
) -> Usually<&mut Self> where Self: DslInto<C> + DslInto<E> {
|
||||
if let Some(exp) = dsl.head()?.exp()?
|
||||
&& let Some(sym) = exp.head()?.sym()?
|
||||
&& let Some(tail) = exp.tail()?
|
||||
{
|
||||
let event = self.dsl_into_or_else(&sym, ||panic!())?;
|
||||
let command = self.dsl_into_or_else(&tail, ||panic!())?;
|
||||
Ok(self.add(event, Binding {
|
||||
command, condition: None, description: None, source: path.cloned()
|
||||
}))
|
||||
} else {
|
||||
Err(format!("unexpected: {:?}", dsl.head()?).into())
|
||||
}
|
||||
}
|
||||
//})Ok(if let Some(sym) = dsl.head()?.exp()?.head()?.sym()? {
|
||||
//if let Some(tail) = dsl.head()?.exp()?.tail()? {
|
||||
//let event: E = sym.into();
|
||||
//let binding: Binding<C> = Binding { command: tail.into(), condition: None, description: None, source: None };
|
||||
//if let Some(bindings) = map.0.get_mut(&event) {
|
||||
//bindings.push(binding);
|
||||
//} else {
|
||||
//map.0.insert(event, vec![binding]);
|
||||
//}
|
||||
//} else {
|
||||
//panic!("empty binding: {}", dsl.head()?.exp()?.unwrap_or_default())
|
||||
//}
|
||||
//} else if let Some(ref text) = dsl.text()? {
|
||||
//map.0.extend(Self::from_path(PathBuf::from(text))?.0);
|
||||
//} else {
|
||||
//return Err(format!("unexpected: {dsl:?}").into())
|
||||
//}));
|
||||
//Ok(map)
|
||||
//}
|
||||
}
|
||||
impl<C> Binding<C> {
|
||||
fn from_dsl (dsl: impl Dsl) -> Usually<Self> {
|
||||
pub fn from_dsl (dsl: impl Dsl) -> Usually<Self> {
|
||||
let mut command: Option<C> = None;
|
||||
let mut condition: Option<Condition> = None;
|
||||
let mut description: Option<Arc<str>> = None;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue