Move graph error to its own module.

This commit is contained in:
James Pace 2026-07-03 13:40:00 -04:00
parent f5b0a710e0
commit 736798e65a
2 changed files with 34 additions and 22 deletions

32
src/graph_error.rs Normal file
View File

@ -0,0 +1,32 @@
//
// Copyright 2023 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::string::{String, ToString};
#[derive(Debug, Clone)]
pub struct GraphError {
pub msg: String,
}
impl GraphError {
pub fn from_msg(msg: &str) -> Self {
GraphError {
msg: msg.to_string(),
}
}
}
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 GraphError {}

View File

@ -11,12 +11,13 @@
#![no_std]
extern crate alloc;
mod node;
mod graph_error;
use alloc::collections::vec_deque::VecDeque;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
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.
@ -145,27 +146,6 @@ impl<NodeValueT: NodeValue> Graph<NodeValueT> {
}
}
#[derive(Debug, Clone)]
pub struct GraphError {
pub msg: String,
}
impl GraphError {
pub fn from_msg(msg: &str) -> Self {
GraphError {
msg: msg.to_string(),
}
}
}
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 GraphError {}
#[cfg(test)]
mod tests {
use super::*;