Skip to content

Commit

Permalink
feat: ParentNode fallible interface (#133)
Browse files Browse the repository at this point in the history
* ParentNode fallible interface

Merge

* ChildReturn alias

* Warnings

* Refactor

* Update node.rs

* Clean up

* Remove unnecessary trait restrictions

* Update path_iterator.rs

* Comments and test

* Rename to child error

* ChildError

* Add Key to ChildError

* Simplify

* Simplify

* Update merkle_tree.rs

* Refactor

* StroageNodeError

* NodeIsLeaf error for leaf nodes

* thiserror for StorageNodeError and ChildError

* Minor refactor common use statements

* Remove extern crate core

* Clean up use statements

* Update path_iterator.rs

* Update path_iterator.rs
  • Loading branch information
Brandon Vrooman authored Dec 4, 2022
1 parent 10568d2 commit 098b545
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 205 deletions.
1 change: 1 addition & 0 deletions fuel-merkle/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub use position::Position;
pub use storage_map::StorageMap;
pub use subtree::Subtree;

pub(crate) use node::{ChildError, ChildResult};
pub(crate) use position_path::PositionPath;
pub(crate) use prefix::{Prefix, PrefixError};

Expand Down
32 changes: 29 additions & 3 deletions fuel-merkle/src/common/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,35 @@ pub trait Node {
fn height(&self) -> u32;
fn leaf_key(&self) -> Self::Key;
fn is_leaf(&self) -> bool;
fn is_node(&self) -> bool;
}

pub trait ParentNode: Node {
fn left_child(&self) -> Self;
fn right_child(&self) -> Self;
pub trait ParentNode: Node
where
Self: Sized,
{
type Error;

fn left_child(&self) -> ChildResult<Self>;
fn right_child(&self) -> ChildResult<Self>;
}

#[allow(type_alias_bounds)]
pub type ChildResult<T: ParentNode> = Result<T, ChildError<T::Key, T::Error>>;

#[derive(Debug, Clone)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum ChildError<Key, E> {
#[cfg_attr(feature = "std", error("Child with key {0} was not found in storage"))]
ChildNotFound(Key),
#[cfg_attr(feature = "std", error("Node is a leaf with no children"))]
NodeIsLeaf,
#[cfg_attr(feature = "std", error(transparent))]
Error(E),
}

impl<Key, E> From<E> for ChildError<Key, E> {
fn from(e: E) -> Self {
Self::Error(e)
}
}
Loading

0 comments on commit 098b545

Please sign in to comment.