From c9a5b0c526c6640f4a7af91c02c71644c234ba0d Mon Sep 17 00:00:00 2001 From: James Pace Date: Fri, 3 Jul 2026 14:31:45 -0400 Subject: [PATCH] Make error an enum. --- src/{graph_error.rs => error.rs} | 26 +++++++++++++++----------- src/lib.rs | 30 +++++++++++++++--------------- 2 files changed, 30 insertions(+), 26 deletions(-) rename src/{graph_error.rs => error.rs} (62%) diff --git a/src/graph_error.rs b/src/error.rs similarity index 62% rename from src/graph_error.rs rename to src/error.rs index aea6588..6906965 100644 --- a/src/graph_error.rs +++ b/src/error.rs @@ -11,22 +11,26 @@ use alloc::string::{String, ToString}; #[derive(Debug, Clone)] -pub struct GraphError { - pub msg: String, +pub enum Error { + Msg(String), } -impl GraphError { +impl Error { pub fn from_msg(msg: &str) -> Self { - GraphError { - msg: msg.to_string(), + Error::Msg(msg.to_string()) + } +} + +impl core::fmt::Display for Error { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { + match self { + Error::Msg(msg) => { + write!(f, "Error manipulating graph: {}", msg) + } } } } -impl core::fmt::Display for GraphError { - fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { - write!(f, "Error manipulating graph: {}", self.msg) - } -} +impl core::error::Error for Error {} -impl core::error::Error for GraphError {} +pub type Result = core::result::Result; diff --git a/src/lib.rs b/src/lib.rs index 67f9aff..4c5fef3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,14 +10,14 @@ // #![no_std] extern crate alloc; +mod error; mod node; -mod graph_error; use alloc::collections::vec_deque::VecDeque; use alloc::vec; use alloc::vec::Vec; +pub use error::*; pub use node::*; -pub use graph_error::*; /// A generic graph type holding values connected to other values. /// Values can be added to the graph, but not removed. @@ -39,10 +39,10 @@ impl Graph { /// Add a child wth value `val` to the parent with Key `parent`. /// If the parent key is not in the graph, returns an error. /// Returns a result with the key of the new node or an error. - pub fn add(&mut self, val: NodeValueT, parent: Key) -> Result { + pub fn add(&mut self, val: NodeValueT, parent: Key) -> Result { // Make sure parent is valid. if parent >= self.nodes.len() { - return Err(GraphError::from_msg("Parent node not in graph.")); + return Err(Error::from_msg("Parent node not in graph.")); } // Add new node to graph, get it's key. let new_node = Node::new(val, Some(parent)); @@ -55,35 +55,35 @@ impl Graph { /// Replace the value of `key` with value `value`. /// `key` must already exist in the graph, and no connections will be modified. - pub fn replace_value_of(&mut self, key: &Key, value: NodeValueT) -> Result<(), GraphError> { + pub fn replace_value_of(&mut self, key: &Key, value: NodeValueT) -> Result<()> { if let Some(node) = self.nodes.get_mut(*key) { node.set_value(value); return Ok(()); } - Err(GraphError::from_msg("Can't set value of invalid key.")) + Err(Error::from_msg("Can't set value of invalid key.")) } /// Get the value of key `key` if the key is valid. - pub fn value_of(&self, key: &Key) -> Result { + pub fn value_of(&self, key: &Key) -> Result { if key >= &self.nodes.len() { - return Err(GraphError::from_msg("Can't get value of invalid key.")); + return Err(Error::from_msg("Can't get value of invalid key.")); } Ok(self.nodes[*key].value()) } /// Get the children (as a list of keys) of key `key` if the key is valid. - pub fn children_of(&self, key: &Key) -> Result, GraphError> { + pub fn children_of(&self, key: &Key) -> Result> { if key >= &self.nodes.len() { - return Err(GraphError::from_msg("Can't get children of invalid key.")); + return Err(Error::from_msg("Can't get children of invalid key.")); } Ok(self.nodes[*key].children()) } /// Get the parent of key `key` if the key is valid. /// Will return None if the node at `key` as no parent (i.e. is the root node). - pub fn parent_of(&self, key: &Key) -> Result, GraphError> { + pub fn parent_of(&self, key: &Key) -> Result> { if key >= &self.nodes.len() { - return Err(GraphError::from_msg("Can't get parent of invalid key.")); + return Err(Error::from_msg("Can't get parent of invalid key.")); } Ok(self.nodes[*key].parent()) } @@ -96,7 +96,7 @@ impl Graph { /// Find all nodes that are leaf nodes. /// A leaf node is one that doesn't have any children. - pub fn find_leaf_keys(&self) -> Result, GraphError> { + pub fn find_leaf_keys(&self) -> Result> { let mut leaf_keys = Vec::::new(); for key in 0..self.nodes.len() { @@ -111,7 +111,7 @@ impl Graph { /// Return all keys ordered like you were doing /// a depth first search. - pub fn get_keys_by_depth(&self) -> Result, GraphError> { + pub fn get_keys_by_depth(&self) -> Result> { let mut visited_keys = Vec::::new(); let mut stack = Vec::::new(); @@ -131,7 +131,7 @@ impl Graph { /// Given a key, return the vec of keys connecting that key to the root. /// Order is root->key inclusive. - pub fn backtrack_from_key(&self, key: &Key) -> Result, GraphError> { + pub fn backtrack_from_key(&self, key: &Key) -> Result> { let mut curr_key = key.clone(); let mut deque = VecDeque::::new(); deque.push_front(curr_key.clone());