use serde::Deserialize;
#[derive(Debug, Deserialize, Clone)]
pub struct InputMessage {
pub t: f64,
pub dt: f64,
pub z: Option<Vec<f64>>,
}
#[derive(Debug, Clone)]
pub enum InputError {
InvalidDt { dt: f64, reason: String },
WrongZLength { got: usize, expected: usize },
MalformedInput { reason: String },
}
impl InputMessage {
pub fn validate(&self, expected_m: usize) -> Result<(), InputError> {
if !self.dt.is_finite() {
return Err(InputError::InvalidDt {
dt: self.dt,
reason: "non-finite value".to_string(),
});
}
if self.dt <= 0.0 {
return Err(InputError::InvalidDt {
dt: self.dt,
reason: format!("must be positive, got {}", self.dt),
});
}
if let Some(ref z) = self.z {
if z.len() != expected_m {
return Err(InputError::WrongZLength {
got: z.len(),
expected: expected_m,
});
}
}
Ok(())
}
}
impl std::fmt::Display for InputError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
InputError::InvalidDt { dt: _, reason } => {
write!(f, "invalid dt: {}", reason)
}
InputError::WrongZLength { got, expected } => {
write!(f, "z has {} elements, expected {}", got, expected)
}
InputError::MalformedInput { reason } => {
write!(f, "malformed input: {}", reason)
}
}
}
}