-
Notifications
You must be signed in to change notification settings - Fork 60
feat: implement intoHeight trait #700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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<Height, HeightConversionError>; | ||||||
| } | ||||||
|
|
||||||
| // Error type for conversion failures | ||||||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you use |
||||||
| pub enum HeightConversionError { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also disallow |
||||||
| NegativeValue, | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| 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<Height, HeightConversionError> { | ||||||
| Ok(self) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl IntoHeight for u64 { | ||||||
| fn into_height(self) -> Result<Height, HeightConversionError> { | ||||||
| Height::try_from(self).map_err(|_| HeightConversionError::Overflow) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl IntoHeight for i64 { | ||||||
| fn into_height(self) -> Result<Height, HeightConversionError> { | ||||||
| if self < 0 { | ||||||
| Err(HeightConversionError::NegativeValue) | ||||||
| } else { | ||||||
| Ok(Height::try_from(self as u64).map_err(|_| HeightConversionError::Overflow)?) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| mod extended_header; | ||
| pub mod fraud_proof; | ||
| pub mod hash; | ||
| pub mod height; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's make the mod private and re-export the trait down below |
||
| mod merkle_proof; | ||
| pub mod nmt; | ||
| #[cfg(feature = "p2p")] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think since this can be fallible, we should rename it to
TryIntoHeightandtry_into_heightrespectively