use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Id(#[serde(with = "id_serde")] [u8; 16]);
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum IdError {
#[error("base58 decode: {0}")]
Base58(#[from] bs58::decode::Error),
#[error("expected 16 bytes, got {0}")]
Length(usize),
}
impl Id {
pub fn new() -> Self {
Self(*uuid::Uuid::new_v4().as_bytes())
}
pub fn from_bytes(bytes: [u8; 16]) -> Self {
Self(bytes)
}
pub fn parse(s: &str) -> Result<Self, IdError> {
let v = bs58::decode(s).into_vec()?;
if v.len() != 16 {
return Err(IdError::Length(v.len()));
}
let mut buf = [0u8; 16];
buf.copy_from_slice(&v);
Ok(Self(buf))
}
pub fn as_bytes(&self) -> &[u8; 16] {
&self.0
}
}
impl Default for Id {
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&bs58::encode(self.0).into_string())
}
}
mod id_serde {
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S: Serializer>(bytes: &[u8; 16], s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&bs58::encode(bytes).into_string())
}
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<[u8; 16], D::Error> {
let s = String::deserialize(d)?;
let v = bs58::decode(&s)
.into_vec()
.map_err(serde::de::Error::custom)?;
if v.len() != 16 {
return Err(serde::de::Error::custom(format!(
"expected 16 bytes, got {}",
v.len()
)));
}
let mut buf = [0u8; 16];
buf.copy_from_slice(&v);
Ok(buf)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn roundtrip() {
let a = Id::new();
let s = a.to_string();
let b = Id::parse(&s).unwrap();
assert_eq!(a, b);
assert!(
(21..=22).contains(&s.len()),
"unexpected length: {}",
s.len()
);
}
#[test]
fn rejects_bad_length() {
let bad = bs58::encode([0u8; 8]).into_string();
assert!(Id::parse(&bad).is_err());
}
#[test]
fn json_roundtrip() {
let a = Id::new();
let s = serde_json::to_string(&a).unwrap();
let b: Id = serde_json::from_str(&s).unwrap();
assert_eq!(a, b);
}
}