Skip to content

Commit

Permalink
Revert unified_includes stuff
Browse files Browse the repository at this point in the history
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
  • Loading branch information
evanbattaglia committed Sep 14, 2024
1 parent 765fa24 commit 7ce33d9
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 127 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
41 changes: 7 additions & 34 deletions src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
))?;
Expand All @@ -75,11 +75,10 @@ impl TabryConf {
pub fn find_in_subs<'a>(
&'a self,
subs: &'a [TabrySub],
includes: &'a Vec<String>,
name: &String,
check_aliases: bool,
) -> Result<Option<&TabryConcreteSub>, 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)?;
Expand Down Expand Up @@ -116,16 +115,15 @@ impl TabryConf {
pub fn flatten_subs<'a>(
&'a self,
subs: &'a [TabrySub],
includes: &'a Vec<String>,
) -> Result<Vec<&TabryConcreteSub>, 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
Expand All @@ -135,15 +133,6 @@ impl TabryConf {
})
.collect::<Result<Vec<_>, _>>()?;

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::<Result<Vec<_>, _>>()?,
);

// collect() will return an error if there were one, so now we just have flatten the
// vectors
Ok(vecofvecs.into_iter().flatten().collect::<Vec<_>>())
Expand All @@ -154,47 +143,31 @@ impl TabryConf {
pub fn expand_flags<'a>(
&'a self,
flags: &'a [TabryFlag],
includes: &'a [String],
) -> Box<dyn Iterator<Item = &TabryConcreteFlag> + '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)
}

// TODO: this is an exact copy of the the above expand_flags()
pub fn expand_args<'a>(
&'a self,
args: &'a [TabryArg],
includes: &'a [String],
) -> Box<dyn Iterator<Item = &TabryConcreteArg> + '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)
}
}
4 changes: 0 additions & 4 deletions src/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ pub struct TabryConcreteSub {
pub flags: Vec<TabryFlag>,
#[serde(default)]
pub subs: Vec<TabrySub>,
#[serde(default)]
pub includes: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -89,6 +87,4 @@ pub struct TabryArgInclude {
pub flags: Vec<TabryFlag>,
#[serde(default)]
pub subs: Vec<TabrySub>,
#[serde(default)]
pub includes: Vec<String>,
}
4 changes: 2 additions & 2 deletions src/engine/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 6 additions & 8 deletions src/engine/options_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ impl OptionsFinder {
return;
}

let current_sub = self.result.current_sub();
let concrete_subs = self.result.config.flatten_subs(&current_sub.subs, &current_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());
Expand All @@ -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(&current_sub.flags, &current_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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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(&current_sub.args, &current_sub.includes)
.expand_args(&self.result.current_sub().args)
.collect::<Vec<_>>();

if let Some(arg) = sub_args.get(self.result.state.args.len()) {
Expand All @@ -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(());
Expand Down
31 changes: 24 additions & 7 deletions src/lang/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ fn make_new_sub() -> types::TabryConcreteSub {
args: vec![],
flags: vec![],
aliases: vec![],
includes: vec![],
description: None,
}
}
Expand All @@ -35,7 +34,12 @@ fn add_subs_from_sub_statement(subs: &mut Vec<types::TabrySub>, 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());
}
Expand Down Expand Up @@ -142,19 +146,35 @@ fn add_args_from_arg_statement(args: &mut Vec<types::TabryArg>, stmt: parser::Ar
}
}

fn add_sub_arg_flag_includes(
subs: &mut Vec<types::TabrySub>,
args: &mut Vec<types::TabryArg>,
flags: &mut Vec<types::TabryFlag>,
includes: Vec<String>,
) {
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<types::TabrySub>,
args: &mut Vec<types::TabryArg>,
flags: &mut Vec<types::TabryFlag>,
includes: &mut Vec<String>,
statement: parser::Statement,
) {
match statement {
parser::Statement::Sub(child_sub_stmt) => add_subs_from_sub_statement(subs, child_sub_stmt),
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: {:?}",
Expand All @@ -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,
),
}
Expand All @@ -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,
);
}
Expand Down
71 changes: 0 additions & 71 deletions tmp.json

This file was deleted.

0 comments on commit 7ce33d9

Please sign in to comment.