use core::fmt::{Display, Error as FmtError, Formatter};
use ibc_proto::google::protobuf::Any;
use uuid::Uuid;
#[derive(Copy, Clone, Debug)]
pub enum TrackingId {
Uuid(Uuid),
Static(&'static str),
PacketClearing(Uuid),
}
impl TrackingId {
pub fn new_uuid() -> Self {
Self::Uuid(Uuid::new_v4())
}
pub fn new_static(s: &'static str) -> Self {
Self::Static(s)
}
pub fn new_packet_clearing() -> Self {
Self::PacketClearing(Uuid::new_v4())
}
pub fn is_clearing(&self) -> bool {
matches!(self, Self::PacketClearing(_))
}
}
impl Display for TrackingId {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
match self {
TrackingId::Uuid(u) => {
let mut s = u.to_string();
s.truncate(8);
s.fmt(f)
}
TrackingId::Static(s) => s.fmt(f),
TrackingId::PacketClearing(u) => {
let mut uuid = "cleared/".to_owned();
let mut s = u.to_string();
s.truncate(8);
uuid.push_str(&s);
uuid.fmt(f)
}
}
}
}
#[derive(Debug, Clone)]
pub struct TrackedMsgs {
pub msgs: Vec<Any>,
pub tracking_id: TrackingId,
}
impl TrackedMsgs {
pub fn new(msgs: Vec<Any>, tracking_id: TrackingId) -> Self {
Self { msgs, tracking_id }
}
pub fn new_static(msgs: Vec<Any>, tracking_id: &'static str) -> Self {
Self {
msgs,
tracking_id: TrackingId::Static(tracking_id),
}
}
pub fn new_uuid(msgs: Vec<Any>, tracking_id: Uuid) -> Self {
Self {
msgs,
tracking_id: TrackingId::Uuid(tracking_id),
}
}
pub fn new_single(msg: Any, tracking_id: &'static str) -> Self {
Self {
msgs: vec![msg],
tracking_id: TrackingId::Static(tracking_id),
}
}
pub fn new_single_uuid(msg: Any, tracking_id: Uuid) -> Self {
Self {
msgs: vec![msg],
tracking_id: TrackingId::Uuid(tracking_id),
}
}
pub fn messages(&self) -> &Vec<Any> {
&self.msgs
}
pub fn tracking_id(&self) -> TrackingId {
self.tracking_id
}
}