diff --git a/src/flow/binary/balancing.rs b/src/flow/binary/balancing.rs index 26b898a..23aab1f 100644 --- a/src/flow/binary/balancing.rs +++ b/src/flow/binary/balancing.rs @@ -39,8 +39,7 @@ impl<'a, BT: FunctorContext<'a>> FunctorContext<'a> for BalancedTrees { #[derive(Debug)] pub enum BalancingError { - LeafHeight(u64), - NodeHeight, + Height(HeightError), Balance(u64, u64), HeightOverflow, HeightMismatch { children: (u64, u64), parent: u64 }, @@ -49,10 +48,7 @@ pub enum BalancingError { impl Display for BalancingError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::NodeHeight => write!(f, "invalid AVL non-leaf height: 0."), - Self::LeafHeight(height) => { - write!(f, "invalid AVL leaf height: {height}!=0.") - } + Self::Height(height_error) => write!(f, "invalid AVL non-leaf height: {height_error}"), Self::Balance(hl, hr) => write!(f, "unbalanced AVL node: {hl} {hr}."), Self::HeightOverflow => write!(f, "AVL tree height overflow"), Self::HeightMismatch { children, parent } => { diff --git a/src/rstd/collections/avl.rs b/src/rstd/collections/avl.rs index bfa877c..99cdaa2 100644 --- a/src/rstd/collections/avl.rs +++ b/src/rstd/collections/avl.rs @@ -3,7 +3,7 @@ pub mod context; use std::{error::Error, fmt::Display, rc::Rc}; -use crate::flow::binary::balancing::*; +use crate::flow::binary::{balancing::*, *}; use crate::rcore::*; use crate::rstd::{ atomic::{au64::*, *}, @@ -37,6 +37,12 @@ impl From for AvlError { } } +impl From for AvlError { + fn from(value: HeightError) -> Self { + BalancingError::Height(value).into() + } +} + impl Display for AvlError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -173,10 +179,10 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for AvlTreeFact let height = u64::a_deserialize(deserializer)?; if let Nullable::Null(_) = node { if height != 0 { - Err(BalancingError::LeafHeight(height))? + Err(HeightError::LeafHeight(height))? } } else if height == 0 { - Err(BalancingError::NodeHeight)? + Err(HeightError::NodeHeight)? } Ok(AvlTree { node, height }) }