ibc_core_host_types/identifiers/
sequence.rsuse ibc_primitives::prelude::*;
use crate::error::IdentifierError;
#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Sequence(u64);
impl core::str::FromStr for Sequence {
type Err = IdentifierError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from(s.parse::<u64>().map_err(|e| {
IdentifierError::FailedToParse {
description: format!("sequence `{s}`: {e}"),
}
})?))
}
}
impl Sequence {
pub fn value(&self) -> u64 {
self.0
}
pub fn is_zero(&self) -> bool {
self.0 == 0
}
pub fn increment(&self) -> Sequence {
Sequence(self.0 + 1)
}
pub fn to_vec(&self) -> Vec<u8> {
self.0.to_be_bytes().to_vec()
}
}
impl From<u64> for Sequence {
fn from(seq: u64) -> Self {
Sequence(seq)
}
}
impl From<Sequence> for u64 {
fn from(s: Sequence) -> u64 {
s.0
}
}
impl core::fmt::Display for Sequence {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
write!(f, "{}", self.0)
}
}