diff --git a/src/build.rs b/src/build.rs index d0132a0..2ad5b78 100644 --- a/src/build.rs +++ b/src/build.rs @@ -510,7 +510,7 @@ fn build_parse_flag_option(param: &FlagOptionParam, signs: &str) -> String { let names = param.list_names().join(" | "); let long_name = param.render_long_name(); let var_name = param.var_name(); - if param.is_flag { + if param.is_flag() { if param.id() == "help" || param.id() == "version" { return String::new(); } @@ -566,7 +566,7 @@ fn build_parse_flag_option(param: &FlagOptionParam, signs: &str) -> String { ) }; let (min, max) = param.args_range(); - let code = if param.assigned { + let code = if param.is_assigned() { let not_assigned = if min == 1 { format!( r#" diff --git a/src/command/mod.rs b/src/command/mod.rs index bb86e86..756fcb7 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -335,10 +335,10 @@ impl Command { pub(crate) fn flag_option_signs(&self) -> String { let mut signs: IndexSet = ['-'].into(); for param in &self.flag_option_params { - if let Some(short) = ¶m.short { + if let Some(short) = param.short() { signs.extend(short.chars().take(1)) } - signs.extend(param.long_prefix.chars().take(1)) + signs.extend(param.long_prefix().chars().take(1)) } signs.into_iter().collect() } @@ -652,7 +652,7 @@ impl Command { let mut describe = false; let mut single = false; for param in self.flag_option_params.iter() { - if param.long_prefix.len() == 1 { + if param.long_prefix().len() == 1 { single = true; } if !param.describe().is_empty() { @@ -683,7 +683,7 @@ impl Command { for flag_option in &self.flag_option_params { if subcmd.find_flag_option(flag_option.id()).is_none() { let mut flag_option = flag_option.clone(); - flag_option.inherit = true; + flag_option.set_inherit(); inherited_flag_options.push(flag_option); } } @@ -702,7 +702,7 @@ impl Command { for env_param in &self.env_params { if subcmd.find_env(env_param.id()).is_none() { let mut env_param = env_param.clone(); - env_param.inherit = true; + env_param.set_inherit(); inherited_envs.push(env_param); } } diff --git a/src/mangen.rs b/src/mangen.rs index b6ce336..202cbc6 100644 --- a/src/mangen.rs +++ b/src/mangen.rs @@ -86,12 +86,12 @@ fn render_options_section(roff: &mut Roff, cmd: &Command) { roff.control("SH", ["OPTIONS"]); for param in cmd.all_flag_options() { let mut header = vec![]; - if let Some(short) = ¶m.short { + if let Some(short) = param.short() { header.push(bold(short)); header.push(roman(", ")); } header.push(bold(param.render_long_name())); - let notations = ¶m.notations; + let notations = param.notations(); if notations.len() == 1 { header.push(roman("=")); let notation = ¬ations[0]; @@ -124,7 +124,7 @@ fn render_options_section(roff: &mut Roff, cmd: &Command) { } for param in &cmd.positional_params { - let notation = ¶m.notation; + let notation = param.notation(); let mut header = match (param.required(), param.multiple_values()) { (true, true) => vec![roman("<"), italic(notation), roman(">...")], (true, false) => vec![roman("<"), italic(notation), roman(">")], @@ -178,9 +178,9 @@ fn render_envs_section(roff: &mut Roff, cmd: &Command) { } let mut body = vec![]; let mut has_help_written = false; - if !param.describe.is_empty() { + if !param.describe().is_empty() { has_help_written = true; - render_describe(&mut body, ¶m.describe); + render_describe(&mut body, param.describe()); } roff.control("TP", []); roff.text(header); diff --git a/src/matcher.rs b/src/matcher.rs index f4c0903..ee7878b 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -629,8 +629,7 @@ impl<'a, 'b> Matcher<'a, 'b> { param.render_name_notations(), )); } - if let Some(choices) = - get_param_choice(¶m.data.choice, &choices_fn_values) + if let Some(choices) = get_param_choice(param.choice(), &choices_fn_values) { for value in values.iter() { if !choices.contains(&value.to_string()) { @@ -675,8 +674,7 @@ impl<'a, 'b> Matcher<'a, 'b> { choice_values, )); } - if let Some(choices) = get_param_choice(¶m.data.choice, &choices_fn_values) - { + if let Some(choices) = get_param_choice(param.choice(), &choices_fn_values) { choice_values = choices.to_vec(); for value in values.iter() { if !choices.contains(&value.to_string()) { @@ -752,7 +750,7 @@ impl<'a, 'b> Matcher<'a, 'b> { for (i, param) in last_cmd.positional_params.iter().enumerate() { if let (Some(values), Some(choices)) = ( positional_values.get(i), - get_param_choice(¶m.data.choice, &choices_fn_values), + get_param_choice(param.choice(), &choices_fn_values), ) { for value in values.iter() { if !choices.contains(&value.to_string()) { @@ -770,8 +768,7 @@ impl<'a, 'b> Matcher<'a, 'b> { let mut missing_positionals = vec![]; for param in &last_cmd.positional_params[positional_values_len..] { if let Some(values) = bind_envs.positionals.get(param.id()) { - if let Some(choices) = get_param_choice(¶m.data.choice, &choices_fn_values) - { + if let Some(choices) = get_param_choice(param.choice(), &choices_fn_values) { for value in values.iter() { if !choices.contains(&value.to_string()) { return Some(MatchError::InvalidBindEnvironment( @@ -808,7 +805,7 @@ impl<'a, 'b> Matcher<'a, 'b> { for param in &last_cmd.env_params { if let (Some(choices), Some(value)) = ( - get_param_choice(¶m.data.choice, &choices_fn_values), + get_param_choice(param.choice(), &choices_fn_values), self.envs.get(param.id()), ) { if !choices.contains(&value.to_string()) { @@ -1021,7 +1018,7 @@ impl<'a, 'b> Matcher<'a, 'b> { CompColor::of_option() }; for v in param.list_names() { - let nospace = param.prefixed || param.assigned; + let nospace = param.is_prefixed() || param.is_assigned(); output.push((v, describe.to_string(), nospace, kind)) } } @@ -1170,11 +1167,11 @@ fn match_flag_option<'a, 'b>( } else if let Some(prefix) = param.match_prefix(arg) { let args_len = args.len(); let prefix_len = prefix.len(); - let value_args = take_value_args(args, *arg_index + 1, 1, signs, param.assigned); + let value_args = take_value_args(args, *arg_index + 1, 1, signs, param.is_assigned()); let take_empty = value_args.is_empty(); *arg_index += value_args.len(); let values = if take_empty { vec!["1"] } else { value_args }; - if !param.assigned && *arg_index == args_len - 1 { + if !param.is_assigned() && *arg_index == args_len - 1 { *arg_comp = ArgComp::OptionValue(param.id().to_string(), 0); if take_empty { *split_last_arg_at = Some(prefix_len); @@ -1184,11 +1181,12 @@ fn match_flag_option<'a, 'b>( } else { let values_max = param.args_range().1; let args_len = args.len(); - let value_args = take_value_args(args, *arg_index + 1, values_max, signs, param.assigned); + let value_args = + take_value_args(args, *arg_index + 1, values_max, signs, param.is_assigned()); *arg_index += value_args.len(); if *arg_index == args_len - 1 { if *arg_comp != ArgComp::FlagOrOption { - if param.is_option() && !param.assigned && value_args.len() <= values_max { + if param.is_option() && !param.is_assigned() && value_args.len() <= values_max { *arg_comp = ArgComp::OptionValue( param.id().to_string(), value_args.len().saturating_sub(1), @@ -1320,15 +1318,15 @@ fn comp_symbol(cmd: &Command, ch: char) -> Vec { fn comp_flag_option(param: &FlagOptionParam, index: usize) -> Vec { let value_name = param - .notations + .notations() .get(index) .map(|v| v.as_str()) - .unwrap_or_else(|| param.notations.last().unwrap()); - comp_param(param.describe_oneline(), value_name, ¶m.data) + .unwrap_or_else(|| param.notations().last().unwrap()); + comp_param(param.describe_oneline(), value_name, param.data()) } fn comp_positional(param: &PositionalParam) -> Vec { - comp_param(param.describe_oneline(), ¶m.notation, ¶m.data) + comp_param(param.describe_oneline(), param.notation(), param.data()) } fn comp_param(describe: &str, value_name: &str, data: &ParamData) -> Vec { @@ -1371,7 +1369,7 @@ fn comp_param(describe: &str, value_name: &str, data: &ParamData) -> Vec( - choice: &'a Option, + choice: Option<&'a ChoiceValue>, choices_fn_values: &'a HashMap<&str, Vec>, ) -> Option<&'a Vec> { match choice { diff --git a/src/param.rs b/src/param.rs index a7d54e2..cccea5a 100644 --- a/src/param.rs +++ b/src/param.rs @@ -8,6 +8,7 @@ use serde::Serialize; pub(crate) trait Param { fn data(&self) -> &ParamData; + fn data_mut(&mut self) -> &mut ParamData; fn id(&self) -> &str; fn var_name(&self) -> String; fn tag_name(&self) -> &str; @@ -27,6 +28,10 @@ pub(crate) trait Param { &self.data().describe } + fn describe_mut(&mut self) -> &mut String { + &mut self.data_mut().describe + } + fn required(&self) -> bool { self.data().required() } @@ -64,16 +69,16 @@ pub(crate) trait Param { #[derive(Debug, PartialEq, Eq, Clone)] pub(crate) struct FlagOptionParam { - pub(crate) data: ParamData, - pub(crate) id: String, - pub(crate) is_flag: bool, - pub(crate) short: Option, - pub(crate) long_prefix: String, - pub(crate) prefixed: bool, - pub(crate) assigned: bool, - pub(crate) raw_notations: Vec, - pub(crate) notations: Vec, - pub(crate) inherit: bool, + data: ParamData, + id: String, + is_flag: bool, + short: Option, + long_prefix: String, + prefixed: bool, + assigned: bool, + raw_notations: Vec, + notations: Vec, + inherit: bool, } impl Param for FlagOptionParam { @@ -81,6 +86,10 @@ impl Param for FlagOptionParam { &self.data } + fn data_mut(&mut self) -> &mut ParamData { + &mut self.data + } + fn id(&self) -> &str { &self.id } @@ -240,6 +249,26 @@ impl FlagOptionParam { !self.is_flag() } + pub(crate) fn is_assigned(&self) -> bool { + self.assigned + } + + pub(crate) fn is_prefixed(&self) -> bool { + self.prefixed + } + + pub(crate) fn short(&self) -> &Option { + &self.short + } + + pub(crate) fn long_prefix(&self) -> &str { + &self.long_prefix + } + + pub(crate) fn notations(&self) -> &[String] { + &self.notations + } + pub(crate) fn args_range(&self) -> (usize, usize) { let len = self.notations.len(); if self.terminated() @@ -399,6 +428,10 @@ impl FlagOptionParam { } } + pub(crate) fn set_inherit(&mut self) { + self.inherit = true; + } + pub(crate) fn match_prefix<'a>(&self, arg: &'a str) -> Option<&'a str> { if self.prefixed { self.list_names().iter().find_map(|v| { @@ -458,9 +491,9 @@ pub struct FlagOptionValue { #[derive(Debug, PartialEq, Eq, Clone)] pub(crate) struct PositionalParam { - pub(crate) data: ParamData, - pub(crate) raw_notation: Option, - pub(crate) notation: String, + data: ParamData, + raw_notation: Option, + notation: String, } impl Param for PositionalParam { @@ -468,6 +501,10 @@ impl Param for PositionalParam { &self.data } + fn data_mut(&mut self) -> &mut ParamData { + &mut self.data + } + fn id(&self) -> &str { &self.data().name } @@ -536,6 +573,10 @@ impl PositionalParam { } } + pub(crate) fn notation(&self) -> &str { + &self.notation + } + pub(crate) fn render_notation(&self) -> String { let name: &String = &self.notation; match (self.required(), self.multiple_values()) { @@ -585,9 +626,9 @@ pub struct PositionalValue { #[derive(Debug, PartialEq, Eq, Clone)] pub(crate) struct EnvParam { - pub(crate) data: ParamData, - pub(crate) describe: String, - pub(crate) inherit: bool, + data: ParamData, + describe: String, + inherit: bool, } impl Param for EnvParam { @@ -595,6 +636,10 @@ impl Param for EnvParam { &self.data } + fn data_mut(&mut self) -> &mut ParamData { + &mut self.data + } + fn id(&self) -> &str { &self.data().name } @@ -674,6 +719,10 @@ impl EnvParam { }; Some(value) } + + pub(crate) fn set_inherit(&mut self) { + self.inherit = true; + } } #[derive(Debug, Serialize)] diff --git a/src/parser.rs b/src/parser.rs index fbe7bdd..a774a3c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,5 +1,6 @@ use crate::param::{ - ChoiceValue, DefaultValue, EnvParam, FlagOptionParam, Modifier, ParamData, PositionalParam, + ChoiceValue, DefaultValue, EnvParam, FlagOptionParam, Modifier, Param, ParamData, + PositionalParam, }; use crate::utils::is_special_var_char; use crate::Result; @@ -85,27 +86,18 @@ pub(crate) fn parse(source: &str) -> Result> { EventData::Cmd(text) } EventData::Env(mut param) => { - line_idx += take_comment_lines( - &lines, - line_idx + 1, - &mut param.data.describe, - ); + line_idx += + take_comment_lines(&lines, line_idx + 1, param.describe_mut()); EventData::Env(param) } EventData::FlagOption(mut param) => { - line_idx += take_comment_lines( - &lines, - line_idx + 1, - &mut param.data.describe, - ); + line_idx += + take_comment_lines(&lines, line_idx + 1, param.describe_mut()); EventData::FlagOption(param) } EventData::Positional(mut param) => { - line_idx += take_comment_lines( - &lines, - line_idx + 1, - &mut param.data.describe, - ); + line_idx += + take_comment_lines(&lines, line_idx + 1, param.describe_mut()); EventData::Positional(param) } v => v,