titanium_model/
string.rs

1//! Smart string implementation for Titan-rs.
2//!
3//! Provides a Copy-on-Write string type that supports:
4//! - Borrowed slices (`&'a str`) for zero-copy parsing.
5//! - Owned strings (`String`) for modification.
6//! - Shared strings (`Arc<str>`) for efficient caching.
7
8use serde::{Deserialize, Deserializer, Serialize, Serializer};
9use std::borrow::Cow;
10use std::fmt;
11use std::ops::Deref;
12use std::sync::Arc;
13
14#[derive(Clone, Eq)]
15pub enum TitanString<'a> {
16    Borrowed(&'a str),
17    Owned(String),
18    Shared(Arc<str>),
19}
20
21impl<'a> TitanString<'a> {
22    pub fn into_owned(self) -> String {
23        match self {
24            Self::Borrowed(s) => s.to_owned(),
25            Self::Owned(s) => s,
26            Self::Shared(s) => s.to_string(),
27        }
28    }
29
30    pub fn into_shared(self) -> Arc<str> {
31        match self {
32            Self::Borrowed(s) => Arc::from(s),
33            Self::Owned(s) => Arc::from(s),
34            Self::Shared(s) => s,
35        }
36    }
37}
38
39impl<'a> Deref for TitanString<'a> {
40    type Target = str;
41
42    fn deref(&self) -> &Self::Target {
43        match self {
44            Self::Borrowed(s) => s,
45            Self::Owned(s) => s,
46            Self::Shared(s) => s,
47        }
48    }
49}
50
51impl<'a> fmt::Debug for TitanString<'a> {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        fmt::Debug::fmt(&**self, f)
54    }
55}
56
57impl<'a> fmt::Display for TitanString<'a> {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        fmt::Display::fmt(&**self, f)
60    }
61}
62
63impl<'a> PartialEq for TitanString<'a> {
64    fn eq(&self, other: &Self) -> bool {
65        **self == **other
66    }
67}
68
69impl<'a> PartialEq<str> for TitanString<'a> {
70    fn eq(&self, other: &str) -> bool {
71        &**self == other
72    }
73}
74
75impl<'a> PartialEq<&str> for TitanString<'a> {
76    fn eq(&self, other: &&str) -> bool {
77        &**self == *other
78    }
79}
80
81impl<'a> PartialEq<String> for TitanString<'a> {
82    fn eq(&self, other: &String) -> bool {
83        &**self == other.as_str()
84    }
85}
86
87impl<'a> PartialEq<TitanString<'a>> for String {
88    fn eq(&self, other: &TitanString<'a>) -> bool {
89        self.as_str() == &**other
90    }
91}
92
93impl<'a> std::hash::Hash for TitanString<'a> {
94    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
95        (**self).hash(state)
96    }
97}
98
99impl<'a> Serialize for TitanString<'a> {
100    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
101    where
102        S: Serializer,
103    {
104        serializer.serialize_str(self)
105    }
106}
107
108impl<'de, 'a> Deserialize<'de> for TitanString<'a> {
109    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
110    where
111        D: Deserializer<'de>,
112    {
113        let s = String::deserialize(deserializer)?;
114        Ok(TitanString::Owned(s))
115    }
116}
117
118impl<'a> From<&'a str> for TitanString<'a> {
119    fn from(s: &'a str) -> Self {
120        Self::Borrowed(s)
121    }
122}
123
124impl<'a> From<String> for TitanString<'a> {
125    fn from(s: String) -> Self {
126        Self::Owned(s)
127    }
128}
129
130impl<'a> From<Arc<str>> for TitanString<'a> {
131    fn from(s: Arc<str>) -> Self {
132        Self::Shared(s)
133    }
134}
135
136impl<'a> From<Cow<'a, str>> for TitanString<'a> {
137    fn from(cow: Cow<'a, str>) -> Self {
138        match cow {
139            Cow::Borrowed(s) => Self::Borrowed(s),
140            Cow::Owned(s) => Self::Owned(s),
141        }
142    }
143}
144
145impl<'a> Default for TitanString<'a> {
146    fn default() -> Self {
147        Self::Borrowed("")
148    }
149}