1use std::ops::{Deref, DerefMut, Index};
2
3use crate::{Reflect, TypeOf};
4
5#[derive(Debug, Copy, Clone, PartialEq, Default)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct StringType;
8
9impl StringType {
10 pub fn id(&self) -> std::any::TypeId {
11 return std::any::TypeId::of::<String>();
12 }
13
14 pub fn name(&self) -> std::string::String {
15 return format!("{}", "type::string");
16 }
17
18 pub fn to_type(&self) -> crate::Type {
19 return crate::Type::String(self.clone());
20 }
21
22 pub fn assignable_to(&self, _type: crate::Type) -> bool {
23 return self.id() == _type.id();
24 }
25
26 pub fn convertable_to(&self, _type: crate::Type) -> bool {
27 return _type.is_string();
28 }
29}
30
31impl std::fmt::Display for StringType {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 return write!(f, "{}", self.name());
34 }
35}
36
37impl TypeOf for std::string::String {
38 fn type_of() -> crate::Type {
39 return crate::Type::String(StringType::default());
40 }
41}
42
43impl TypeOf for str {
44 fn type_of() -> crate::Type {
45 return crate::Type::String(StringType::default());
46 }
47}
48
49#[derive(Debug, Clone, PartialEq)]
50#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
51pub struct String(std::string::String);
52
53impl String {
54 pub fn len(&self) -> usize {
55 return self.0.len();
56 }
57
58 pub fn as_str(&self) -> &str {
59 return self.0.as_str();
60 }
61
62 pub fn get(&self) -> std::string::String {
63 return self.0.clone();
64 }
65}
66
67impl From<std::string::String> for crate::Value {
68 fn from(value: std::string::String) -> Self {
69 return Self::String(String(value));
70 }
71}
72
73impl Into<std::string::String> for crate::Value {
74 fn into(self) -> std::string::String {
75 return self.to_string().get();
76 }
77}
78
79impl From<std::string::String> for String {
80 fn from(value: std::string::String) -> Self {
81 return Self(value);
82 }
83}
84
85impl Into<std::string::String> for String {
86 fn into(self) -> std::string::String {
87 return self.0;
88 }
89}
90
91impl std::fmt::Display for String {
92 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93 return write!(f, "{}", self.as_ref());
94 }
95}
96
97impl TypeOf for String {
98 fn type_of() -> crate::Type {
99 return std::string::String::type_of();
100 }
101}
102
103impl Reflect for String {
104 fn reflect(self) -> crate::Value {
105 return crate::Value::String(self.clone());
106 }
107}
108
109impl Reflect for std::string::String {
110 fn reflect(self) -> crate::Value {
111 return crate::Value::String(String(self.clone()));
112 }
113}
114
115impl Reflect for &str {
116 fn reflect(self) -> crate::Value {
117 return crate::Value::String(String(self.to_string()));
118 }
119}
120
121impl AsRef<str> for String {
122 fn as_ref(&self) -> &str {
123 return &self.0;
124 }
125}
126
127impl AsMut<std::string::String> for String {
128 fn as_mut(&mut self) -> &mut std::string::String {
129 return &mut self.0;
130 }
131}
132
133impl Deref for String {
134 type Target = str;
135
136 fn deref(&self) -> &Self::Target {
137 return &self.0;
138 }
139}
140
141impl DerefMut for String {
142 fn deref_mut(&mut self) -> &mut Self::Target {
143 return &mut self.0;
144 }
145}
146
147impl Index<usize> for String {
148 type Output = u8;
149
150 fn index(&self, index: usize) -> &Self::Output {
151 return self.as_bytes().index(index);
152 }
153}
154
155#[cfg(test)]
156mod test {
157 use crate::*;
158
159 #[test]
160 pub fn str() {
161 let value = value_of!("test");
162
163 assert!(value.is_string());
164 assert_eq!(value.len(), 4);
165 assert_eq!(value.to_string().as_ref(), "test");
166 }
167
168 #[test]
169 pub fn string() {
170 let value = value_of!("test".to_string());
171
172 assert!(value.is_string());
173 assert_eq!(value.len(), 4);
174 assert_eq!(value.to_string().as_ref(), "test");
175 }
176}