pub struct NumberRangeOptions<T> {
    pub group_sep: char,
    pub whitespace: bool,
    pub decimal_sep: char,
    pub list_sep: char,
    pub range_sep: char,
    pub default_start: Option<T>,
    pub default_end: Option<T>,
}
Expand description

Options for the NumberRange, includes different separator character customization.

For example, if you’re dealing with unsigned numbers then you can use - as a range separator to parse ranges from many sources.

    let rng: Vec<usize> = NumberRangeOptions::default()
        .with_range_sep('-')
        .parse("1,4,6-10,14")
        .unwrap()
        .collect();
    println!("{:?}", rng);

Since it is made using generics to be able to pass as many types of numeric types as possible, you might have to give the types in between when rust cannot infer it.

It can be inferred if you have intermediate variables with type.

    let rng: NumberRange<usize> = NumberRangeOptions::default()
        .with_range_sep('-')
        .parse("1,4,6-10,14")
        .unwrap();
    println!("{:?}", rng.collect::<Vec<usize>>());
NumberRangeOptions::<usize>::new()
             .with_list_sep(',')
             .with_range_sep('-')
             .parse("1,3-10,14")?;

Or with default start and end:

assert_eq!(NumberRangeOptions::<usize>::new()
             .with_list_sep(',')
             .with_range_sep(':')
             .with_default_start(1)
             .parse(":4,14")?.collect::<Vec<usize>>(), vec![1,2,3,4,14]);

All the numbers in the string must be of the same type that you want to parse into, due to that restriction even the step needs to be unsigned for unsigned number (meaning "4:-1:1" would fail even if the final output should be unsigned).

Another function is to parse the numbers in different localization like different decimal separators or grouping of numbers.

let rng: Vec<usize> = NumberRangeOptions::new()
             .with_list_sep(';')
             .with_range_sep('-')
             .with_group_sep(',')
             .with_whitespace(true)
             .parse("1,200; 1, 400, 230")?.collect();
assert_eq!(rng, vec![1200, 1400230]);

Fields§

§group_sep: char

Character used to group numbers [default: _]. Group separator is the first one to be removed from the string, if any other characters are same as the group separators then they’ll be useless.

§whitespace: bool

Remove spaces between the numbers. While spaces are removed after the group separator. If any other separator characters are whitespace they’ll be useless.

§decimal_sep: char

Decimal separator [default: .]. Decimal separator is replaced by . for rust to parse the float properly. The replacement occurs after the whitespace removal.

§list_sep: char

Separator for different numbers or numbers range [default: ,]. List separator is used to split first.

§range_sep: char

Separator for range start, step, and end [default: :]. This one is used at the end, so if it is using the same character as other separators, it’ll be useless, or have different meaning.

§default_start: Option<T>

Default start value, if the start value is ommited in a range, it’ll be used

§default_end: Option<T>

Default end value, if the end value is ommited in a range, it’ll be used

Implementations§

source§

impl<T: FromStr + One + Copy + PartialOrd + Add<Output = T>> NumberRangeOptions<T>

source

pub fn new() -> Self

New struct with default options

source

pub fn with_group_sep(self, sep: char) -> Self

Change the group separator character

source

pub fn with_whitespace(self, flag: bool) -> Self

Change the group separator character

source

pub fn with_decimal_sep(self, sep: char) -> Self

Change the decimal separator character

source

pub fn with_list_sep(self, sep: char) -> Self

Change the list separator character

source

pub fn with_range_sep(self, sep: char) -> Self

Change the range separator character

source

pub fn with_default_start(self, def: T) -> Self

Include a default start value

source

pub fn with_default_end(self, def: T) -> Self

Include a default end value

source

pub fn parse<'a>(self, numstr: &'a str) -> Result<NumberRange<'_, T>>where <T as FromStr>::Err: Error + Send + Sync + 'static,

Same as NumberRange::parse_str(), Makes a NumberRange<T> and parses the string.

Trait Implementations§

source§

impl<T: Debug> Debug for NumberRangeOptions<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: FromStr + One + Copy + PartialOrd + Add<Output = T>> Default for NumberRangeOptions<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for NumberRangeOptions<T>where T: RefUnwindSafe,

§

impl<T> Send for NumberRangeOptions<T>where T: Send,

§

impl<T> Sync for NumberRangeOptions<T>where T: Sync,

§

impl<T> Unpin for NumberRangeOptions<T>where T: Unpin,

§

impl<T> UnwindSafe for NumberRangeOptions<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.