feat: add basic parser functions

This commit is contained in:
Matej Janezic 2024-12-01 22:49:58 +01:00
parent 00000010b7
commit 000000208a
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
2 changed files with 23 additions and 0 deletions

View File

@ -1 +1,4 @@
#![feature(pattern)]
pub mod parsers;
pub mod template;

20
src/parsers.rs Normal file
View File

@ -0,0 +1,20 @@
use std::str::{pattern::Pattern, FromStr};
pub fn to_vec<T, P>(s: &str, pat: P) -> Vec<T>
where
T: FromStr,
P: Pattern,
{
s.split(pat).filter_map(|x| x.parse().ok()).collect()
}
pub fn to_vec_map<T, U, P>(s: &str, pat: P, func: impl FnMut(T) -> U) -> Vec<U>
where
T: FromStr,
P: Pattern,
{
s.split(pat)
.filter_map(|x| x.parse().ok())
.map(func)
.collect()
}