1use std::{
8 fmt,
9 io::{Read, Write},
10 str::FromStr,
11};
12
13use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
14use uuid::Uuid;
15
16use crate::types::encoding::*;
17
18#[derive(Eq, PartialEq, Clone, Hash)]
20pub struct Guid {
21 uuid: Uuid,
22}
23
24impl Serialize for Guid {
25 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
26 where
27 S: Serializer,
28 {
29 self.uuid.to_string().serialize(serializer)
30 }
31}
32
33impl<'de> Deserialize<'de> for Guid {
34 fn deserialize<D>(deserializer: D) -> Result<Guid, D::Error>
35 where
36 D: Deserializer<'de>,
37 {
38 let s = String::deserialize(deserializer)?;
39 let guid = Guid::from_str(&s).map_err(|_| D::Error::custom("Cannot parse uuid"))?;
40 Ok(guid)
41 }
42}
43
44impl fmt::Display for Guid {
45 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46 write!(f, "{}", self.uuid)
47 }
48}
49
50impl fmt::Debug for Guid {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 write!(f, "{}", self.uuid.hyphenated())
53 }
54}
55
56impl BinaryEncoder<Guid> for Guid {
57 fn byte_len(&self) -> usize {
58 16
59 }
60
61 fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
62 let mut size: usize = 0;
63 size += process_encode_io_result(stream.write(self.uuid.as_bytes()))?;
64 Ok(size)
65 }
66
67 fn decode<S: Read>(stream: &mut S, _: &DecodingOptions) -> EncodingResult<Self> {
68 let mut bytes = [0u8; 16];
69 process_decode_io_result(stream.read_exact(&mut bytes))?;
70 Ok(Guid {
71 uuid: Uuid::from_bytes(bytes),
72 })
73 }
74}
75
76impl FromStr for Guid {
77 type Err = ();
78
79 fn from_str(s: &str) -> Result<Self, Self::Err> {
80 Uuid::from_str(s).map(|uuid| Guid { uuid }).map_err(|err| {
81 error!("Guid cannot be parsed from string, err = {:?}", err);
82 })
83 }
84}
85
86impl From<Uuid> for Guid {
87 fn from(uuid: Uuid) -> Self {
88 Self { uuid }
89 }
90}
91
92impl Default for Guid {
93 fn default() -> Self {
94 Guid::null()
95 }
96}
97
98impl Guid {
99 pub fn null() -> Guid {
101 Guid { uuid: Uuid::nil() }
102 }
103
104 pub fn new() -> Guid {
106 Guid {
107 uuid: Uuid::new_v4(),
108 }
109 }
110
111 pub fn as_bytes(&self) -> &[u8; 16] {
113 self.uuid.as_bytes()
114 }
115
116 pub fn from_bytes(bytes: [u8; 16]) -> Guid {
118 Guid {
119 uuid: Uuid::from_bytes(bytes),
120 }
121 }
122}