From 000000208a00dfa2953b8b42dfb09596b1c94243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Sun, 1 Dec 2024 22:49:58 +0100 Subject: [PATCH] feat: add basic parser functions --- src/lib.rs | 3 +++ src/parsers.rs | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/parsers.rs diff --git a/src/lib.rs b/src/lib.rs index 612b5b9..d103939 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,4 @@ +#![feature(pattern)] + +pub mod parsers; pub mod template; diff --git a/src/parsers.rs b/src/parsers.rs new file mode 100644 index 000000000..a3f8d44 --- /dev/null +++ b/src/parsers.rs @@ -0,0 +1,20 @@ +use std::str::{pattern::Pattern, FromStr}; + +pub fn to_vec(s: &str, pat: P) -> Vec +where + T: FromStr, + P: Pattern, +{ + s.split(pat).filter_map(|x| x.parse().ok()).collect() +} + +pub fn to_vec_map(s: &str, pat: P, func: impl FnMut(T) -> U) -> Vec +where + T: FromStr, + P: Pattern, +{ + s.split(pat) + .filter_map(|x| x.parse().ok()) + .map(func) + .collect() +}