feat(input): make parsing functions generic
Make parsing functions generic with regards to the containers they return. Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
19604eafb2
commit
b31e46487a
2 changed files with 21 additions and 12 deletions
|
@ -29,7 +29,7 @@ struct Day03 {
|
||||||
}
|
}
|
||||||
impl Solution<Output1, Output2> for Day03 {
|
impl Solution<Output1, Output2> for Day03 {
|
||||||
fn new<P: AsRef<Path>>(pathname: P) -> Self {
|
fn new<P: AsRef<Path>>(pathname: P) -> Self {
|
||||||
let lines = file_to_lines(pathname);
|
let lines: Vec<String> = file_to_lines(pathname);
|
||||||
|
|
||||||
let symbols = lines
|
let symbols = lines
|
||||||
.iter()
|
.iter()
|
||||||
|
|
31
src/input.rs
31
src/input.rs
|
@ -11,18 +11,23 @@ pub fn file_to_string<P: AsRef<Path>>(pathname: P) -> String {
|
||||||
read_to_string(pathname).expect("Unable to open file")
|
read_to_string(pathname).expect("Unable to open file")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads file and returns it as a vector of characters.
|
/// Reads file and returns it as a collection of characters.
|
||||||
pub fn file_to_chars<P: AsRef<Path>>(pathname: P) -> Vec<char> {
|
pub fn file_to_chars<B, P: AsRef<Path>>(pathname: P) -> B
|
||||||
|
where
|
||||||
|
B: FromIterator<char>,
|
||||||
|
{
|
||||||
read_to_string(pathname)
|
read_to_string(pathname)
|
||||||
.expect("Unable to open file")
|
.expect("Unable to open file")
|
||||||
.chars()
|
.chars()
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads file and returns a vector of parsed structures. Expects each structure
|
/// Reads file and returns a collection of parsed structures. Expects each
|
||||||
/// on its own line in the file. And `T` needs to implement `FromStr` trait.
|
/// structure on its own line in the file. And `T` needs to implement `FromStr`
|
||||||
pub fn file_to_structs<P: AsRef<Path>, T: FromStr>(pathname: P) -> Vec<T>
|
/// trait.
|
||||||
|
pub fn file_to_structs<B, P: AsRef<Path>, T: FromStr>(pathname: P) -> B
|
||||||
where
|
where
|
||||||
|
B: FromIterator<T>,
|
||||||
<T as FromStr>::Err: Debug,
|
<T as FromStr>::Err: Debug,
|
||||||
{
|
{
|
||||||
strings_to_structs(
|
strings_to_structs(
|
||||||
|
@ -32,11 +37,12 @@ where
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts iterator over strings to a vector of parsed structures. `T` needs
|
/// Converts iterator over strings to a collection of parsed structures. `T`
|
||||||
/// to implement `FromStr` trait and its error must derive `Debug` trait.
|
/// needs to implement `FromStr` trait and its error must derive `Debug` trait.
|
||||||
pub fn strings_to_structs<T: FromStr, U>(iter: impl Iterator<Item = U>) -> Vec<T>
|
pub fn strings_to_structs<B, T: FromStr, U>(iter: impl Iterator<Item = U>) -> B
|
||||||
where
|
where
|
||||||
<T as std::str::FromStr>::Err: std::fmt::Debug,
|
B: FromIterator<T>,
|
||||||
|
<T as FromStr>::Err: Debug,
|
||||||
U: Deref<Target = str>,
|
U: Deref<Target = str>,
|
||||||
{
|
{
|
||||||
iter.map(|line| {
|
iter.map(|line| {
|
||||||
|
@ -46,8 +52,11 @@ where
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads file and returns it as a vector of its lines.
|
/// Reads file and returns it as a collection of its lines.
|
||||||
pub fn file_to_lines<P: AsRef<Path>>(pathname: P) -> Vec<String> {
|
pub fn file_to_lines<B, P: AsRef<Path>>(pathname: P) -> B
|
||||||
|
where
|
||||||
|
B: FromIterator<String>,
|
||||||
|
{
|
||||||
read_to_string(pathname)
|
read_to_string(pathname)
|
||||||
.expect("Unable to open file")
|
.expect("Unable to open file")
|
||||||
.lines()
|
.lines()
|
||||||
|
|
Loading…
Reference in a new issue