Crates.io | range-parser |
lib.rs | range-parser |
version | 0.1.2 |
source | src |
created_at | 2024-07-08 08:57:03.654298 |
updated_at | 2024-07-22 10:15:28.062422 |
description | A rust library to parse ranges representation of any kind of numbers |
homepage | https://github.com/veeso/range-parser |
repository | https://github.com/veeso/range-parser |
max_upload_size | |
id | 1295508 |
size | 18,175 |
range-parser is a simple Rust crate to parse range from text representation (e.g. 1-3,5-8
, 1,3,4
, 1-5
) into a Vector containing all the items for that range.
Include range-parser
to your Cargo.toml
range-parser = "0.1"
Parse range from str
let range_str = "1-3,5-8";
let range: Vec<u64> = range_parser::parse(range_str).unwrap();
assert_eq!(&range, &[1, 2, 3, 5, 6, 7, 8]);
range-parser supports any kind of number primitive.
It is possible to extend the range-parser for custom types as long as they satisfy these trait bounds: T: FromStr + Add<Output = T> + PartialEq + PartialOrd + Unit + Copy,
.
This requires you to implement the trait Unit
which is exposed by this library.
The trait Unit is defined as
pub trait Unit {
fn unit() -> Self;
}
and should return the base unit for a type, which for numbers should be 1
.
let range: Vec<u64> = range_parser::parse("1-3").unwrap();
assert_eq!(range, vec![1, 2, 3]);
let range: Vec<u64> = range_parser::parse("1,3,4").unwrap();
assert_eq!(range, vec![1, 3, 4]);
let range: Vec<u64> = range_parser::parse("1,3-5,2").unwrap();
assert_eq!(range, vec![1, 3, 4, 5, 2]);
let range: Vec<i32> = range_parser::parse("-8,-5--1,0-3,-1").unwrap();
assert_eq!(range, vec![-8, -5, -4, -3, -2, -1, 0, 1, 2, 3, -1]);
// parse range using `;` as separator for values and `..` as separator for ranges
let range: Vec<i32> = range_parser::parse_with("-2;0..3;-1;7", ";", "..").unwrap();
assert_eq!(range, vec![-2, 0, 1, 2, 3, -1, 7]);
View range-parser's changelog HERE
range-parser is licensed under the MIT license.
You can read the entire license HERE