From 00000010c8abf8a7e5df4b810c94835be34b5abd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Thu, 30 Nov 2023 21:46:12 +0100 Subject: [PATCH] feat: prepare basic parsers --- README.md | 2 ++ src/lib.rs | 3 +++ src/parsers.rs | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 src/parsers.rs diff --git a/README.md b/README.md index 1b82e1a..58889e2 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ # Advent-of-Code 2023 *This is a dumbed down version of [fspoettel/advent-of-code-rust](https://github.com/fspoettel/advent-of-code-rust) with some extra features* +This project targets nightly build of rust, since some preview features are enabled + ## Project overview ### Project structure diff --git a/src/lib.rs b/src/lib.rs index 612b5b9..f461881 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,4 @@ +#![feature(pattern)] + pub mod template; +pub mod parsers; diff --git a/src/parsers.rs b/src/parsers.rs new file mode 100644 index 000000000..1bc56fc --- /dev/null +++ b/src/parsers.rs @@ -0,0 +1,20 @@ +use std::str::{pattern::Pattern, FromStr}; + +pub fn to_vec<'a, T, P>(s: &'a str, pat: P) -> Vec +where + T: FromStr, + P: Pattern<'a>, +{ + s.split(pat).filter_map(|x| x.parse().ok()).collect() +} + +pub fn to_vec_map<'a, T, U, P>(s: &'a str, pat: P, func: impl FnMut(T) -> U) -> Vec +where + T: FromStr, + P: Pattern<'a>, +{ + s.split(pat) + .filter_map(|x| x.parse().ok()) + .map(func) + .collect() +}