Factor out from_file_data; hopefully this doesn't end up being the only shared thing.

This commit is contained in:
2021-12-03 07:35:29 -08:00
parent f7fc8feb03
commit 27569672e5
2 changed files with 12 additions and 10 deletions

View File

@@ -1,3 +1,4 @@
use advent2021::from_file_data;
use std::num; use std::num;
use std::str::FromStr; use std::str::FromStr;
use thiserror::Error; use thiserror::Error;
@@ -37,16 +38,6 @@ impl FromStr for Command {
} }
} }
fn from_file_data<T: FromStr>(filedata: &str) -> Result<Vec<T>, T::Err> {
let mut retval = Vec::new();
for line in filedata.lines() {
retval.push(T::from_str(line)?);
}
Ok(retval)
}
#[test] #[test]
fn command_parsing() { fn command_parsing() {
assert_eq!(Ok(Command::Forward(5)), Command::from_str("forward 5")); assert_eq!(Ok(Command::Forward(5)), Command::from_str("forward 5"));

11
src/lib.rs Normal file
View File

@@ -0,0 +1,11 @@
use std::str::FromStr;
pub fn from_file_data<T: FromStr>(filedata: &str) -> Result<Vec<T>, T::Err> {
let mut retval = Vec::new();
for line in filedata.lines() {
retval.push(T::from_str(line)?);
}
Ok(retval)
}