Get rid of anyhow.
This commit is contained in:
1 parent
1b2411436d
commit
766cd48c1a
5 files changed
+107
-34
No files matched your search
@@ -9,7 +9,7 @@
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
//
|
||||
use crate::DiagnosticStatus;
|
||||
use anyhow::anyhow;
|
||||
use crate::error::{Error, Result};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum DiagnosticNode {
|
||||
@@ -30,10 +30,10 @@ impl DiagnosticNode {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn value(&self) -> anyhow::Result<&DiagnosticStatus> {
|
||||
pub fn value(&self) -> Result<&DiagnosticStatus> {
|
||||
match self {
|
||||
Self::DiagnosticStatus(value) => Ok(value),
|
||||
_ => Err(anyhow!("Can't get value of root key.")),
|
||||
_ => Err(Error::from_msg("Can't get value of root key.")),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// 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::string::{String, ToString};
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("GraphError: {0}")]
|
||||
GraphError(#[from] limbo_graph::Error),
|
||||
#[error("Error from j7s_diagnostics: {0}")]
|
||||
Msg(String),
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn from_msg(msg: &str) -> Self {
|
||||
Error::Msg(msg.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub type Result<T, E = Error> = core::result::Result<T, E>;
|
||||
+19
-23
@@ -9,25 +9,24 @@
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
//
|
||||
|
||||
// TODO: Remove anyhow.
|
||||
#![no_std]
|
||||
#![allow(unused_imports)]
|
||||
extern crate alloc;
|
||||
|
||||
mod diagnostic_node;
|
||||
mod diagnostic_status;
|
||||
mod error;
|
||||
mod name_parsing;
|
||||
|
||||
use anyhow::anyhow;
|
||||
|
||||
use crate::alloc::string::ToString;
|
||||
use alloc::borrow::ToOwned;
|
||||
use alloc::collections::BTreeMap;
|
||||
use alloc::collections::VecDeque;
|
||||
use alloc::string::String;
|
||||
use alloc::string::ToString;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
pub use crate::diagnostic_status::*;
|
||||
pub use crate::error::*;
|
||||
|
||||
use crate::diagnostic_node::*;
|
||||
use crate::name_parsing::*;
|
||||
@@ -47,13 +46,13 @@ impl DiagnosticGraph {
|
||||
return self.graph.root_key();
|
||||
}
|
||||
|
||||
pub fn value_of(&self, key: &limbo_graph::Key) -> anyhow::Result<DiagnosticStatus> {
|
||||
pub fn value_of(&self, key: &limbo_graph::Key) -> Result<DiagnosticStatus> {
|
||||
let graph_value = self.graph.value_of(key)?;
|
||||
let result = graph_value.value()?;
|
||||
Ok(result.clone())
|
||||
}
|
||||
|
||||
pub fn children_of(&self, key: &limbo_graph::Key) -> anyhow::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)?;
|
||||
Ok(result)
|
||||
}
|
||||
@@ -62,9 +61,9 @@ impl DiagnosticGraph {
|
||||
&self,
|
||||
parent: &limbo_graph::Key,
|
||||
desired_child_name: &str,
|
||||
) -> anyhow::Result<Option<limbo_graph::Key>> {
|
||||
) -> Result<Option<limbo_graph::Key>> {
|
||||
if !name_is_basic(&desired_child_name.to_owned()) {
|
||||
return Err(anyhow!("desired child name must be basic."));
|
||||
return Err(Error::from_msg("desired child name must be basic."));
|
||||
}
|
||||
let all_children = self.children_of(parent)?;
|
||||
for child in all_children.iter() {
|
||||
@@ -76,10 +75,7 @@ impl DiagnosticGraph {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
pub fn key_from_full_name(
|
||||
&self,
|
||||
full_name: &String,
|
||||
) -> anyhow::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 mut cur_base_key = self.graph.root_key();
|
||||
@@ -94,7 +90,7 @@ impl DiagnosticGraph {
|
||||
return Ok(Some(cur_base_key));
|
||||
}
|
||||
|
||||
pub fn full_name_from_key(&self, key: &limbo_graph::Key) -> anyhow::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 mut full_name = String::new();
|
||||
|
||||
@@ -112,7 +108,7 @@ impl DiagnosticGraph {
|
||||
pub fn value_from_name(
|
||||
&self,
|
||||
full_name: &String,
|
||||
) -> anyhow::Result<Option<(DiagnosticStatus, limbo_graph::Key)>> {
|
||||
) -> Result<Option<(DiagnosticStatus, limbo_graph::Key)>> {
|
||||
let key = self.key_from_full_name(full_name)?;
|
||||
if key.is_none() {
|
||||
return Ok(None);
|
||||
@@ -123,7 +119,7 @@ impl DiagnosticGraph {
|
||||
return Ok(Some((value, key)));
|
||||
}
|
||||
|
||||
pub fn add_status(&mut self, status: DiagnosticStatus) -> anyhow::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 status.name_is_basic() {
|
||||
self.graph.add(
|
||||
@@ -154,7 +150,7 @@ impl DiagnosticGraph {
|
||||
let child_node = self.graph.value_of(&child)?;
|
||||
if child_node.is_root() {
|
||||
// A child node can't be root.
|
||||
return Err(anyhow!("A child node can't be root!"));
|
||||
return Err(Error::from_msg("A child node can't be root!"));
|
||||
}
|
||||
if child_node.value()?.name() == *parent_name {
|
||||
// The child node matched the parent we were looking for.
|
||||
@@ -195,14 +191,14 @@ impl DiagnosticGraph {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_status_vec(&mut self, statuses: &Vec<DiagnosticStatus>) -> anyhow::Result<()> {
|
||||
pub fn add_status_vec(&mut self, statuses: &Vec<DiagnosticStatus>) -> Result<()> {
|
||||
for status in statuses {
|
||||
self.add_status(status.clone())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn reconcile_levels(&mut self) -> anyhow::Result<()> {
|
||||
pub fn reconcile_levels(&mut self) -> Result<()> {
|
||||
let leaf_keys = self.graph.find_leaf_keys()?;
|
||||
|
||||
'leaf_key_loop: for leaf_key in leaf_keys.iter() {
|
||||
@@ -238,7 +234,7 @@ impl DiagnosticGraph {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn export_graph(&self) -> anyhow::Result<Vec<DiagnosticStatus>> {
|
||||
pub fn export_graph(&self) -> Result<Vec<DiagnosticStatus>> {
|
||||
let keys_in_order = self.graph.get_keys_by_depth()?;
|
||||
|
||||
let mut statuses = Vec::<DiagnosticStatus>::new();
|
||||
@@ -288,7 +284,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_one_to_graph() -> anyhow::Result<()> {
|
||||
fn add_one_to_graph() -> Result<()> {
|
||||
let mut graph = DiagnosticGraph::new();
|
||||
graph.add_status(make_a_status_with_name("/a/b/c"))?;
|
||||
|
||||
@@ -315,7 +311,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_multiple_to_graph() -> anyhow::Result<()> {
|
||||
fn add_multiple_to_graph() -> Result<()> {
|
||||
let statuses = vec![
|
||||
make_a_status_with_name("/a"),
|
||||
make_a_status_with_name("/a/b"),
|
||||
@@ -354,7 +350,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn key_name_connections() -> anyhow::Result<()> {
|
||||
fn key_name_connections() -> Result<()> {
|
||||
let statuses = vec![
|
||||
make_a_status_with_name("/a"),
|
||||
make_a_status_with_name("/a/b"),
|
||||
@@ -381,7 +377,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reconcile_graph() -> anyhow::Result<()> {
|
||||
fn reconcile_graph() -> Result<()> {
|
||||
let statuses = vec![
|
||||
make_a_status_with_name_and_level("/a", DiagnosticLevel::UNSET),
|
||||
make_a_status_with_name_and_level("/a/b", DiagnosticLevel::UNSET),
|
||||
|
||||
Reference in new issue
Block a user