taxy_api/
id.rs

1use crate::error::Error;
2use serde::{Deserialize, Serialize};
3use std::{fmt, str::FromStr};
4use utoipa::ToSchema;
5
6#[derive(Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, ToSchema)]
7pub struct ShortId([u8; 8]);
8
9impl ShortId {
10    pub fn new() -> Self {
11        Default::default()
12    }
13}
14
15impl From<[u8; 7]> for ShortId {
16    fn from(id: [u8; 7]) -> Self {
17        let mut bytes = [0; 8];
18        bytes[1..].copy_from_slice(&id);
19        Self(bytes)
20    }
21}
22
23impl fmt::Debug for ShortId {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        write!(f, "ShortId({})", self)
26    }
27}
28
29impl fmt::Display for ShortId {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        if self.0[0] == 0 {
32            write!(f, "{}", hex::encode(&self.0[1..]))
33        } else {
34            let len = self.0.iter().rposition(|&b| b != 0).map_or(0, |i| i + 1);
35            write!(f, "{}", String::from_utf8_lossy(&self.0[..len]))
36        }
37    }
38}
39
40impl FromStr for ShortId {
41    type Err = Error;
42
43    fn from_str(s: &str) -> Result<Self, Self::Err> {
44        let mut id = [0; 8];
45        if hex::decode_to_slice(s, &mut id[1..]).is_ok() {
46            return Ok(ShortId(id));
47        }
48        let str = s.to_ascii_lowercase();
49        if !str.is_ascii() || str.len() > 8 {
50            return Err(Error::InvalidShortId { id: s.to_string() });
51        }
52        let bytes = str.as_bytes();
53        let len = bytes.len().min(id.len());
54        id[..len].copy_from_slice(&bytes[..len]);
55        Ok(ShortId(id))
56    }
57}
58
59impl Serialize for ShortId {
60    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
61        serializer.serialize_str(&self.to_string())
62    }
63}
64
65impl<'de> Deserialize<'de> for ShortId {
66    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
67        let s = String::deserialize(deserializer)?;
68        ShortId::from_str(&s).map_err(serde::de::Error::custom)
69    }
70}
71
72#[cfg(test)]
73mod test {
74    use super::*;
75
76    #[test]
77    fn parse_hex() {
78        let id = ShortId::from_str("f9cf7e3faa1aca").unwrap();
79        assert_eq!(id.to_string(), "f9cf7e3faa1aca");
80    }
81
82    #[test]
83    fn parse_hyphen_id() {
84        let id = ShortId::from_str("djs-vjd").unwrap();
85        assert_eq!(id.to_string(), "djs-vjd");
86    }
87}