use std::fmt;
#[derive(
Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
)]
pub struct AccountId(String);
impl AccountId {
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for AccountId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<&str> for AccountId {
fn from(s: &str) -> Self {
Self::new(s)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ProfileIx(pub u32);
impl ProfileIx {
pub const ROOT: ProfileIx = ProfileIx(0);
}
impl fmt::Display for ProfileIx {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<u32> for ProfileIx {
fn from(ix: u32) -> Self {
ProfileIx(ix)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn root_is_zero() {
assert_eq!(ProfileIx::ROOT, ProfileIx(0));
}
#[test]
fn displays_the_index() {
assert_eq!(ProfileIx(7).to_string(), "7");
}
#[test]
fn converts_from_u32() {
assert_eq!(ProfileIx::from(3u32), ProfileIx(3));
}
#[test]
fn account_id_round_trips_its_string() {
let id = AccountId::new("acct-42");
assert_eq!(id.as_str(), "acct-42");
assert_eq!(id.to_string(), "acct-42");
}
#[test]
fn account_id_from_str_matches_new() {
assert_eq!(AccountId::from("x"), AccountId::new("x"));
}
#[test]
fn account_ids_order_lexically() {
assert!(AccountId::new("alpha") < AccountId::new("bravo"));
}
}