Debian Codegraph @

src/parser/utils.rs

raw symbols SCIP
1 //! Utility functions for parsing rrule input.
2 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#;
3
4 /// Attempts to convert a comma separated `&str` to a `Vec<T>` of unique and sorted values.
5 /// The function accepts a closure which can be used to validate the values which are parsed.
6 pub(craterust-analyzer cargo rrule 0.14.0 crate/) fn parse_str_to_vecrust-analyzer cargo rrule 0.14.0 parser/utils/parse_str_to_vec().<T: FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr# + Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord# + PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq# + Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy#, F: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(T) -> bool>(
7 val: &str,
8 accept: F,
9 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
10 if val.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() {
11 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]);
12 }
13
14 let mut parsed_vals = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
15 for val in val.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(',') {
16 let parsed_val = val.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| val.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
17 if accept(parsed_val) {
18 parsed_vals.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(parsed_val);
19 } else {
20 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(val.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
21 }
22 }
23
24 parsed_vals.sortrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort().();
25 parsed_vals.deduprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]dedup().();
26
27 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(parsed_vals)
28 }
29
30 #[cfg(test)]
31 mod testsrust-analyzer cargo rrule 0.14.0 parser/utils/tests/ {
32 use superrust-analyzer cargo rrule 0.14.0 parser/utils/::parse_str_to_vecrust-analyzer cargo rrule 0.14.0 parser/utils/parse_str_to_vec().;
33
34 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
35 fn parses_str_to_vecrust-analyzer cargo rrule 0.14.0 parser/utils/tests/parses_str_to_vec().() {
36 const INVALID_VALUErust-analyzer cargo rrule 0.14.0 parser/utils/tests/INVALID_VALUE.: usize = 17;
37 let accept_fn = |val: usize| val rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().17;
38 let tests = [
39 ("", Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![])),
40 ("1,2,3", Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1, 2, 3])),
41 ("3,2,1", Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1, 2, 3])),
42 ("3,2,1,4", Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1, 2, 3, 4])),
43 ("3,2,1,4,3,4,4,3,1", Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1, 2, 3, 4])),
44 (
45 &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("14,15,16,{},18", INVALID_VALUErust-analyzer cargo rrule 0.14.0 parser/utils/tests/INVALID_VALUE.),
46 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}", INVALID_VALUErust-analyzer cargo rrule 0.14.0 parser/utils/tests/INVALID_VALUE.)),
47 ),
48 ];
49 for (input, expected_output) in tests {
50 let output = parse_str_to_vecrust-analyzer cargo rrule 0.14.0 parser/utils/parse_str_to_vec().(input, accept_fn);
51 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(output, expected_output);
52 }
53 }
54 }