From aefc14734703611876cee253bb1ccc168ae4ecb6 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Fri, 2 May 2025 21:05:06 +0300 Subject: [PATCH] config: load view (and maybe name/info?) --- crates/app/src/config.rs | 71 ++++++++++++++++++++++++++++++++++++++++ crates/app/src/lib.rs | 1 + crates/app/src/model.rs | 49 --------------------------- 3 files changed, 72 insertions(+), 49 deletions(-) create mode 100644 crates/app/src/config.rs diff --git a/crates/app/src/config.rs b/crates/app/src/config.rs new file mode 100644 index 00000000..a4474710 --- /dev/null +++ b/crates/app/src/config.rs @@ -0,0 +1,71 @@ +use crate::*; + +/// Configuration +#[derive(Default, Debug)] +pub struct Configuration { + /// Name of configuration + pub name: Option>, + /// Description of configuration + pub info: Option>, + /// View definition + pub view: TokenIter<'static>, + // Input keymap + pub keys: InputMap<'static, Tek, TekCommand, TuiIn, TokenIter<'static>> +} + +impl Configuration { + pub fn from_file (path: &impl AsRef, _watch: bool) -> Usually { + Self::from_source(String::from_utf8(std::fs::read(path.as_ref())?)?) + } + pub fn from_source (source: impl AsRef + 'static) -> Usually { + let source: Box = source.as_ref().into(); + let source: &'static str = Box::leak(source); + let iter = TokenIter::from(source.as_ref()); + let mut name: Option = None; + let mut info: Option = None; + let mut view: Option = None; + let mut keys: Option = None; + for token in iter { + match token.value { + Value::Exp(_, mut exp) => { + let next = exp.next(); + match next { + Some(Token { value: Value::Key(sym), .. }) => match sym { + "name" => name = Some(exp), + "info" => info = Some(exp), + "keys" => keys = Some(exp), + "view" => view = Some(exp), + _ => return Err(format!("(e3) unexpected symbol {sym:?}").into()) + }, + _ => return Err(format!("(e2) unexpected exp {:?}", next.map(|x|x.value)).into()) + } + }, + t => return Err(format!("(e1) unexpected token {token:?}").into()) + }; + } + Ok(Self { + info: info.and_then(|mut x|x.next()).and_then(|x|if let Value::Str(x) = x.value { + Some(x.into()) + } else { + None + }), + name: name.and_then(|mut x|x.next()).and_then(|x|if let Value::Str(x) = x.value { + Some(x.into()) + } else { + None + }), + view: if let Some(view) = view { + view + } else { + return Err(format!("missing view definition").into()) + }, + keys: if let Some(keys) = keys { + let mut map = InputMap::default(); + // TODO add layers + map + } else { + return Err(format!("missing keys definition").into()) + }, + }) + } +} diff --git a/crates/app/src/lib.rs b/crates/app/src/lib.rs index e3578460..ef4e3a64 100644 --- a/crates/app/src/lib.rs +++ b/crates/app/src/lib.rs @@ -37,6 +37,7 @@ pub(crate) use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::Relaxed}; mod api; pub use self::api::*; mod audio; pub use self::audio::*; +mod config; pub use self::config::*; mod model; pub use self::model::*; mod view; pub use self::view::*; diff --git a/crates/app/src/model.rs b/crates/app/src/model.rs index 44e0a0c7..b8e6471c 100644 --- a/crates/app/src/model.rs +++ b/crates/app/src/model.rs @@ -419,55 +419,6 @@ pub trait HasSelection { fn selected_mut (&mut self) -> &mut Selection; } -/// Configuration -#[derive(Default, Debug)] -pub struct Configuration { - /// View definition - pub view: TokenIter<'static>, - // Input keymap - pub keys: InputMap<'static, Tek, TekCommand, TuiIn, TokenIter<'static>> -} - -impl Configuration { - pub fn from_file (path: &impl AsRef, _watch: bool) -> Usually { - Self::from_source(String::from_utf8(std::fs::read(path.as_ref())?)?) - } - pub fn from_source (source: impl AsRef + 'static) -> Usually { - let source: Box = source.as_ref().into(); - let source: &'static str = Box::leak(source); - let iter = TokenIter::from(source.as_ref()); - let mut view: Option = None; - let mut keys: Option = None; - for token in iter { - match token.value { - Value::Exp(_, mut exp) => match exp.next() { - Some(Token { value: Value::Sym(sym), .. }) => match sym { - "keys" => keys = Some(exp), - "view" => view = Some(exp), - _ => return Err(format!("unexpected token {token:?}").into()) - }, - _ => return Err(format!("unexpected token {token:?}").into()) - }, - t => return Err(format!("unexpected token {token:?}").into()) - }; - } - Ok(Self { - view: if let Some(view) = view { - view - } else { - return Err(format!("missing view definition").into()) - }, - keys: if let Some(keys) = keys { - let mut map = InputMap::default(); - // TODO add layers - map - } else { - return Err(format!("missing keys definition").into()) - }, - }) - } -} - /// Various possible modal overlays #[derive(PartialEq, Clone, Copy, Debug)] pub enum Modal {