Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions types/src/height.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use tendermint::block::Height;

pub trait IntoHeight {

Check failure on line 3 in types/src/height.rs

View workflow job for this annotation

GitHub Actions / docs

missing documentation for a trait

Copy link
Copy Markdown
Collaborator

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 TryIntoHeight and try_into_height respectively

// Convert self into a Height, returning an error if conversion fails
fn into_height(self) -> Result<Height, HeightConversionError>;

Check failure on line 5 in types/src/height.rs

View workflow job for this annotation

GitHub Actions / docs

missing documentation for a method
}

// Error type for conversion failures
#[derive(Debug, Clone, PartialEq, Eq)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use thiserror for creating the error type? that's what we do for all other error types

pub enum HeightConversionError {

Check failure on line 10 in types/src/height.rs

View workflow job for this annotation

GitHub Actions / docs

missing documentation for an enum

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also disallow 0 as height and have respective error

NegativeValue,

Check failure on line 11 in types/src/height.rs

View workflow job for this annotation

GitHub Actions / docs

missing documentation for a variant

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
NegativeValue,
NegativeHeight,

Overflow,

Check failure on line 12 in types/src/height.rs

View workflow job for this annotation

GitHub Actions / docs

missing documentation for a variant
}

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)?)
}
}
}
1 change: 1 addition & 0 deletions types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
mod extended_header;
pub mod fraud_proof;
pub mod hash;
pub mod height;

Check failure on line 20 in types/src/lib.rs

View workflow job for this annotation

GitHub Actions / docs

missing documentation for a module

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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")]
Expand Down
Loading