1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use std::{borrow::Cow, ffi::OsStr, fmt, path::Path};

use logix_vfs::LogixVfs;
use smol_str::SmolStr;

use crate::{
    error::{ParseError, Result, Wanted},
    parser::LogixParser,
    token::{Literal, Token},
    type_trait::{LogixTypeDescriptor, LogixValueDescriptor, Value},
    LogixType,
};

/// Represents a short string, will not need allocation for typical identifiers
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ShortStr {
    value: SmolStr,
}

impl From<String> for ShortStr {
    fn from(value: String) -> Self {
        Self {
            value: value.into(),
        }
    }
}

impl From<SmolStr> for ShortStr {
    fn from(value: SmolStr) -> Self {
        Self { value }
    }
}

impl<'a> From<&'a str> for ShortStr {
    fn from(value: &'a str) -> Self {
        Self {
            value: value.into(),
        }
    }
}

impl<'a> From<Cow<'a, str>> for ShortStr {
    fn from(value: Cow<'a, str>) -> Self {
        Self {
            value: value.into(),
        }
    }
}

impl AsRef<Path> for ShortStr {
    fn as_ref(&self) -> &Path {
        self.value.as_str().as_ref()
    }
}

impl AsRef<OsStr> for ShortStr {
    fn as_ref(&self) -> &OsStr {
        self.value.as_str().as_ref()
    }
}

impl AsRef<str> for ShortStr {
    fn as_ref(&self) -> &str {
        self.value.as_str()
    }
}

impl std::ops::Deref for ShortStr {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl fmt::Display for ShortStr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self.value.as_str(), f)
    }
}

impl fmt::Debug for ShortStr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self.value.as_str(), f)
    }
}

macro_rules! impl_for_str {
    ($($type:ty),+) => {$(
        impl LogixType for $type {
            fn descriptor() -> &'static LogixTypeDescriptor {
                &LogixTypeDescriptor {
                    name: "string",
                    doc: "a valid utf-8 string",
                    value: LogixValueDescriptor::Native,
                }
            }

            fn default_value() -> Option<Self> {
                None
            }

            fn logix_parse<FS: LogixVfs>(p: &mut LogixParser<FS>) -> Result<Value<Self>> {
                Ok(match p.next_token()? {
                    (span, Token::Literal(Literal::Str(value))) => Value {
                        value: <$type>::from(value.decode_str(&span)?),
                        span,
                    },
                    (span, Token::Action(name)) => crate::action::for_string_data(name, span, p)
                        .map(|Value { span, value }| Value { span, value: value.into() })?,
                    (span, token) => return Err(ParseError::UnexpectedToken {
                        span,
                        while_parsing: "string",
                        wanted: Wanted::LitStr,
                        got_token: token.token_type_name(),
                    }),
                })
            }
        }
    )*};
}

impl_for_str!(String, ShortStr);