Add docs.

This commit is contained in:
James Pace 2026-07-03 16:11:51 -04:00
parent df82bc45e5
commit 5edc68f9ad
1 changed files with 26 additions and 4 deletions

View File

@ -31,32 +31,43 @@ pub use crate::error::*;
use crate::diagnostic_node::*; use crate::diagnostic_node::*;
use crate::name_parsing::*; use crate::name_parsing::*;
/// A graph of diagnostic statuses, following the general idea of
/// ros aggregated diagnostics.
///
/// Diagnostics are stored in a tree by their namme.
/// Their levels can be reconciled, and the individual
/// statuses queried.
pub struct DiagnosticGraph { pub struct DiagnosticGraph {
graph: limbo_graph::Graph<DiagnosticNode>, graph: limbo_graph::Graph<DiagnosticNode>,
} }
impl DiagnosticGraph { impl DiagnosticGraph {
/// Generate a empty graph.
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
graph: limbo_graph::Graph::<DiagnosticNode>::new(DiagnosticNode::Root), graph: limbo_graph::Graph::<DiagnosticNode>::new(DiagnosticNode::Root),
} }
} }
pub fn root(&self) -> limbo_graph::Key { /// Get the value of a specific element of the graph.
return self.graph.root_key(); /// Will error if the key is not in the graph, or if the key
} /// is the root, which doesn't have a value.
pub fn value_of(&self, key: &limbo_graph::Key) -> Result<DiagnosticStatus> { pub fn value_of(&self, key: &limbo_graph::Key) -> Result<DiagnosticStatus> {
let graph_value = self.graph.value_of(key)?; let graph_value = self.graph.value_of(key)?;
let result = graph_value.value()?; let result = graph_value.value()?;
Ok(result.clone()) Ok(result.clone())
} }
/// Find all the children of key as keys.
/// Will error if key is not in the graph.
pub fn children_of(&self, key: &limbo_graph::Key) -> Result<Vec<limbo_graph::Key>> { pub fn children_of(&self, key: &limbo_graph::Key) -> Result<Vec<limbo_graph::Key>> {
let result = self.graph.children_of(key)?; let result = self.graph.children_of(key)?;
Ok(result) Ok(result)
} }
/// Given a key, find the child of that key that has the provided name.
/// The provided name must be basic (have no slashes).
/// Will return None if there are no children with that name.
pub fn child_of_which_has_name( pub fn child_of_which_has_name(
&self, &self,
parent: &limbo_graph::Key, parent: &limbo_graph::Key,
@ -75,6 +86,8 @@ impl DiagnosticGraph {
return Ok(None); return Ok(None);
} }
/// Find the key of the item in the graph with the provided full name
/// (i.e. the name has slashes.)
pub fn key_from_full_name(&self, full_name: &String) -> Result<Option<limbo_graph::Key>> { pub fn key_from_full_name(&self, full_name: &String) -> Result<Option<limbo_graph::Key>> {
let name_as_vec = split_name(&full_name); let name_as_vec = split_name(&full_name);
@ -90,6 +103,7 @@ impl DiagnosticGraph {
return Ok(Some(cur_base_key)); return Ok(Some(cur_base_key));
} }
/// Provide the full name for a given key. (i.e. the name will have slashes).
pub fn full_name_from_key(&self, key: &limbo_graph::Key) -> Result<String> { pub fn full_name_from_key(&self, key: &limbo_graph::Key) -> Result<String> {
let path_to_key = self.graph.backtrack_from_key(key)?; let path_to_key = self.graph.backtrack_from_key(key)?;
let mut full_name = String::new(); let mut full_name = String::new();
@ -105,6 +119,7 @@ impl DiagnosticGraph {
Ok(full_name) Ok(full_name)
} }
/// Provide the value of a status from the full name.
pub fn value_from_name( pub fn value_from_name(
&self, &self,
full_name: &String, full_name: &String,
@ -119,6 +134,9 @@ impl DiagnosticGraph {
return Ok(Some((value, key))); return Ok(Some((value, key)));
} }
/// Add a status to the graph.
/// The name of the status will be used to determine where in the
/// graph the status belongs.
pub fn add_status(&mut self, status: DiagnosticStatus) -> Result<()> { pub fn add_status(&mut self, status: DiagnosticStatus) -> Result<()> {
// If I'm basic I just need to be added as a child of the root. // If I'm basic I just need to be added as a child of the root.
if status.name_is_basic() { if status.name_is_basic() {
@ -191,6 +209,9 @@ impl DiagnosticGraph {
Ok(()) Ok(())
} }
/// Add all the statuses in the provided vector.
/// The name of the statuses will be used to determine where in the
/// graph the statuses belong.
pub fn add_status_vec(&mut self, statuses: &Vec<DiagnosticStatus>) -> Result<()> { pub fn add_status_vec(&mut self, statuses: &Vec<DiagnosticStatus>) -> Result<()> {
for status in statuses { for status in statuses {
self.add_status(status.clone())?; self.add_status(status.clone())?;
@ -198,6 +219,7 @@ impl DiagnosticGraph {
Ok(()) Ok(())
} }
/// Update the levels for parent nodes based on the level of their children.
pub fn reconcile_levels(&mut self) -> Result<()> { pub fn reconcile_levels(&mut self) -> Result<()> {
let leaf_keys = self.graph.find_leaf_keys()?; let leaf_keys = self.graph.find_leaf_keys()?;