From b7c48c8dd288c84a16230232481922dd0c0c8e55 Mon Sep 17 00:00:00 2001 From: varun-doshi Date: Mon, 28 Jul 2025 20:20:20 +0530 Subject: [PATCH 1/3] feat: implement intoHeight trait --- types/src/height.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++ types/src/lib.rs | 1 + 2 files changed, 48 insertions(+) create mode 100644 types/src/height.rs diff --git a/types/src/height.rs b/types/src/height.rs new file mode 100644 index 000000000..976235fd9 --- /dev/null +++ b/types/src/height.rs @@ -0,0 +1,47 @@ +use tendermint::block::Height; + +pub trait IntoHeight { + // Convert self into a Height, returning an error if conversion fails + fn into_height(self) -> Result; +} + +// Error type for conversion failures +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum HeightConversionError { + NegativeValue, + Overflow, +} + +impl std::fmt::Display for HeightConversionError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + HeightConversionError::NegativeValue => write!(f, "Height cannot be negative"), + HeightConversionError::Overflow => write!(f, "Value too large for Height"), + } + } +} + +impl std::error::Error for HeightConversionError {} + +// Implementations for different types +impl IntoHeight for Height { + fn into_height(self) -> Result { + Ok(self) + } +} + +impl IntoHeight for u64 { + fn into_height(self) -> Result { + Ok(Height::try_from(self).map_err(|_| HeightConversionError::Overflow)?) + } +} + +impl IntoHeight for i64 { + fn into_height(self) -> Result { + if self < 0 { + Err(HeightConversionError::NegativeValue) + } else { + Ok(Height::try_from(self as u64).map_err(|_| HeightConversionError::Overflow)?) + } + } +} diff --git a/types/src/lib.rs b/types/src/lib.rs index 28414ac47..41c2fba46 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -17,6 +17,7 @@ pub mod evidence; mod extended_header; pub mod fraud_proof; pub mod hash; +pub mod height; mod merkle_proof; pub mod nmt; #[cfg(feature = "p2p")] From 7caaa7f223ec9c4f7c863d53f1b10ec5846a8af0 Mon Sep 17 00:00:00 2001 From: varun-doshi Date: Mon, 28 Jul 2025 20:23:11 +0530 Subject: [PATCH 2/3] fix: clippy --- types/src/height.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/src/height.rs b/types/src/height.rs index 976235fd9..21a9a30fd 100644 --- a/types/src/height.rs +++ b/types/src/height.rs @@ -32,7 +32,7 @@ impl IntoHeight for Height { impl IntoHeight for u64 { fn into_height(self) -> Result { - Ok(Height::try_from(self).map_err(|_| HeightConversionError::Overflow)?) + Height::try_from(self).map_err(|_| HeightConversionError::Overflow) } } From 1df63e048f2616eedfd7198941bcc93d6b105c52 Mon Sep 17 00:00:00 2001 From: varun-doshi Date: Tue, 5 Aug 2025 12:35:57 +0530 Subject: [PATCH 3/3] fix: apply suggestions --- types/src/height.rs | 40 ++++++++++++++++++---------------------- types/src/lib.rs | 3 ++- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/types/src/height.rs b/types/src/height.rs index 21a9a30fd..e451153a6 100644 --- a/types/src/height.rs +++ b/types/src/height.rs @@ -1,45 +1,41 @@ +use serde::Serialize; use tendermint::block::Height; -pub trait IntoHeight { +pub trait TryIntoHeight: Serialize + Sized { // Convert self into a Height, returning an error if conversion fails - fn into_height(self) -> Result; + fn try_into_height(self) -> Result; } // Error type for conversion failures -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] pub enum HeightConversionError { - NegativeValue, + #[error("Height cannot be zero")] + ZeroHeight, + #[error("Height cannot be negative")] + NegativeHeight, + #[error("Value too large for Height")] Overflow, } -impl std::fmt::Display for HeightConversionError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - HeightConversionError::NegativeValue => write!(f, "Height cannot be negative"), - HeightConversionError::Overflow => write!(f, "Value too large for Height"), - } - } -} - -impl std::error::Error for HeightConversionError {} - // Implementations for different types -impl IntoHeight for Height { - fn into_height(self) -> Result { +impl TryIntoHeight for Height { + fn try_into_height(self) -> Result { Ok(self) } } -impl IntoHeight for u64 { - fn into_height(self) -> Result { +impl TryIntoHeight for u64 { + fn try_into_height(self) -> Result { Height::try_from(self).map_err(|_| HeightConversionError::Overflow) } } -impl IntoHeight for i64 { - fn into_height(self) -> Result { +impl TryIntoHeight for i64 { + fn try_into_height(self) -> Result { if self < 0 { - Err(HeightConversionError::NegativeValue) + Err(HeightConversionError::NegativeHeight) + } else if self == 0 { + Err(HeightConversionError::ZeroHeight) } else { Ok(Height::try_from(self as u64).map_err(|_| HeightConversionError::Overflow)?) } diff --git a/types/src/lib.rs b/types/src/lib.rs index 41c2fba46..14f83df4c 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -17,7 +17,7 @@ pub mod evidence; mod extended_header; pub mod fraud_proof; pub mod hash; -pub mod height; +mod height; mod merkle_proof; pub mod nmt; #[cfg(feature = "p2p")] @@ -52,6 +52,7 @@ pub use crate::eds::{AxisType, ExtendedDataSquare}; pub use crate::error::*; pub use crate::extended_header::*; pub use crate::fraud_proof::FraudProof; +pub use crate::height::TryIntoHeight; pub use crate::merkle_proof::MerkleProof; pub use crate::share::*; pub use crate::sync::*;