diff --git a/src/flow/binary.rs b/src/flow/binary.rs index 06693ab..1d2b4e3 100644 --- a/src/flow/binary.rs +++ b/src/flow/binary.rs @@ -3,6 +3,8 @@ pub mod balancing; pub mod bound; pub mod bounds; +use std::fmt::Display; + use crate::flow::comparator::*; use crate::func::{context::*, *}; @@ -132,10 +134,26 @@ pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> { } } +#[derive(Debug)] +pub enum HeightError { + LeafHeight(u64), + NodeHeight, +} + +impl Display for HeightError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::NodeHeight => write!(f, "invalid node height: 0."), + Self::LeafHeight(height) => { + write!(f, "invalid leaf height: {height}!=0.") + } + } + } +} + pub trait BinaryTreesHeight<'a>: BinaryTrees<'a> { fn height(&self, tree: &Self::Tree) -> u64; - fn leaf_height_error(&self, height: u64) -> BTWrap<'a, Self, T>; - fn node_height_error(&self) -> BTWrap<'a, Self, T>; + fn height_error(&self, error: HeightError) -> BTWrap<'a, Self, T>; } pub trait BinaryTreesTryJoin<'a>: BinaryTrees<'a> { diff --git a/src/flow/binary/avl.rs b/src/flow/binary/avl.rs index ede31bf..9acd5d7 100644 --- a/src/flow/binary/avl.rs +++ b/src/flow/binary/avl.rs @@ -6,7 +6,7 @@ pub trait BinaryTreesAvl<'a>: fn assume_node(&self, tree: &Self::Tree) -> BTWrap<'a, Self, Self::Node> { match self.refer(tree) { Some(reference) => self.resolve(&reference), - None => self.leaf_height_error(self.height(tree)), + None => self.height_error(HeightError::LeafHeight(self.height(tree))), } } diff --git a/src/flow/binary/balancing.rs b/src/flow/binary/balancing.rs index a8b95b7..26b898a 100644 --- a/src/flow/binary/balancing.rs +++ b/src/flow/binary/balancing.rs @@ -160,12 +160,8 @@ impl<'a, BT: BinaryTreesUnbalanced<'a>> BinaryTreesHeight<'a> for BalancedTrees< self.0.height(tree) } - fn leaf_height_error(&self, height: u64) -> BTWrap<'a, Self, T> { - self.0.leaf_height_error(height) - } - - fn node_height_error(&self) -> BTWrap<'a, Self, T> { - self.0.node_height_error() + fn height_error(&self, error: HeightError) -> BTWrap<'a, Self, T> { + self.0.height_error(error) } } diff --git a/src/flow/binary/bound.rs b/src/flow/binary/bound.rs index 72b7bf9..0f655ae 100644 --- a/src/flow/binary/bound.rs +++ b/src/flow/binary/bound.rs @@ -169,12 +169,8 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesHeight<'a>> BinaryTreesHeight< self.0.height(&tree.bound) } - fn leaf_height_error(&self, height: u64) -> BTWrap<'a, Self, T> { - self.0.leaf_height_error(height) - } - - fn node_height_error(&self) -> BTWrap<'a, Self, T> { - self.0.node_height_error() + fn height_error(&self, error: HeightError) -> BTWrap<'a, Self, T> { + self.0.height_error(error) } } diff --git a/src/mrds/trees/heighted.rs b/src/mrds/trees/heighted.rs index 5822f2c..159a773 100644 --- a/src/mrds/trees/heighted.rs +++ b/src/mrds/trees/heighted.rs @@ -119,12 +119,8 @@ impl<'a, A: 'a + Ord + Clone> BinaryTreesHeight<'a> for Trees { tree.height } - fn leaf_height_error(&self, height: u64) -> BTWrap<'a, Self, T> { - panic!("leaf height error: {height}.") - } - - fn node_height_error(&self) -> BTWrap<'a, Self, T> { - panic!("node with height 0.") + fn height_error(&self, error: HeightError) -> BTWrap<'a, Self, T> { + panic!("{error}") } }