Skip to content

Commit

Permalink
storage: add Key::last method, impl KeySeg for all ints
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Aug 16, 2022
1 parent d19119e commit cebac03
Showing 1 changed file with 53 additions and 4 deletions.
57 changes: 53 additions & 4 deletions shared/src/types/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub enum Error {
ParseAddressFromKey,
#[error("Reserved prefix or string is specified: {0}")]
InvalidKeySeg(String),
#[error("Error parsing key segment {0}")]
ParseKeySeg(String),
}

/// Result for functions that may fail
Expand Down Expand Up @@ -274,6 +276,11 @@ impl Key {
self.len() == 0
}

/// Returns the last segment of the key, or `None` if it is empty.
pub fn last(&self) -> Option<&DbKeySeg> {
self.segments.last()
}

/// Returns a key of the validity predicate of the given address
/// Only this function can push "?" segment for validity predicate
pub fn validity_predicate(addr: &Address) -> Self {
Expand Down Expand Up @@ -317,8 +324,11 @@ impl Key {
.split_off(2)
.join(&KEY_SEGMENT_SEPARATOR.to_string()),
)
.map_err(|e| Error::Temporary {
error: format!("Cannot parse key segments {}: {}", db_key, e),
.map_err(|e| {
Error::ParseKeySeg(format!(
"Cannot parse key segments {}: {}",
db_key, e
))
})?,
};
Ok(key)
Expand Down Expand Up @@ -446,8 +456,11 @@ impl KeySeg for String {

impl KeySeg for BlockHeight {
fn parse(string: String) -> Result<Self> {
let h = string.parse::<u64>().map_err(|e| Error::Temporary {
error: format!("Unexpected height value {}, {}", string, e),
let h = string.parse::<u64>().map_err(|e| {
Error::ParseKeySeg(format!(
"Unexpected height value {}, {}",
string, e
))
})?;
Ok(BlockHeight(h))
}
Expand Down Expand Up @@ -481,6 +494,42 @@ impl KeySeg for Address {
}
}

/// Implement [`KeySeg`] for a type via its [`Display`] and
/// [`std::str::FromStr`] implementation.
macro_rules! impl_key_seg {
($type:ty) => {
impl KeySeg for $type {
fn parse(string: String) -> Result<Self> {
std::str::FromStr::from_str(&string).map_err(|err| {
Error::ParseKeySeg(format!(
"Failed parsing {} with {}",
string, err
))
})
}

fn raw(&self) -> String {
self.to_string()
}

fn to_db_key(&self) -> DbKeySeg {
DbKeySeg::StringSeg(self.to_string())
}
}
};
}

impl_key_seg!(i8);
impl_key_seg!(i16);
impl_key_seg!(i32);
impl_key_seg!(i64);
impl_key_seg!(i128);
impl_key_seg!(u8);
impl_key_seg!(u16);
impl_key_seg!(u32);
impl_key_seg!(u64);
impl_key_seg!(u128);

/// Epoch identifier. Epochs are identified by consecutive numbers.
#[derive(
Clone,
Expand Down

0 comments on commit cebac03

Please sign in to comment.