orchestra_toolkit/hgtp/
token.rs

1/* Copyright 2024-2025 LEDR Technologies Inc.
2* This file is part of the Orchestra library, which helps developer use our Orchestra technology which is based on AvesTerra, owned and developped by Georgetown University, under license agreement with LEDR Technologies Inc.
3*
4* The Orchestra library is a free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
5*
6* The Orchestra library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
7*
8* You should have received a copy of the GNU Lesser General Public License along with the Orchestra library. If not, see <https://www.gnu.org/licenses/>.
9*
10* If you have any questions, feedback or issues about the Orchestra library, you can contact us at support@ledr.io.
11*/
12
13use std::{
14    ops::{Deref, DerefMut},
15    str::FromStr,
16};
17use uuid::Uuid;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
20pub struct Token(Uuid);
21
22impl Deref for Token {
23    type Target = Uuid;
24
25    fn deref(&self) -> &Self::Target {
26        &self.0
27    }
28}
29
30impl DerefMut for Token {
31    fn deref_mut(&mut self) -> &mut Self::Target {
32        &mut self.0
33    }
34}
35
36impl Token {
37    pub const NULL: Self = Self(Uuid::nil());
38    pub const NOAUTH: Self = Self(Uuid::max());
39
40    pub fn random() -> Self {
41        Self(Uuid::new_v4())
42    }
43
44    pub fn from_bytes(bytes: [u8; 16]) -> Self {
45        Self(Uuid::from_bytes(bytes))
46    }
47
48    /// Useful to skip serialization of the default value.
49    pub fn is_null(&self) -> bool {
50        *self == Self::NULL
51    }
52}
53
54impl FromStr for Token {
55    type Err = uuid::Error;
56
57    fn from_str(s: &str) -> Result<Self, Self::Err> {
58        if s == "********-****-****-****-************" {
59            return Ok(Token::NOAUTH);
60        }
61        Ok(Self(Uuid::try_parse(s)?))
62    }
63}
64
65mod serde_impl {
66    use super::*;
67
68    use serde::de::{self, Visitor};
69    use serde::ser;
70    use std::fmt;
71
72    impl ser::Serialize for Token {
73        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
74        where
75            S: ser::Serializer,
76        {
77            serializer.serialize_str(self.to_string().as_str())
78        }
79    }
80
81    struct TokenVisitor;
82    impl<'de> Visitor<'de> for TokenVisitor {
83        type Value = Token;
84
85        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
86            formatter.write_str("A utf-8 string that is at most 255 bytes long.")
87        }
88
89        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
90        where
91            E: de::Error,
92        {
93            Token::from_str(v).map_err(de::Error::custom)
94        }
95    }
96
97    impl<'de> de::Deserialize<'de> for Token {
98        fn deserialize<D>(deserializer: D) -> Result<Token, D::Error>
99        where
100            D: de::Deserializer<'de>,
101        {
102            deserializer.deserialize_str(TokenVisitor)
103        }
104    }
105}