Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the trait From<regex::RegexSet> to Arg #2675

Closed
2 tasks done
PandH4cker opened this issue Aug 10, 2021 · 1 comment · Fixed by #2899
Closed
2 tasks done

Add support for the trait From<regex::RegexSet> to Arg #2675

PandH4cker opened this issue Aug 10, 2021 · 1 comment · Fixed by #2899
Labels
A-validators Area: ArgMatches validation logi
Milestone

Comments

@PandH4cker
Copy link

Please complete the following tasks

  • I have searched the discussions
  • I have searched the existing issues

Clap Version

3.0.0-beta.2

Describe your use case

It would be nice that clap handle not only a single RegEx in the validator_regex() function but a RegexSet.
RegexSet are useful to combine multiple regex together and check if any of the regex provided match the string to test.

What I standed for BEFORE validator_regex():

This is what I did in conjunction to validator() function.

use lazy_static::lazy_static;
use regex::Regex;

pub fn is_hosts(v: &str) -> Result<(), String> {
    lazy_static! {
        static ref HOSTNAME_REGEX: Regex = Regex::new(r"(?x)
            ^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])
            (\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$
        ").unwrap();
        static ref IP_REGEX: Regex = Regex::new(r"(?x)
            ^((\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}
            (\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$
        ").unwrap();
        static ref NETINT_REGEX: Regex = Regex::new(r"(?x)
            ^((\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}
            (\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])/([1-9]|[12]\d|3[012])$
        ").unwrap();
    }
    match HOSTNAME_REGEX.is_match(v) || IP_REGEX.is_match(v) || NETINT_REGEX.is_match(v) {
        true => {
            Ok(())
        }
        false => {
            Err(format!("{} isn't a host nor an ip nor a network interface", v))
        }
    }
}

What I standed for AFTER validator_regex():

So I would like to do this kind of thing:

//In the module that generate the App
App::new("...")
// [SNIP...]
      .arg(
            Arg::new(target::NAME)
                .last(true)
                .takes_value(true)
                .value_delimiter(" ")
                .multiple(true)
                //.validator(validators::net::is_hosts)
                .validator_regex(HOST_REGEXSET, "only hostnames, ip, network intefaces are allowed")
                .required_unless_present_any(&[input_list::NAME, input_random::NAME])
                .display_order(1)
     )
//In a module of constants variables
use regex::RegexSet;

const HOSTNAME_REGEX_STR : &str = r"(?x)
            ^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])
            (\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$
";
const IP_REGEX_STR : &str = r"(?x)
            ^((\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}
            (\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$
";
const NETINT_REGEX_STR : &str = r"(?x)
            ^((\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}
            (\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])/([1-9]|[12]\d|3[012])$
";
pub const HOST_REGEXSET : RegexSet = RegexSet::new(&[
    HOSTNAME_REGEX_STR,
    IP_REGEX_STR,
    NETINT_REGEX_STR
]).unwrap();

Describe the solution you'd like

I would like a function or an implementation to validate the RegexSet like the validator_regex():

impl<'a> From<RegexSet> for RegexRef<'a> {
    fn from(r: RegexSet) -> Self {
        RegexRef(Cow::Owned(r))
    }
}

But RegexRef take a Cow<'a, Regex>, maybe you should refactor this to have RegexSet instead ?

Alternatives, if applicable

The solution I standed before the validator_regex() works fine, I would like to know:

Which one of these both solutions are the most effective ?

Since I am not figuring out yet : How could I use the lazy_static to improve the effectiveness of the regex compilation combining to store it in a constant variable and be reusable further in the code ?

So until there is not an implementation for handling RegexSet I would use the validator and my match pattern 😄 .

Additional Context

No response

@pksunkara pksunkara added the A-validators Area: ArgMatches validation logi label Aug 10, 2021
@pksunkara pksunkara added this to the 3.0 milestone Aug 10, 2021
@epage epage modified the milestones: 3.0, 3.1 Oct 16, 2021
@bors bors bot closed this as completed in #2899 Oct 16, 2021
@epage
Copy link
Member

epage commented May 23, 2022

FYI I am considering removing validator_regex, see #3743

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-validators Area: ArgMatches validation logi
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants