Skip to content

Commit 7ce33d9

Browse files
committed
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
1 parent 765fa24 commit 7ce33d9

File tree

7 files changed

+39
-127
lines changed

7 files changed

+39
-127
lines changed

TODO.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ TODO before "public announcement"
55
* put on cargo
66
* more examples / language reference in this repo.
77
delegate "super aliases"
8-
-> but delegate not working in fish, and not in all cases in bash (works for docker, not for git)
98
* bump version number and add deb
109
* better instructions for installing (build from source, cargo, nix, deb)
1110

src/core/config.rs

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ impl TabryConf {
6060
let mut result = vec![&self.main];
6161

6262
for name in sub_names_vec {
63-
let sub = result.last().unwrap();
63+
let subs_here = &result.last().unwrap().subs;
6464
let next =
65-
self.find_in_subs(&sub.subs, &sub.includes, name, false)?
65+
self.find_in_subs(subs_here, name, false)?
6666
.ok_or(TabryConfError::InternalError(
6767
"sub not found in dig sub".to_owned(),
6868
))?;
@@ -75,11 +75,10 @@ impl TabryConf {
7575
pub fn find_in_subs<'a>(
7676
&'a self,
7777
subs: &'a [TabrySub],
78-
includes: &'a Vec<String>,
7978
name: &String,
8079
check_aliases: bool,
8180
) -> Result<Option<&TabryConcreteSub>, TabryConfError> {
82-
let concrete_subs: Vec<&TabryConcreteSub> = self.flatten_subs(subs, includes)?;
81+
let concrete_subs: Vec<&TabryConcreteSub> = self.flatten_subs(subs)?;
8382

8483
for sub in concrete_subs {
8584
let sub_name = Self::unwrap_sub_name(sub)?;
@@ -116,16 +115,15 @@ impl TabryConf {
116115
pub fn flatten_subs<'a>(
117116
&'a self,
118117
subs: &'a [TabrySub],
119-
includes: &'a Vec<String>,
120118
) -> Result<Vec<&TabryConcreteSub>, TabryConfError> {
121-
let mut vecofvecs = subs
119+
let vecofvecs = subs
122120
.iter()
123121
.map(|sub| match sub {
124122
TabrySub::TabryIncludeSub { include } => {
125123
// Lookup include, which may return an error
126124
let inc = self.get_arg_include(include)?;
127125
// Flatten the include's subs recursively (which may return an error)
128-
self.flatten_subs(&inc.subs, &inc.includes)
126+
self.flatten_subs(&inc.subs)
129127
}
130128
TabrySub::TabryConcreteSub(s) =>
131129
// This is a concrete sub, add it
@@ -135,15 +133,6 @@ impl TabryConf {
135133
})
136134
.collect::<Result<Vec<_>, _>>()?;
137135

138-
vecofvecs.extend(
139-
includes.iter().map(|include| {
140-
// Lookup include, which may return an error
141-
let inc = self.get_arg_include(include)?;
142-
// Flatten the include's subs recursively (which may return an error)
143-
self.flatten_subs(&inc.subs, &inc.includes)
144-
}).collect::<Result<Vec<_>, _>>()?,
145-
);
146-
147136
// collect() will return an error if there were one, so now we just have flatten the
148137
// vectors
149138
Ok(vecofvecs.into_iter().flatten().collect::<Vec<_>>())
@@ -154,47 +143,31 @@ impl TabryConf {
154143
pub fn expand_flags<'a>(
155144
&'a self,
156145
flags: &'a [TabryFlag],
157-
includes: &'a [String],
158146
) -> Box<dyn Iterator<Item = &TabryConcreteFlag> + 'a> {
159147
let iter = flags.iter().flat_map(|flag| match flag {
160148
TabryFlag::TabryIncludeFlag { include } => {
161149
// TODO: bubble up error instead of unwrap (use get_arg_include)
162150
let include = self.arg_includes.get(include).unwrap();
163-
self.expand_flags(&include.flags, &include.includes)
151+
self.expand_flags(&include.flags)
164152
}
165153
TabryFlag::TabryConcreteFlag(concrete_flag) => Box::new(std::iter::once(concrete_flag)),
166154
});
167-
let iter = iter.chain(
168-
includes.iter().flat_map(move |include| {
169-
// TODO: bubble up error instead of unwrap
170-
let inc = self.arg_includes.get(include).unwrap();
171-
self.expand_flags(&inc.flags, &inc.includes)
172-
}),
173-
);
174-
175155
Box::new(iter)
176156
}
177157

178158
// TODO: this is an exact copy of the the above expand_flags()
179159
pub fn expand_args<'a>(
180160
&'a self,
181161
args: &'a [TabryArg],
182-
includes: &'a [String],
183162
) -> Box<dyn Iterator<Item = &TabryConcreteArg> + 'a> {
184163
let iter = args.iter().flat_map(|arg| match arg {
185164
TabryArg::TabryIncludeArg { include } => {
186165
// TODO: bubble up error instead of unwrap (use get_arg_include)
187166
let include = self.arg_includes.get(include).unwrap();
188-
self.expand_args(&include.args, &include.includes)
167+
self.expand_args(&include.args)
189168
}
190169
TabryArg::TabryConcreteArg(concrete_arg) => Box::new(std::iter::once(concrete_arg)),
191170
});
192-
let iter = iter.chain(
193-
includes.iter().flat_map(move |include| {
194-
let inc = self.arg_includes.get(include).unwrap();
195-
self.expand_args(&inc.args, &inc.includes)
196-
}),
197-
);
198171
Box::new(iter)
199172
}
200173
}

src/core/types.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ pub struct TabryConcreteSub {
7070
pub flags: Vec<TabryFlag>,
7171
#[serde(default)]
7272
pub subs: Vec<TabrySub>,
73-
#[serde(default)]
74-
pub includes: Vec<String>,
7573
}
7674

7775
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -89,6 +87,4 @@ pub struct TabryArgInclude {
8987
pub flags: Vec<TabryFlag>,
9088
#[serde(default)]
9189
pub subs: Vec<TabrySub>,
92-
#[serde(default)]
93-
pub includes: Vec<String>,
9490
}

src/engine/machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Machine {
7878
// due to weird lifetime problem.
7979
let sub_here = self.config.dig_sub(&self.state.subcommand_stack)?;
8080

81-
if let Some(sub) = self.config.find_in_subs(&sub_here.subs, &sub_here.includes, token, true)? {
81+
if let Some(sub) = self.config.find_in_subs(&sub_here.subs, token, true)? {
8282
let name = TabryConf::unwrap_sub_name(sub)?;
8383
self.state.subcommand_stack.push(name.to_owned());
8484
self.log(format!("STEP subcommand, add {}", name));
@@ -109,7 +109,7 @@ impl Machine {
109109
.iter()
110110
.rev()
111111
{
112-
for flag in self.config.expand_flags(&sub.flags, &sub.includes) {
112+
for flag in self.config.expand_flags(&sub.flags) {
113113
if flag.match_token(token) {
114114
if flag.arg {
115115
self.state.mode = MachineStateMode::Flagarg {

src/engine/options_finder.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ impl OptionsFinder {
6464
return;
6565
}
6666

67-
let current_sub = self.result.current_sub();
68-
let concrete_subs = self.result.config.flatten_subs(&current_sub.subs, &current_sub.includes).unwrap();
67+
let opaque_subs = &self.result.current_sub().subs;
68+
let concrete_subs = self.result.config.flatten_subs(opaque_subs).unwrap();
6969
for s in concrete_subs {
7070
// TODO: error here if no name -- only allowable for top level
7171
res.insert(s.name.as_ref().unwrap());
@@ -91,11 +91,10 @@ impl OptionsFinder {
9191
return Ok(());
9292
}
9393

94-
let current_sub = self.result.current_sub();
9594
let mut current_sub_flags = self
9695
.result
9796
.config
98-
.expand_flags(&current_sub.flags, &current_sub.includes);
97+
.expand_flags(&self.result.current_sub().flags);
9998
let first_reqd_flag = current_sub_flags.find(|f| f.required && !self.flag_is_used(f));
10099
if let Some(first_reqd_flag) = first_reqd_flag {
101100
Self::add_option_for_flag(res, first_reqd_flag);
@@ -108,7 +107,7 @@ impl OptionsFinder {
108107
}
109108

110109
for sub in self.result.sub_stack.iter() {
111-
for flag in self.result.config.expand_flags(&sub.flags, &sub.includes) {
110+
for flag in self.result.config.expand_flags(&sub.flags) {
112111
if !self.flag_is_used(flag) {
113112
Self::add_option_for_flag(res, flag);
114113
}
@@ -166,11 +165,10 @@ impl OptionsFinder {
166165
}
167166

168167
fn add_options_subcommand_args(&self, res: &mut OptionsResults) -> Result<(), TabryConfError> {
169-
let current_sub = self.result.current_sub();
170168
let sub_args = self
171169
.result
172170
.config
173-
.expand_args(&current_sub.args, &current_sub.includes)
171+
.expand_args(&self.result.current_sub().args)
174172
.collect::<Vec<_>>();
175173

176174
if let Some(arg) = sub_args.get(self.result.state.args.len()) {
@@ -192,7 +190,7 @@ impl OptionsFinder {
192190
unreachable!()
193191
};
194192
for sub in &self.result.sub_stack {
195-
for flag in self.result.config.expand_flags(&sub.flags, &sub.includes) {
193+
for flag in self.result.config.expand_flags(&sub.flags) {
196194
if &flag.name == current_flag {
197195
self.add_options(res, &flag.options)?;
198196
return Ok(());

src/lang/compiler.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ fn make_new_sub() -> types::TabryConcreteSub {
2020
args: vec![],
2121
flags: vec![],
2222
aliases: vec![],
23-
includes: vec![],
2423
description: None,
2524
}
2625
}
@@ -35,7 +34,12 @@ fn add_subs_from_sub_statement(subs: &mut Vec<types::TabrySub>, stmt: parser::Su
3534
// the whole statement. maybe I should pass a reference that can be copied, but would have
3635
// to change references all the way down
3736
sub.description.clone_from(&stmt.description);
38-
sub.includes.extend(stmt.includes.clone());
37+
add_sub_arg_flag_includes(
38+
&mut sub.subs,
39+
&mut sub.args,
40+
&mut sub.flags,
41+
stmt.includes.clone(),
42+
);
3943
for stmt_in_block in &stmt.statements {
4044
process_statement_inside_sub(&mut sub, stmt_in_block.clone());
4145
}
@@ -142,19 +146,35 @@ fn add_args_from_arg_statement(args: &mut Vec<types::TabryArg>, stmt: parser::Ar
142146
}
143147
}
144148

149+
fn add_sub_arg_flag_includes(
150+
subs: &mut Vec<types::TabrySub>,
151+
args: &mut Vec<types::TabryArg>,
152+
flags: &mut Vec<types::TabryFlag>,
153+
includes: Vec<String>,
154+
) {
155+
for include in includes {
156+
args.push(types::TabryArg::TabryIncludeArg {
157+
include: include.clone(),
158+
});
159+
subs.push(types::TabrySub::TabryIncludeSub {
160+
include: include.clone(),
161+
});
162+
flags.push(types::TabryFlag::TabryIncludeFlag { include });
163+
}
164+
}
165+
145166
fn process_statement_inside_sub_or_defargs(
146167
subs: &mut Vec<types::TabrySub>,
147168
args: &mut Vec<types::TabryArg>,
148169
flags: &mut Vec<types::TabryFlag>,
149-
includes: &mut Vec<String>,
150170
statement: parser::Statement,
151171
) {
152172
match statement {
153173
parser::Statement::Sub(child_sub_stmt) => add_subs_from_sub_statement(subs, child_sub_stmt),
154174
parser::Statement::Arg(arg_stmt) => add_args_from_arg_statement(args, arg_stmt),
155175
parser::Statement::Flag(flag_stmt) => add_flags_from_flag_statement(flags, flag_stmt),
156176
parser::Statement::Include(include_stmt) => {
157-
includes.extend(include_stmt.includes);
177+
add_sub_arg_flag_includes(subs, args, flags, include_stmt.includes)
158178
}
159179
_ => unreachable!(
160180
"unhandled statement in process_statement_inside_sub_or_defargs: {:?}",
@@ -170,7 +190,6 @@ fn process_statement_inside_sub(sub: &mut types::TabryConcreteSub, statement: pa
170190
&mut sub.subs,
171191
&mut sub.args,
172192
&mut sub.flags,
173-
&mut sub.includes,
174193
statement,
175194
),
176195
}
@@ -181,14 +200,12 @@ fn compile_defargs(stmt: parser::DefArgsStatement) -> (String, types::TabryArgIn
181200
args: vec![],
182201
flags: vec![],
183202
subs: vec![],
184-
includes: vec![],
185203
};
186204
for statement in stmt.statements {
187205
process_statement_inside_sub_or_defargs(
188206
&mut arg_include.subs,
189207
&mut arg_include.args,
190208
&mut arg_include.flags,
191-
&mut arg_include.includes,
192209
statement,
193210
);
194211
}

tmp.json

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)