Refactor out diagnostic_status.
This commit is contained in:
parent
f80437e46d
commit
4cf54d18c3
|
|
@ -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<String, String>,
|
||||
}
|
||||
|
||||
impl DiagnosticStatus {
|
||||
pub fn new(
|
||||
level: DiagnosticLevel,
|
||||
name: String,
|
||||
message: String,
|
||||
hardware_id: String,
|
||||
values: BTreeMap<String, String>,
|
||||
) -> 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::<String, String>::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<String> {
|
||||
self.values.keys().cloned().collect()
|
||||
}
|
||||
|
||||
pub fn value(&self, key: &str) -> Option<String> {
|
||||
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<String> {
|
||||
if self.name_is_basic() {
|
||||
return Vec::<String>::new();
|
||||
}
|
||||
|
||||
let split: Vec<String> = 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<String> = 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::<String, String>::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::<String, String>::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");
|
||||
}
|
||||
}
|
||||
165
src/lib.rs
165
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<String, String>,
|
||||
}
|
||||
|
||||
impl DiagnosticStatus {
|
||||
pub fn new(
|
||||
level: DiagnosticLevel,
|
||||
name: String,
|
||||
message: String,
|
||||
hardware_id: String,
|
||||
values: BTreeMap<String, String>,
|
||||
) -> 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::<String, String>::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<String> {
|
||||
self.values.keys().cloned().collect()
|
||||
}
|
||||
|
||||
pub fn value(&self, key: &str) -> Option<String> {
|
||||
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<String> {
|
||||
if self.name_is_basic() {
|
||||
return Vec::<String>::new();
|
||||
}
|
||||
|
||||
let split: Vec<String> = 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<String> = 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::<String, String>::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::<String, String>::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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue