use std::fmt;
use std::str::FromStr;
use std::time::Duration;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Fsync {
Always,
Interval(Duration),
Never,
}
impl Default for Fsync {
fn default() -> Self {
Fsync::Interval(Duration::from_millis(1000))
}
}
impl fmt::Display for Fsync {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Fsync::Always => write!(f, "always"),
Fsync::Interval(d) => write!(f, "interval:{}", d.as_millis()),
Fsync::Never => write!(f, "never"),
}
}
}
impl FromStr for Fsync {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"always" => Ok(Fsync::Always),
"never" => Ok(Fsync::Never),
_ if s.starts_with("interval:") => {
let ms = s["interval:".len()..]
.parse::<u64>()
.map_err(|_| "Invalid interval for 'interval' fsync policy".to_string())?;
if ms == 0 {
return Err("Interval must be >= 1 ms for 'interval' fsync policy".to_string());
}
Ok(Fsync::Interval(Duration::from_millis(ms)))
}
_ => Err("Invalid fsync policy: expected always, interval:<ms>, or never".to_string()),
}
}
}