diff --git a/pscand-core/src/logging.rs b/pscand-core/src/logging.rs index 8426961..c6b4cd1 100644 --- a/pscand-core/src/logging.rs +++ b/pscand-core/src/logging.rs @@ -27,7 +27,7 @@ pub enum LogLevel { } impl LogLevel { - pub fn from_str(s: &str) -> Self { + pub fn parse(s: &str) -> Self { match s.to_lowercase().as_str() { "debug" => LogLevel::Debug, "info" => LogLevel::Info, @@ -488,7 +488,7 @@ impl Heartbeat { .unwrap_or(0); if let Some(parent) = self.path.parent() { - fs::create_dir_all(parent)?; + fs::create_dir_all(parent)? } let mut file = fs::File::create(&self.path)?; @@ -528,8 +528,8 @@ impl CrashDetector { pub fn write_state(&self, stats: &RuntimeStats) -> std::io::Result<()> { if let Some(parent) = self.state_file.parent() { - fs::create_dir_all(parent)?; - } + fs::create_dir_all(parent)? + }; let json = serde_json::to_string_pretty(stats).unwrap_or_default(); fs::write(&self.state_file, json) } @@ -560,13 +560,21 @@ impl CrashDetector { return false; } - if let Ok(content) = fs::read_to_string(&self.state_file) { - if let Ok(state) = serde_json::from_str::(&content) { - if let Some(status) = state.get("status").and_then(|v| v.as_str()) { - if status == "stopped" { - return false; - } - } + if let Some(content) = fs::read_to_string(&self.state_file) + .and_then(|c| { + serde_json::from_str::(&c) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) + }) + .ok() + .and_then(|state| { + state + .get("status") + .and_then(|v| v.as_str()) + .map(|s| s.to_string()) + }) + { + if content == "stopped" { + return false; } }