//! # Input-related utilities use std::fmt::Debug; use std::fs::read_to_string; use std::ops::Deref; use std::path::Path; use std::str::FromStr; /// Reads file to the string. pub fn file_to_string>(pathname: P) -> String { read_to_string(pathname).expect("Unable to open file") } /// Reads file and returns it as a collection of characters. pub fn file_to_chars>(pathname: P) -> B where B: FromIterator, { read_to_string(pathname) .expect("Unable to open file") .chars() .collect() } /// Reads file and returns a collection of parsed structures. Expects each /// structure on its own line in the file. And `T` needs to implement `FromStr` /// trait. pub fn file_to_structs, T: FromStr>(pathname: P) -> B where B: FromIterator, ::Err: Debug, { strings_to_structs( read_to_string(pathname) .expect("Unable to open files") .lines(), ) } /// Converts iterator over strings to a collection of parsed structures. `T` /// needs to implement `FromStr` trait and its error must derive `Debug` trait. pub fn strings_to_structs(iter: impl Iterator) -> B where B: FromIterator, ::Err: Debug, U: Deref, { iter.map(|line| { line.parse() .expect("Could not parse the struct from the line") }) .collect() } /// Reads file and returns it as a collection of its lines. pub fn file_to_lines>(pathname: P) -> B where B: FromIterator, { read_to_string(pathname) .expect("Unable to open file") .lines() .map(str::to_string) .collect() }