agent_tk/
uuid.rs

1//! Wrap a u128 as a UUID
2
3use std::fmt::Debug;
4
5use crate::Result;
6
7/// Store a UUID
8#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
9pub struct Uuid
10{
11  data: u128,
12}
13
14impl Uuid
15{
16  /// Generate a new UUID
17  pub fn new() -> Self
18  {
19    Self {
20      data: uuid::Uuid::new_v4().as_u128(),
21    }
22  }
23  /// Convert this UUID into a hex representation
24  pub fn to_hex(&self) -> String
25  {
26    hex::encode(self.data.to_le_bytes())
27  }
28  /// Parse a UUID from a string representation
29  pub fn from_string(input: &str) -> Result<Self>
30  {
31    Ok(
32      uuid::Uuid::parse_str(input)
33        .map_err(|e| crate::Error::InvalidUuid(input.to_string(), e.to_string()))?
34        .into(),
35    )
36  }
37}
38
39impl Default for Uuid
40{
41  fn default() -> Self
42  {
43    Self::new()
44  }
45}
46
47impl From<uuid::Uuid> for Uuid
48{
49  fn from(data: uuid::Uuid) -> Self
50  {
51    Self {
52      data: data.as_u128(),
53    }
54  }
55}
56
57impl From<u128> for Uuid
58{
59  fn from(data: u128) -> Self
60  {
61    Self { data }
62  }
63}
64
65impl From<Uuid> for u128
66{
67  fn from(val: Uuid) -> Self
68  {
69    val.data
70  }
71}
72
73impl Debug for Uuid
74{
75  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
76  {
77    uuid::Uuid::from_u128(self.data).fmt(f)
78  }
79}
80
81impl std::fmt::Display for Uuid
82{
83  /// Convert a UUID into a hyphenated string representation
84  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
85  {
86    write!(f, "{}", uuid::Uuid::from_u128(self.data).hyphenated())
87  }
88}
89
90//  ____               _
91// / ___|  ___ _ __ __| | ___
92// \___ \ / _ \ '__/ _` |/ _ \
93//  ___) |  __/ | | (_| |  __/
94// |____/ \___|_|  \__,_|\___|
95
96impl serde::Serialize for Uuid
97{
98  fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
99  where
100    S: serde::Serializer,
101  {
102    serializer.serialize_str(self.to_string().as_str())
103  }
104}
105
106struct UuidVisitor;
107
108impl<'de> serde::de::Visitor<'de> for UuidVisitor
109{
110  type Value = Uuid;
111
112  fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
113  {
114    formatter.write_str("a valid UUID string")
115  }
116  fn visit_str<E>(self, v: &str) -> std::result::Result<Self::Value, E>
117  where
118    E: serde::de::Error,
119  {
120    Uuid::from_string(v).map_err(|e| {
121      E::custom(format!(
122        "Failed to parse UUID string {} with error {}",
123        v, e
124      ))
125    })
126  }
127  fn visit_string<E>(self, v: String) -> std::result::Result<Self::Value, E>
128  where
129    E: serde::de::Error,
130  {
131    self.visit_str(v.as_str())
132  }
133}
134
135impl<'de> serde::Deserialize<'de> for Uuid
136{
137  fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
138  where
139    D: serde::Deserializer<'de>,
140  {
141    deserializer.deserialize_str(UuidVisitor)
142  }
143}