-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a json-conversion feature for converting JSON to valid Heights
- Loading branch information
Showing
6 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//! Consensus-critical conversion from JSON [`Value`] to [`Height`]. | ||
use serde_json::Value; | ||
|
||
use crate::BoxError; | ||
|
||
use super::{Height, TryIntoHeight}; | ||
|
||
impl TryIntoHeight for Value { | ||
type Error = BoxError; | ||
|
||
fn try_into_height(&self) -> Result<Height, Self::Error> { | ||
if self.is_number() { | ||
let height = self.as_u64().ok_or("JSON value outside u64 range")?; | ||
return height.try_into_height(); | ||
} | ||
|
||
if let Some(height) = self.as_str() { | ||
return height.try_into_height(); | ||
} | ||
|
||
Err("JSON value must be a number or string".into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters