-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add
Prefix
enum and buffer error checking (#131)
* Prefix enum * Simplify * Revert accidental change * Clippy * impl AsRef * Update prefix.rs * Error handling * Error checks * Collapse use statements * Manual impl From StorageError * Manual from prefix error * Update node.rs * Revert renaming Internal to Node * Update binary_proofs.rs * Revert "Update binary_proofs.rs" This reverts commit d9996de6072072d73e6b5c0fd7d6fcfe939cf625. * Update node.rs
- Loading branch information
Brandon Vrooman
authored
Nov 30, 2022
1 parent
e302ce8
commit 10568d2
Showing
8 changed files
with
306 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use crate::common::PrefixError; | ||
|
||
#[derive(Debug, Clone)] | ||
#[cfg_attr(feature = "std", derive(thiserror::Error))] | ||
pub enum DeserializeError { | ||
#[cfg_attr(feature = "std", error(transparent))] | ||
PrefixError(PrefixError), | ||
} | ||
|
||
impl From<PrefixError> for DeserializeError { | ||
fn from(err: PrefixError) -> Self { | ||
DeserializeError::PrefixError(err) | ||
} | ||
} |
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,56 @@ | ||
use crate::common::prefix::PrefixError::InvalidPrefix; | ||
|
||
const NODE: u8 = 0x01; | ||
const LEAF: u8 = 0x00; | ||
|
||
#[derive(Debug, Clone)] | ||
#[cfg_attr(feature = "std", derive(thiserror::Error))] | ||
pub enum PrefixError { | ||
#[cfg_attr(feature = "std", error("prefix {0} is not valid"))] | ||
InvalidPrefix(u8), | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
pub enum Prefix { | ||
Node, | ||
Leaf, | ||
} | ||
|
||
impl From<Prefix> for u8 { | ||
fn from(prefix: Prefix) -> Self { | ||
match prefix { | ||
Prefix::Node => NODE, | ||
Prefix::Leaf => LEAF, | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<u8> for Prefix { | ||
type Error = PrefixError; | ||
|
||
fn try_from(byte: u8) -> Result<Self, Self::Error> { | ||
match byte { | ||
NODE => Ok(Prefix::Node), | ||
LEAF => Ok(Prefix::Leaf), | ||
_ => Err(InvalidPrefix(byte)), | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for Prefix { | ||
fn as_ref(&self) -> &[u8] { | ||
match self { | ||
Prefix::Node => &[NODE], | ||
Prefix::Leaf => &[LEAF], | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<[u8; 1]> for Prefix { | ||
fn as_ref(&self) -> &[u8; 1] { | ||
match self { | ||
Prefix::Node => &[NODE], | ||
Prefix::Leaf => &[LEAF], | ||
} | ||
} | ||
} |
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
Oops, something went wrong.