Skip to content

Commit

Permalink
refactor: do not export param field
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Apr 12, 2024
1 parent 9277ab9 commit 6645783
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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#"
Expand Down
10 changes: 5 additions & 5 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ impl Command {
pub(crate) fn flag_option_signs(&self) -> String {
let mut signs: IndexSet<char> = ['-'].into();
for param in &self.flag_option_params {
if let Some(short) = &param.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()
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/mangen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) = &param.short {
if let Some(short) = param.short() {
header.push(bold(short));
header.push(roman(", "));
}
header.push(bold(param.render_long_name()));
let notations = &param.notations;
let notations = param.notations();
if notations.len() == 1 {
header.push(roman("="));
let notation = &notations[0];
Expand Down Expand Up @@ -124,7 +124,7 @@ fn render_options_section(roff: &mut Roff, cmd: &Command) {
}

for param in &cmd.positional_params {
let notation = &param.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(">")],
Expand Down Expand Up @@ -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, &param.describe);
render_describe(&mut body, param.describe());
}
roff.control("TP", []);
roff.text(header);
Expand Down
34 changes: 16 additions & 18 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,7 @@ impl<'a, 'b> Matcher<'a, 'b> {
param.render_name_notations(),
));
}
if let Some(choices) =
get_param_choice(&param.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()) {
Expand Down Expand Up @@ -675,8 +674,7 @@ impl<'a, 'b> Matcher<'a, 'b> {
choice_values,
));
}
if let Some(choices) = get_param_choice(&param.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()) {
Expand Down Expand Up @@ -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(&param.data.choice, &choices_fn_values),
get_param_choice(param.choice(), &choices_fn_values),
) {
for value in values.iter() {
if !choices.contains(&value.to_string()) {
Expand All @@ -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(&param.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(
Expand Down Expand Up @@ -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(&param.data.choice, &choices_fn_values),
get_param_choice(param.choice(), &choices_fn_values),
self.envs.get(param.id()),
) {
if !choices.contains(&value.to_string()) {
Expand Down Expand Up @@ -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))
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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),
Expand Down Expand Up @@ -1320,15 +1318,15 @@ fn comp_symbol(cmd: &Command, ch: char) -> Vec<CompItem> {

fn comp_flag_option(param: &FlagOptionParam, index: usize) -> Vec<CompItem> {
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, &param.data)
.unwrap_or_else(|| param.notations().last().unwrap());
comp_param(param.describe_oneline(), value_name, param.data())
}

fn comp_positional(param: &PositionalParam) -> Vec<CompItem> {
comp_param(param.describe_oneline(), &param.notation, &param.data)
comp_param(param.describe_oneline(), param.notation(), param.data())
}

fn comp_param(describe: &str, value_name: &str, data: &ParamData) -> Vec<CompItem> {
Expand Down Expand Up @@ -1371,7 +1369,7 @@ fn comp_param(describe: &str, value_name: &str, data: &ParamData) -> Vec<CompIte
}

fn get_param_choice<'a, 'b: 'a>(
choice: &'a Option<ChoiceValue>,
choice: Option<&'a ChoiceValue>,
choices_fn_values: &'a HashMap<&str, Vec<String>>,
) -> Option<&'a Vec<String>> {
match choice {
Expand Down
81 changes: 65 additions & 16 deletions src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
}
Expand Down Expand Up @@ -64,23 +69,27 @@ 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<String>,
pub(crate) long_prefix: String,
pub(crate) prefixed: bool,
pub(crate) assigned: bool,
pub(crate) raw_notations: Vec<String>,
pub(crate) notations: Vec<String>,
pub(crate) inherit: bool,
data: ParamData,
id: String,
is_flag: bool,
short: Option<String>,
long_prefix: String,
prefixed: bool,
assigned: bool,
raw_notations: Vec<String>,
notations: Vec<String>,
inherit: bool,
}

impl Param for FlagOptionParam {
fn data(&self) -> &ParamData {
&self.data
}

fn data_mut(&mut self) -> &mut ParamData {
&mut self.data
}

fn id(&self) -> &str {
&self.id
}
Expand Down Expand Up @@ -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<String> {
&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()
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -458,16 +491,20 @@ pub struct FlagOptionValue {

#[derive(Debug, PartialEq, Eq, Clone)]
pub(crate) struct PositionalParam {
pub(crate) data: ParamData,
pub(crate) raw_notation: Option<String>,
pub(crate) notation: String,
data: ParamData,
raw_notation: Option<String>,
notation: String,
}

impl Param for PositionalParam {
fn data(&self) -> &ParamData {
&self.data
}

fn data_mut(&mut self) -> &mut ParamData {
&mut self.data
}

fn id(&self) -> &str {
&self.data().name
}
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -585,16 +626,20 @@ 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 {
fn data(&self) -> &ParamData {
&self.data
}

fn data_mut(&mut self) -> &mut ParamData {
&mut self.data
}

fn id(&self) -> &str {
&self.data().name
}
Expand Down Expand Up @@ -674,6 +719,10 @@ impl EnvParam {
};
Some(value)
}

pub(crate) fn set_inherit(&mut self) {
self.inherit = true;
}
}

#[derive(Debug, Serialize)]
Expand Down
Loading

0 comments on commit 6645783

Please sign in to comment.