diff --git a/src/diagnostic_status.rs b/src/diagnostic_status.rs index 66ee763..087c288 100644 --- a/src/diagnostic_status.rs +++ b/src/diagnostic_status.rs @@ -121,6 +121,12 @@ impl DiagnosticStatus { return to_return; } + pub fn copy_with_new_name(&self, name: String) -> Self { + let mut to_return = self.clone(); + to_return.name = name; + return to_return; + } + fn clean_name(name: &str) -> String { // Remove prefix "/" let without_prefix = name.strip_prefix("/").unwrap_or(name); diff --git a/src/lib.rs b/src/lib.rs index 5fb6cd5..a87e174 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,10 +9,6 @@ // defined by the Mozilla Public License, v. 2.0. // -// Once all elements added, start at the edge nodes and travel up -// to mark levels of parent nodes. -// Depth first search from top to build new vector to return. - // TODO: Remove anyhow. #![no_std] #![allow(unused_imports)] @@ -24,6 +20,7 @@ mod name_parsing; use anyhow::anyhow; +use crate::alloc::string::ToString; use alloc::borrow::ToOwned; use alloc::collections::BTreeMap; use alloc::collections::VecDeque; @@ -97,6 +94,21 @@ impl DiagnosticGraph { return Ok(Some(cur_base_key)); } + pub fn full_name_from_key(&self, key: &limbo_graph::Key) -> anyhow::Result { + let path_to_key = self.graph.backtrack_from_key(key)?; + let mut full_name = String::new(); + + for curr_key in path_to_key.iter() { + if *curr_key == self.graph.root_key() { + continue; + } + let status = self.value_of(&curr_key)?; + full_name = full_name + "/" + &status.name(); + } + + Ok(full_name) + } + pub fn value_from_name( &self, full_name: &String, @@ -225,6 +237,19 @@ impl DiagnosticGraph { Ok(()) } + + pub fn export_graph(&self) -> anyhow::Result> { + let keys_in_order = self.graph.get_keys_by_depth()?; + + let mut statuses = Vec::::new(); + + for key in keys_in_order.iter() { + let status_at_key = self.value_of(&key)?; + statuses.push(status_at_key.copy_with_new_name(self.full_name_from_key(&key)?)); + } + + Ok(statuses) + } } #[cfg(test)] @@ -329,7 +354,7 @@ mod tests { } #[test] - fn key_from_name() -> anyhow::Result<()> { + fn key_name_connections() -> anyhow::Result<()> { let statuses = vec![ make_a_status_with_name("/a"), make_a_status_with_name("/a/b"), @@ -349,6 +374,9 @@ mod tests { let not_in_graph = graph.key_from_full_name(&"/a/b/c".to_owned())?; assert!(not_in_graph == None); + let name_for_e = graph.full_name_from_key(&4)?; + assert!(name_for_e == "/a/d/e".to_owned()); + Ok(()) }