aoc-2021/d07/src/utils.rs

37 lines
954 B
Rust

use std::fmt;
use std::str::FromStr;
/// Returns a vector of items parsed from a delimited string
///
/// # Arguments
///
/// * `input` input string to split and parse from
/// * `delimiter` item delimiter
pub fn parse_delimited<T>(input: &str, delimiter: &str) -> Vec<T>
where
T: FromStr,
<T as FromStr>::Err: fmt::Debug,
{
input
.split(delimiter)
.map(|v| {
v.trim()
.parse::<T>()
.expect(format!("Unable to parse '{}' from text '{}'", v, input).as_str())
})
.collect()
}
/// Returns a string with each element of the vector formatted and joined
///
/// # Arguments
///
/// * `join_str` the string to use to join the vector elements
/// * `v` the vector with elements to join
pub fn join_vec<T: std::fmt::Display>(join_str: &str, v: &Vec<T>) -> String {
v.iter()
.map(|p| format!("{}", p))
.collect::<Vec<String>>()
.join(join_str)
}