From 4cf54d18c3d849dc1c25d98dd7c1ae1e7aeed15d Mon Sep 17 00:00:00 2001 From: James Pace Date: Sat, 20 Jun 2026 09:26:01 -0400 Subject: [PATCH] Refactor out diagnostic_status. --- src/diagnostic_status.rs | 183 +++++++++++++++++++++++++++++++++++++++ src/lib.rs | 165 +---------------------------------- 2 files changed, 186 insertions(+), 162 deletions(-) create mode 100644 src/diagnostic_status.rs diff --git a/src/diagnostic_status.rs b/src/diagnostic_status.rs new file mode 100644 index 0000000..c87a32f --- /dev/null +++ b/src/diagnostic_status.rs @@ -0,0 +1,183 @@ +// +// Copyright 2026 James Pace +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. +// +// This Source Code Form is "Incompatible With Secondary Licenses", as +// defined by the Mozilla Public License, v. 2.0. +// +use alloc::borrow::ToOwned; +use alloc::collections::BTreeMap; +use alloc::string::String; +use alloc::vec::Vec; + +#[derive(Clone, PartialEq)] +pub enum DiagnosticLevel { + OK, + WARN, + ERROR, + STALE, +} + +#[derive(Clone)] +pub struct DiagnosticStatus { + level: DiagnosticLevel, + name: String, + message: String, + hardware_id: String, + values: BTreeMap, +} + +impl DiagnosticStatus { + pub fn new( + level: DiagnosticLevel, + name: String, + message: String, + hardware_id: String, + values: BTreeMap, + ) -> Self { + Self { + level: level, + name: name, + message: message, + hardware_id: hardware_id, + values: values, + } + } + + pub fn from_name(name: String) -> Self { + let level = DiagnosticLevel::STALE; + let message = ""; + let hardware_id = ""; + let values = BTreeMap::::new(); + + Self::new( + level, + name, + message.to_owned(), + hardware_id.to_owned(), + values, + ) + } + + pub fn level(&self) -> DiagnosticLevel { + self.level.clone() + } + + pub fn name(&self) -> String { + self.name.clone() + } + + pub fn message(&self) -> String { + self.message.clone() + } + + pub fn hardware_id(&self) -> String { + self.hardware_id.clone() + } + + pub fn keys(&self) -> Vec { + self.values.keys().cloned().collect() + } + + pub fn value(&self, key: &str) -> Option { + let value = self.values.get(key); + if value.is_none() { + return None; + } + return Some(value.unwrap().clone()); + } + + pub fn name_is_basic(&self) -> bool { + // A basic name is one that doesn't have slashes in it other than the + // first character. + let is_not_basic = self + .name + .strip_prefix("/") + .unwrap_or(&self.name) + .contains("/"); + return !is_not_basic; + } + + pub fn get_parent_names(&self) -> Vec { + if self.name_is_basic() { + return Vec::::new(); + } + + let split: Vec = self + .name() + .split("/") + .map(|x| x.to_owned()) + .filter(|x| !x.is_empty()) + .collect(); + return split + .iter() + .take(split.len() - 1) + .map(|x| x.to_owned()) + .collect(); + } + + pub fn get_child_name(&self) -> String { + if self.name_is_basic() { + return self.name(); + } + let split: Vec = self.name().split("/").map(|x| x.to_owned()).collect(); + return split.last().unwrap().clone(); + } + + pub fn copy_with_child_name(&self) -> Self { + let mut to_return: Self = self.clone(); + to_return.name = self.get_child_name(); + return to_return; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn basic_diagnostic_creation() { + let level = DiagnosticLevel::OK; + let name = "/a/b/c"; + let message = "I'm ok"; + let hardware_id = ""; + let values = BTreeMap::::new(); + + let diag_status = DiagnosticStatus::new( + level, + name.to_owned(), + message.to_owned(), + hardware_id.to_owned(), + values, + ); + assert!(diag_status.level() == DiagnosticLevel::OK); + } + + #[test] + fn diagnostic_name_parsing() { + let level = DiagnosticLevel::OK; + let name = "/a/b/c"; + let message = "I'm ok"; + let hardware_id = ""; + let values = BTreeMap::::new(); + + let diag_status = DiagnosticStatus::new( + level, + name.to_owned(), + message.to_owned(), + hardware_id.to_owned(), + values, + ); + + assert!(!diag_status.name_is_basic()); + + let parent_names = diag_status.get_parent_names(); + let child_name = diag_status.get_child_name(); + assert!(parent_names[0] == "a"); + assert!(parent_names[1] == "b"); + assert!(child_name == "c"); + } +} diff --git a/src/lib.rs b/src/lib.rs index 21844d6..6d9b1ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,8 @@ #![allow(unused_imports)] extern crate alloc; +mod diagnostic_status; + use anyhow::anyhow; use alloc::borrow::ToOwned; @@ -32,126 +34,8 @@ use alloc::collections::VecDeque; use alloc::string::String; use alloc::vec::Vec; -#[derive(Clone, PartialEq)] -pub enum DiagnosticLevel { - OK, - WARN, - ERROR, - STALE, -} +pub use crate::diagnostic_status::*; -#[derive(Clone)] -pub struct DiagnosticStatus { - level: DiagnosticLevel, - name: String, - message: String, - hardware_id: String, - values: BTreeMap, -} - -impl DiagnosticStatus { - pub fn new( - level: DiagnosticLevel, - name: String, - message: String, - hardware_id: String, - values: BTreeMap, - ) -> Self { - Self { - level: level, - name: name, - message: message, - hardware_id: hardware_id, - values: values, - } - } - - pub fn from_name(name: String) -> Self { - let level = DiagnosticLevel::STALE; - let message = ""; - let hardware_id = ""; - let values = BTreeMap::::new(); - - Self::new( - level, - name, - message.to_owned(), - hardware_id.to_owned(), - values, - ) - } - - pub fn level(&self) -> DiagnosticLevel { - self.level.clone() - } - - pub fn name(&self) -> String { - self.name.clone() - } - - pub fn message(&self) -> String { - self.message.clone() - } - - pub fn hardware_id(&self) -> String { - self.hardware_id.clone() - } - - pub fn keys(&self) -> Vec { - self.values.keys().cloned().collect() - } - - pub fn value(&self, key: &str) -> Option { - let value = self.values.get(key); - if value.is_none() { - return None; - } - return Some(value.unwrap().clone()); - } - - pub fn name_is_basic(&self) -> bool { - // A basic name is one that doesn't have slashes in it other than the - // first character. - let is_not_basic = self - .name - .strip_prefix("/") - .unwrap_or(&self.name) - .contains("/"); - return !is_not_basic; - } - - pub fn get_parent_names(&self) -> Vec { - if self.name_is_basic() { - return Vec::::new(); - } - - let split: Vec = self - .name() - .split("/") - .map(|x| x.to_owned()) - .filter(|x| !x.is_empty()) - .collect(); - return split - .iter() - .take(split.len() - 1) - .map(|x| x.to_owned()) - .collect(); - } - - pub fn get_child_name(&self) -> String { - if self.name_is_basic() { - return self.name(); - } - let split: Vec = self.name().split("/").map(|x| x.to_owned()).collect(); - return split.last().unwrap().clone(); - } - - pub fn copy_with_child_name(&self) -> Self { - let mut to_return: Self = self.clone(); - to_return.name = self.get_child_name(); - return to_return; - } -} // TODO: Make this not public. #[derive(Clone)] pub enum DiagnosticNode { @@ -262,49 +146,6 @@ impl DiagnosticGraph { mod tests { use super::*; - #[test] - fn basic_diagnostic_creation() { - let level = DiagnosticLevel::OK; - let name = "/a/b/c"; - let message = "I'm ok"; - let hardware_id = ""; - let values = BTreeMap::::new(); - - let diag_status = DiagnosticStatus::new( - level, - name.to_owned(), - message.to_owned(), - hardware_id.to_owned(), - values, - ); - assert!(diag_status.level() == DiagnosticLevel::OK); - } - - #[test] - fn diagnostic_name_parsing() { - let level = DiagnosticLevel::OK; - let name = "/a/b/c"; - let message = "I'm ok"; - let hardware_id = ""; - let values = BTreeMap::::new(); - - let diag_status = DiagnosticStatus::new( - level, - name.to_owned(), - message.to_owned(), - hardware_id.to_owned(), - values, - ); - - assert!(!diag_status.name_is_basic()); - - let parent_names = diag_status.get_parent_names(); - let child_name = diag_status.get_child_name(); - assert!(parent_names[0] == "a"); - assert!(parent_names[1] == "b"); - assert!(child_name == "c"); - } - #[test] fn add_one_to_graph() -> anyhow::Result<()> { let level = DiagnosticLevel::OK;