Skip to main content

spacetimedb_sats/
raw_identifier.rs

1use crate::algebraic_type::AlgebraicType;
2use crate::{impl_deserialize, impl_serialize, impl_st};
3use core::borrow::Borrow;
4use core::fmt;
5use core::ops::Deref;
6use lean_string::{LeanString, ToLeanString};
7
8/// A not-yet-validated identifier.
9#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
10pub struct RawIdentifier(pub(crate) LeanString);
11
12impl_st!([] RawIdentifier, _ts => AlgebraicType::String);
13impl_serialize!([] RawIdentifier, (self, ser) => ser.serialize_str(&self.0));
14impl_deserialize!([] RawIdentifier, de => LeanString::deserialize(de).map(Self));
15impl RawIdentifier {
16    /// Creates a new `RawIdentifier` from a string.
17    pub fn new(name: impl Into<LeanString>) -> Self {
18        Self(name.into())
19    }
20
21    pub fn into_inner(self) -> LeanString {
22        self.0
23    }
24}
25
26impl Deref for RawIdentifier {
27    type Target = str;
28
29    fn deref(&self) -> &Self::Target {
30        &self.0
31    }
32}
33
34impl AsRef<str> for RawIdentifier {
35    fn as_ref(&self) -> &str {
36        &self.0
37    }
38}
39
40impl Borrow<str> for RawIdentifier {
41    fn borrow(&self) -> &str {
42        &self.0
43    }
44}
45
46impl fmt::Debug for RawIdentifier {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        fmt::Debug::fmt(&self.0, f)
49    }
50}
51
52impl fmt::Display for RawIdentifier {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        fmt::Display::fmt(&self.0, f)
55    }
56}
57
58impl From<&'static str> for RawIdentifier {
59    fn from(s: &'static str) -> Self {
60        RawIdentifier(LeanString::from_static_str(s))
61    }
62}
63
64impl From<String> for RawIdentifier {
65    fn from(s: String) -> Self {
66        RawIdentifier(s.to_lean_string())
67    }
68}