logix_type/types/
string.rs

1use std::{borrow::Cow, ffi::OsStr, fmt, path::Path, rc::Rc, sync::Arc};
2
3use logix_vfs::LogixVfs;
4use smol_str::SmolStr;
5
6use crate::{
7    error::{ParseError, Result, Wanted},
8    parser::LogixParser,
9    token::{Literal, Token},
10    type_trait::{LogixTypeDescriptor, LogixValueDescriptor, Value},
11    LogixType,
12};
13
14/// Represents a short string, will not need allocation for typical identifiers
15#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
16pub struct ShortStr {
17    value: SmolStr,
18}
19
20impl From<String> for ShortStr {
21    fn from(value: String) -> Self {
22        Self {
23            value: value.into(),
24        }
25    }
26}
27
28impl From<Box<str>> for ShortStr {
29    fn from(value: Box<str>) -> Self {
30        Self {
31            value: value.into(),
32        }
33    }
34}
35
36impl From<ShortStr> for Box<str> {
37    fn from(value: ShortStr) -> Self {
38        // TODO: This is not optimal, is there a way to enable From in smol_str?
39        value.value.as_ref().into()
40    }
41}
42
43impl From<Arc<str>> for ShortStr {
44    fn from(value: Arc<str>) -> Self {
45        Self {
46            value: value.into(),
47        }
48    }
49}
50
51impl From<ShortStr> for Arc<str> {
52    fn from(value: ShortStr) -> Self {
53        value.value.into()
54    }
55}
56
57impl From<Rc<str>> for ShortStr {
58    fn from(value: Rc<str>) -> Self {
59        // TODO: This is not optimal, is there a way to enable From in smol_str?
60        Self {
61            value: value.as_ref().into(),
62        }
63    }
64}
65
66impl From<ShortStr> for Rc<str> {
67    fn from(value: ShortStr) -> Self {
68        // TODO: This is not optimal, is there a way to enable From in smol_str?
69        value.value.as_ref().into()
70    }
71}
72
73impl From<SmolStr> for ShortStr {
74    fn from(value: SmolStr) -> Self {
75        Self { value }
76    }
77}
78
79impl<'a> From<&'a str> for ShortStr {
80    fn from(value: &'a str) -> Self {
81        Self {
82            value: value.into(),
83        }
84    }
85}
86
87impl<'a> From<Cow<'a, str>> for ShortStr {
88    fn from(value: Cow<'a, str>) -> Self {
89        Self {
90            value: value.into(),
91        }
92    }
93}
94
95impl AsRef<Path> for ShortStr {
96    fn as_ref(&self) -> &Path {
97        self.value.as_str().as_ref()
98    }
99}
100
101impl AsRef<OsStr> for ShortStr {
102    fn as_ref(&self) -> &OsStr {
103        self.value.as_str().as_ref()
104    }
105}
106
107impl AsRef<str> for ShortStr {
108    fn as_ref(&self) -> &str {
109        self.value.as_str()
110    }
111}
112
113impl std::ops::Deref for ShortStr {
114    type Target = str;
115
116    fn deref(&self) -> &Self::Target {
117        &self.value
118    }
119}
120
121impl fmt::Display for ShortStr {
122    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123        fmt::Display::fmt(self.value.as_str(), f)
124    }
125}
126
127impl fmt::Debug for ShortStr {
128    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129        fmt::Debug::fmt(self.value.as_str(), f)
130    }
131}
132
133macro_rules! impl_for_str {
134    ($($type:ty),+) => {$(
135        impl LogixType for $type {
136            fn descriptor() -> &'static LogixTypeDescriptor {
137                &LogixTypeDescriptor {
138                    name: "string",
139                    doc: "a valid utf-8 string",
140                    value: LogixValueDescriptor::Native,
141                }
142            }
143
144            fn default_value() -> Option<Self> {
145                None
146            }
147
148            fn logix_parse<FS: LogixVfs>(p: &mut LogixParser<FS>) -> Result<Value<Self>> {
149                Ok(match p.next_token()? {
150                    (span, Token::Literal(Literal::Str(value))) => Value {
151                        value: <$type>::from(value.decode_str(&span)?),
152                        span,
153                    },
154                    (span, Token::Action(name)) => crate::action::for_string_data(name, span, p)
155                        .map(|Value { span, value }| Value { span, value: value.into() })?,
156                    (span, token) => return Err(ParseError::UnexpectedToken {
157                        span,
158                        while_parsing: "string",
159                        wanted: Wanted::LitStr,
160                        got_token: token.token_type_name(),
161                    }),
162                })
163            }
164        }
165    )*};
166}
167
168impl_for_str!(String, ShortStr);