From d427dc409d645c8a4307a1841be50c9d8b290247 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Fri, 2 May 2025 23:50:16 +0300 Subject: [PATCH] config: extract read_and_leak; almost done with layer-if --- crates/app/src/config.rs | 128 ++++++++++++++++++++++++++++----------- crates/app/src/lib.rs | 1 + crates/cli/tek.rs | 20 +++--- 3 files changed, 105 insertions(+), 44 deletions(-) diff --git a/crates/app/src/config.rs b/crates/app/src/config.rs index 4be40dff..ac3e1be3 100644 --- a/crates/app/src/config.rs +++ b/crates/app/src/config.rs @@ -1,8 +1,11 @@ use crate::*; +use std::path::PathBuf; /// Configuration #[derive(Default, Debug)] pub struct Configuration { + /// Path of configuration entrypoint + pub path: PathBuf, /// Name of configuration pub name: Option>, /// Description of configuration @@ -14,20 +17,19 @@ pub struct Configuration { } 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 [name, info, view, keys] = Self::parse(TokenIter::from(source.as_ref()))?; + + pub fn new (path: &impl AsRef, _watch: bool) -> Usually { + let text = read_and_leak(path.as_ref())?; + let [name, info, view, keys] = Self::parse(TokenIter::from(text))?; Ok(Self { + path: path.as_ref().into(), info: info.map(Self::parse_info).flatten(), name: name.map(Self::parse_name).flatten(), view: Self::parse_view(view)?, - keys: Self::parse_keys(keys)?, + keys: Self::parse_keys(&path, keys)?, }) } + fn parse (iter: TokenIter) -> Usually<[Option;4]> { let mut name: Option = None; let mut info: Option = None; @@ -59,6 +61,7 @@ impl Configuration { } Ok([name, info, view, keys]) } + fn parse_info (mut iter: TokenIter) -> Option> { iter.next().and_then(|x|if let Value::Str(x) = x.value { Some(x.into()) @@ -66,6 +69,7 @@ impl Configuration { None }) } + fn parse_name (mut iter: TokenIter) -> Option> { iter.next().and_then(|x|if let Value::Str(x) = x.value { Some(x.into()) @@ -73,6 +77,7 @@ impl Configuration { None }) } + fn parse_view (iter: Option) -> Usually { if let Some(view) = iter { Ok(view) @@ -80,36 +85,89 @@ impl Configuration { Err(format!("missing view definition").into()) } } - fn parse_keys (iter: Option) + + fn parse_keys (base: &impl AsRef, iter: Option>) -> Usually>> { - if let Some(mut keys) = iter { - let mut map = InputMap::default(); - while let Some(token) = keys.next() { - match token.value { - Value::Exp(_, mut exp) => { - let next = exp.next(); - match next { - Some(Token { value: Value::Key(sym), .. }) => match sym { - "layer" => { todo!() }, - "layer-if" => { todo!() }, - _ => 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(map) - } else { + if iter.is_none() { return Err(format!("missing keys definition").into()) } + let mut keys = iter.unwrap(); + let mut map = InputMap::default(); + while let Some(token) = keys.next() { + if let Value::Exp(_, mut exp) = token.value { + let next = exp.next(); + if let Some(Token { value: Value::Key(sym), .. }) = next { + match sym { + "layer" => { + let next = exp.next(); + if let Some(Token { value: Value::Str(path), .. }) = next { + let path = base.as_ref().parent().unwrap().join(unquote(path)); + if !std::fs::exists(&path)? { + return Err(format!("(e5) not found: {path:?}").into()) + } + map.add_layer(read_and_leak(path)?.into()); + } else { + return Err(format!("(e4) unexpected non-string {next:?}").into()) + } + }, + + "layer-if" => { + let mut cond = None; + + let next = exp.next(); + if let Some(Token { value: Value::Sym(sym), .. }) = next { + cond = Some(leak(sym)); + } else { + return Err(format!("(e4) unexpected non-symbol {next:?}").into()) + }; + + let next = exp.next(); + if let Some(Token { value: Value::Str(path), .. }) = next { + let path = base.as_ref().parent().unwrap().join(unquote(path)); + if !std::fs::exists(&path)? { + return Err(format!("(e5) not found: {path:?}").into()) + } + print!("{path:?}..."); + let keys = read_and_leak(path)?.into(); + println!("ok"); + let cond = cond.unwrap(); + map.add_layer_if( + Box::new(|state|{ + Context::get(state, &Value::Sym(cond)).unwrap_or(false) + }), + keys + ); + } else { + return Err(format!("(e4) unexpected non-symbol {next:?}").into()) + } + }, + + _ => return Err(format!("(e3) unexpected symbol {sym:?}").into()) + } + } else { + return Err(format!("(e2) unexpected exp {:?}", next.map(|x|x.value)).into()) + } + } else { + return Err(format!("(e1) unexpected token {token:?}").into()) + } + } + Ok(map) } + +} + +fn read_and_leak (path: impl AsRef) -> Usually<&'static str> { + Ok(leak(String::from_utf8(std::fs::read(path.as_ref())?)?)) +} + +fn leak (x: impl AsRef) -> &'static str { + Box::leak(x.as_ref().into()) +} + +fn unquote (x: &str) -> &str { + let mut chars = x.chars(); + chars.next(); + //chars.next_back(); + chars.as_str() } diff --git a/crates/app/src/lib.rs b/crates/app/src/lib.rs index ef4e3a64..fef8fb56 100644 --- a/crates/app/src/lib.rs +++ b/crates/app/src/lib.rs @@ -15,6 +15,7 @@ #![feature(trait_alias)] #![feature(type_changing_struct_update)] #![feature(let_chains)] +#![feature(closure_lifetime_binder)] /// Standard result type. pub type Usually = std::result::Result>; /// Standard optional result type. diff --git a/crates/cli/tek.rs b/crates/cli/tek.rs index 604923a9..a5990ad0 100644 --- a/crates/cli/tek.rs +++ b/crates/cli/tek.rs @@ -100,7 +100,16 @@ impl Cli { for (index, connect) in midi_tos.iter().enumerate() { let port = JackMidiOut::new(jack, &format!("{index}/M"), &[connect.clone()])?; midi_outs.push(port); - } + }; + let config_path = match mode { + LaunchMode::Clock => "config/config_transport.edn", + LaunchMode::Sequencer => "config/config_sequencer.edn", + LaunchMode::Groovebox => "config/config_groovebox.edn", + LaunchMode::Arranger { .. } => "config/config_arranger.edn", + LaunchMode::Sampler => "config/config_sampler.edn", + _ => todo!("{mode:?}"), + }; + let config = Configuration::new(&config_path, false)?; let mut app = Tek { jack: jack.clone(), color: ItemTheme::random(), @@ -139,14 +148,7 @@ impl Cli { }, scenes, selected: Selection::TrackClip { track: 0, scene: 0 }, - config: Configuration::from_file(match mode { - LaunchMode::Clock => &"config/config_transport.edn", - LaunchMode::Sequencer => &"config/config_sequencer.edn", - LaunchMode::Groovebox => &"config/config_groovebox.edn", - LaunchMode::Arranger { .. } => &"config/config_arranger.edn", - LaunchMode::Sampler => &"config/config_sampler.edn", - _ => todo!("{mode:?}"), - }, false)?, + config, ..Default::default() }; if let &LaunchMode::Arranger { scenes, tracks, track_width, .. } = mode {