From 7ce33d980e42fac0a2861fee74daa93d0d4b822c Mon Sep 17 00:00:00 2001 From: Evan Battaglia Date: Sat, 14 Sep 2024 16:57:16 -0230 Subject: [PATCH] Revert unified_includes stuff I forgot that the reason I have arg/sub/flag includes like that is so "include"s can order properly with other flags, args, and subs. e.g. arg foo include @waz arg bar Is different than arg foo arg bar include @waz --- TODO.md | 1 - src/core/config.rs | 41 ++++----------------- src/core/types.rs | 4 -- src/engine/machine.rs | 4 +- src/engine/options_finder.rs | 14 +++---- src/lang/compiler.rs | 31 ++++++++++++---- tmp.json | 71 ------------------------------------ 7 files changed, 39 insertions(+), 127 deletions(-) delete mode 100644 tmp.json diff --git a/TODO.md b/TODO.md index 893b133..4ed47b7 100644 --- a/TODO.md +++ b/TODO.md @@ -5,7 +5,6 @@ TODO before "public announcement" * put on cargo * more examples / language reference in this repo. delegate "super aliases" - -> but delegate not working in fish, and not in all cases in bash (works for docker, not for git) * bump version number and add deb * better instructions for installing (build from source, cargo, nix, deb) diff --git a/src/core/config.rs b/src/core/config.rs index c284ae3..57161c3 100644 --- a/src/core/config.rs +++ b/src/core/config.rs @@ -60,9 +60,9 @@ impl TabryConf { let mut result = vec![&self.main]; for name in sub_names_vec { - let sub = result.last().unwrap(); + let subs_here = &result.last().unwrap().subs; let next = - self.find_in_subs(&sub.subs, &sub.includes, name, false)? + self.find_in_subs(subs_here, name, false)? .ok_or(TabryConfError::InternalError( "sub not found in dig sub".to_owned(), ))?; @@ -75,11 +75,10 @@ impl TabryConf { pub fn find_in_subs<'a>( &'a self, subs: &'a [TabrySub], - includes: &'a Vec, name: &String, check_aliases: bool, ) -> Result, TabryConfError> { - let concrete_subs: Vec<&TabryConcreteSub> = self.flatten_subs(subs, includes)?; + let concrete_subs: Vec<&TabryConcreteSub> = self.flatten_subs(subs)?; for sub in concrete_subs { let sub_name = Self::unwrap_sub_name(sub)?; @@ -116,16 +115,15 @@ impl TabryConf { pub fn flatten_subs<'a>( &'a self, subs: &'a [TabrySub], - includes: &'a Vec, ) -> Result, TabryConfError> { - let mut vecofvecs = subs + let vecofvecs = subs .iter() .map(|sub| match sub { TabrySub::TabryIncludeSub { include } => { // Lookup include, which may return an error let inc = self.get_arg_include(include)?; // Flatten the include's subs recursively (which may return an error) - self.flatten_subs(&inc.subs, &inc.includes) + self.flatten_subs(&inc.subs) } TabrySub::TabryConcreteSub(s) => // This is a concrete sub, add it @@ -135,15 +133,6 @@ impl TabryConf { }) .collect::, _>>()?; - vecofvecs.extend( - includes.iter().map(|include| { - // Lookup include, which may return an error - let inc = self.get_arg_include(include)?; - // Flatten the include's subs recursively (which may return an error) - self.flatten_subs(&inc.subs, &inc.includes) - }).collect::, _>>()?, - ); - // collect() will return an error if there were one, so now we just have flatten the // vectors Ok(vecofvecs.into_iter().flatten().collect::>()) @@ -154,24 +143,15 @@ impl TabryConf { pub fn expand_flags<'a>( &'a self, flags: &'a [TabryFlag], - includes: &'a [String], ) -> Box + 'a> { let iter = flags.iter().flat_map(|flag| match flag { TabryFlag::TabryIncludeFlag { include } => { // TODO: bubble up error instead of unwrap (use get_arg_include) let include = self.arg_includes.get(include).unwrap(); - self.expand_flags(&include.flags, &include.includes) + self.expand_flags(&include.flags) } TabryFlag::TabryConcreteFlag(concrete_flag) => Box::new(std::iter::once(concrete_flag)), }); - let iter = iter.chain( - includes.iter().flat_map(move |include| { - // TODO: bubble up error instead of unwrap - let inc = self.arg_includes.get(include).unwrap(); - self.expand_flags(&inc.flags, &inc.includes) - }), - ); - Box::new(iter) } @@ -179,22 +159,15 @@ impl TabryConf { pub fn expand_args<'a>( &'a self, args: &'a [TabryArg], - includes: &'a [String], ) -> Box + 'a> { let iter = args.iter().flat_map(|arg| match arg { TabryArg::TabryIncludeArg { include } => { // TODO: bubble up error instead of unwrap (use get_arg_include) let include = self.arg_includes.get(include).unwrap(); - self.expand_args(&include.args, &include.includes) + self.expand_args(&include.args) } TabryArg::TabryConcreteArg(concrete_arg) => Box::new(std::iter::once(concrete_arg)), }); - let iter = iter.chain( - includes.iter().flat_map(move |include| { - let inc = self.arg_includes.get(include).unwrap(); - self.expand_args(&inc.args, &inc.includes) - }), - ); Box::new(iter) } } diff --git a/src/core/types.rs b/src/core/types.rs index bf64fb1..e03aa96 100644 --- a/src/core/types.rs +++ b/src/core/types.rs @@ -70,8 +70,6 @@ pub struct TabryConcreteSub { pub flags: Vec, #[serde(default)] pub subs: Vec, - #[serde(default)] - pub includes: Vec, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -89,6 +87,4 @@ pub struct TabryArgInclude { pub flags: Vec, #[serde(default)] pub subs: Vec, - #[serde(default)] - pub includes: Vec, } diff --git a/src/engine/machine.rs b/src/engine/machine.rs index d19cb54..0382b4c 100644 --- a/src/engine/machine.rs +++ b/src/engine/machine.rs @@ -78,7 +78,7 @@ impl Machine { // due to weird lifetime problem. let sub_here = self.config.dig_sub(&self.state.subcommand_stack)?; - if let Some(sub) = self.config.find_in_subs(&sub_here.subs, &sub_here.includes, token, true)? { + if let Some(sub) = self.config.find_in_subs(&sub_here.subs, token, true)? { let name = TabryConf::unwrap_sub_name(sub)?; self.state.subcommand_stack.push(name.to_owned()); self.log(format!("STEP subcommand, add {}", name)); @@ -109,7 +109,7 @@ impl Machine { .iter() .rev() { - for flag in self.config.expand_flags(&sub.flags, &sub.includes) { + for flag in self.config.expand_flags(&sub.flags) { if flag.match_token(token) { if flag.arg { self.state.mode = MachineStateMode::Flagarg { diff --git a/src/engine/options_finder.rs b/src/engine/options_finder.rs index ea862aa..9839cd8 100644 --- a/src/engine/options_finder.rs +++ b/src/engine/options_finder.rs @@ -64,8 +64,8 @@ impl OptionsFinder { return; } - let current_sub = self.result.current_sub(); - let concrete_subs = self.result.config.flatten_subs(¤t_sub.subs, ¤t_sub.includes).unwrap(); + let opaque_subs = &self.result.current_sub().subs; + let concrete_subs = self.result.config.flatten_subs(opaque_subs).unwrap(); for s in concrete_subs { // TODO: error here if no name -- only allowable for top level res.insert(s.name.as_ref().unwrap()); @@ -91,11 +91,10 @@ impl OptionsFinder { return Ok(()); } - let current_sub = self.result.current_sub(); let mut current_sub_flags = self .result .config - .expand_flags(¤t_sub.flags, ¤t_sub.includes); + .expand_flags(&self.result.current_sub().flags); let first_reqd_flag = current_sub_flags.find(|f| f.required && !self.flag_is_used(f)); if let Some(first_reqd_flag) = first_reqd_flag { Self::add_option_for_flag(res, first_reqd_flag); @@ -108,7 +107,7 @@ impl OptionsFinder { } for sub in self.result.sub_stack.iter() { - for flag in self.result.config.expand_flags(&sub.flags, &sub.includes) { + for flag in self.result.config.expand_flags(&sub.flags) { if !self.flag_is_used(flag) { Self::add_option_for_flag(res, flag); } @@ -166,11 +165,10 @@ impl OptionsFinder { } fn add_options_subcommand_args(&self, res: &mut OptionsResults) -> Result<(), TabryConfError> { - let current_sub = self.result.current_sub(); let sub_args = self .result .config - .expand_args(¤t_sub.args, ¤t_sub.includes) + .expand_args(&self.result.current_sub().args) .collect::>(); if let Some(arg) = sub_args.get(self.result.state.args.len()) { @@ -192,7 +190,7 @@ impl OptionsFinder { unreachable!() }; for sub in &self.result.sub_stack { - for flag in self.result.config.expand_flags(&sub.flags, &sub.includes) { + for flag in self.result.config.expand_flags(&sub.flags) { if &flag.name == current_flag { self.add_options(res, &flag.options)?; return Ok(()); diff --git a/src/lang/compiler.rs b/src/lang/compiler.rs index bf42209..6989f5d 100644 --- a/src/lang/compiler.rs +++ b/src/lang/compiler.rs @@ -20,7 +20,6 @@ fn make_new_sub() -> types::TabryConcreteSub { args: vec![], flags: vec![], aliases: vec![], - includes: vec![], description: None, } } @@ -35,7 +34,12 @@ fn add_subs_from_sub_statement(subs: &mut Vec, stmt: parser::Su // the whole statement. maybe I should pass a reference that can be copied, but would have // to change references all the way down sub.description.clone_from(&stmt.description); - sub.includes.extend(stmt.includes.clone()); + add_sub_arg_flag_includes( + &mut sub.subs, + &mut sub.args, + &mut sub.flags, + stmt.includes.clone(), + ); for stmt_in_block in &stmt.statements { process_statement_inside_sub(&mut sub, stmt_in_block.clone()); } @@ -142,11 +146,27 @@ fn add_args_from_arg_statement(args: &mut Vec, stmt: parser::Ar } } +fn add_sub_arg_flag_includes( + subs: &mut Vec, + args: &mut Vec, + flags: &mut Vec, + includes: Vec, +) { + for include in includes { + args.push(types::TabryArg::TabryIncludeArg { + include: include.clone(), + }); + subs.push(types::TabrySub::TabryIncludeSub { + include: include.clone(), + }); + flags.push(types::TabryFlag::TabryIncludeFlag { include }); + } +} + fn process_statement_inside_sub_or_defargs( subs: &mut Vec, args: &mut Vec, flags: &mut Vec, - includes: &mut Vec, statement: parser::Statement, ) { match statement { @@ -154,7 +174,7 @@ fn process_statement_inside_sub_or_defargs( parser::Statement::Arg(arg_stmt) => add_args_from_arg_statement(args, arg_stmt), parser::Statement::Flag(flag_stmt) => add_flags_from_flag_statement(flags, flag_stmt), parser::Statement::Include(include_stmt) => { - includes.extend(include_stmt.includes); + add_sub_arg_flag_includes(subs, args, flags, include_stmt.includes) } _ => unreachable!( "unhandled statement in process_statement_inside_sub_or_defargs: {:?}", @@ -170,7 +190,6 @@ fn process_statement_inside_sub(sub: &mut types::TabryConcreteSub, statement: pa &mut sub.subs, &mut sub.args, &mut sub.flags, - &mut sub.includes, statement, ), } @@ -181,14 +200,12 @@ fn compile_defargs(stmt: parser::DefArgsStatement) -> (String, types::TabryArgIn args: vec![], flags: vec![], subs: vec![], - includes: vec![], }; for statement in stmt.statements { process_statement_inside_sub_or_defargs( &mut arg_include.subs, &mut arg_include.args, &mut arg_include.flags, - &mut arg_include.includes, statement, ); } diff --git a/tmp.json b/tmp.json deleted file mode 100644 index f295f14..0000000 --- a/tmp.json +++ /dev/null @@ -1,71 +0,0 @@ -{ - "cmd": null, - "main": { - "name": null, - "aliases": [], - "description": null, - "args": [], - "flags": [], - "subs": [ - { - "name": "foo", - "aliases": [], - "description": null, - "args": [], - "flags": [], - "subs": [], - "includes": [ - "waz" - ] - } - ], - "includes": [] - }, - "arg_includes": { - "waz": { - "args": [ - { - "name": null, - "description": null, - "options": [ - { - "type": "const", - "value": "a" - }, - { - "type": "const", - "value": "b" - }, - { - "type": "const", - "value": "c" - } - ], - "optional": false, - "varargs": false - } - ], - "flags": [], - "subs": [], - "includes": [ - "ok" - ] - }, - "ok": { - "args": [], - "flags": [ - { - "name": "foo", - "aliases": [], - "options": [], - "description": null, - "arg": false, - "required": false - } - ], - "subs": [], - "includes": [] - } - }, - "option_includes": {} -} \ No newline at end of file