YMV7RPQ5TFBETNHRMS26MGHJXEAYRMIF4F7Z6ITGRCG65TOGSNDAC
}
}
/// Errors. TODO: Should be moved to its own module.
#[derive(Debug)]
pub enum Error {
/// Tried to construct a `RegularTimeSeries` from an irregular `TimeSeries`.
NotRegular,
/// The `TimeSeries` is empty.
NoPoint1s,
/// The `TimeSeries` has only one datapoint.
OnePoint1,
/// Could not parse date.
ParseErr,
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
pub fn from_csv(path: &str) -> TimeSeries<1> {
let s = fs::read_to_string(path).unwrap();
let v = s.lines().map(|line| {
DatePoint::<1>::new(
MonthlyDate::ym(
line[..4].parse().unwrap(),
line[5..7].parse().unwrap(),
),
[line[12..].parse::<f32>().unwrap()],
)
}).collect::<Vec<DatePoint::<1>>>();
TimeSeries::new(v)
pub fn from_csv(path: &str) -> Result<TimeSeries<1>, Error> {
let s = match fs::read_to_string(path) {
Ok(s) => s,
Err(_) => {
println!("Could not find file [{}].", path);
panic!()
},
};
let mut v: Vec<DatePoint<1>> = Vec::new();
for (i, line) in s.lines().enumerate() {
let year = match line[..4].parse() {
Ok(y) => y,
Err(_) => {
return Err(failed_to_parse_csv_date(path, i + 1, line))
},
};
let month = match line[5..7].parse() {
Ok(m) => m,
Err(_) => {
return Err(failed_to_parse_csv_date(path, i + 1, line))
},
};
let value = match line[12..].parse::<f32>() {
Ok(value) => value,
Err(_) => {
return Err(failed_to_parse_csv_value(path, i + 1, line))
},
};
let dp = DatePoint::<1>::new(
MonthlyDate::ym(year, month),
[value],
);
v.push(dp);
}
Ok(TimeSeries::new(v))
pub fn deserialize<'de, D, T, const N: usize>(deserializer: D) -> Result<[T; N], D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
deserializer.deserialize_tuple(N, ArrayVisitor::<T, N>(PhantomData))
}
// pub fn deserialize<'de, D, T, const N: usize>(deserializer: D) -> Result<[T; N], D::Error>
// where
// D: Deserializer<'de>,
// T: Deserialize<'de>,
// {
// deserializer.deserialize_tuple(N, ArrayVisitor::<T, N>(PhantomData))
// }
use std::fmt;
/// A time_series error.
#[derive(Debug)]
pub struct Error(String);
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
///
pub fn expected_regular_time_series() -> Error {
Error(format!(
"[time_series:01] Expected regular time-series."
))
}
///
pub fn failed_to_parse_csv_date(file: &str, line_num: usize, line: &str) -> Error {
Error(format!(
"[time_series:02] Line: {} file: {}, . Failed to parse date on line [{}].",
line_num,
file,
line,
))
}
///
pub fn failed_to_parse_csv_value(file: &str, line_num: usize, line: &str) -> Error {
Error(format!(
"[time_series:03] Line: {} file: {}. Failed to parse value on line [{}].",
line_num,
file,
line,
))
}
pub fn time_series_has_only_one_point() -> Error {
Error(format!(
"[time_series:04] Time-series has only one point.",
))
}
pub fn time_series_is_empty() -> Error {
Error(format!(
"[time_series:05] Time-series is empty.",
))
}