Struct number_range::NumberRangeOptions
source · 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>
impl<T: FromStr + One + Copy + PartialOrd + Add<Output = T>> NumberRangeOptions<T>
sourcepub fn with_group_sep(self, sep: char) -> Self
pub fn with_group_sep(self, sep: char) -> Self
Change the group separator character
sourcepub fn with_whitespace(self, flag: bool) -> Self
pub fn with_whitespace(self, flag: bool) -> Self
Change the group separator character
sourcepub fn with_decimal_sep(self, sep: char) -> Self
pub fn with_decimal_sep(self, sep: char) -> Self
Change the decimal separator character
sourcepub fn with_list_sep(self, sep: char) -> Self
pub fn with_list_sep(self, sep: char) -> Self
Change the list separator character
sourcepub fn with_range_sep(self, sep: char) -> Self
pub fn with_range_sep(self, sep: char) -> Self
Change the range separator character
sourcepub fn with_default_start(self, def: T) -> Self
pub fn with_default_start(self, def: T) -> Self
Include a default start value
sourcepub fn with_default_end(self, def: T) -> Self
pub fn with_default_end(self, def: T) -> Self
Include a default end value
sourcepub fn parse<'a>(self, numstr: &'a str) -> Result<NumberRange<'_, T>>where
<T as FromStr>::Err: Error + Send + Sync + 'static,
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.