Skip to main content

shiki/
raw.rs

1use std::{
2    collections::{BTreeMap, HashMap},
3    ops::Deref,
4    sync::Arc,
5};
6
7#[cfg(feature = "json")]
8use serde::Deserialize;
9
10#[derive(Debug, Clone)]
11pub enum RawString<'a> {
12    Borrowed(&'a str),
13    Owned(Arc<str>),
14}
15
16impl<'a> RawString<'a> {
17    pub const fn borrowed(value: &'a str) -> Self {
18        Self::Borrowed(value)
19    }
20}
21
22impl Deref for RawString<'_> {
23    type Target = str;
24
25    fn deref(&self) -> &Self::Target {
26        match self {
27            Self::Borrowed(value) => value,
28            Self::Owned(value) => value,
29        }
30    }
31}
32
33impl AsRef<str> for RawString<'_> {
34    fn as_ref(&self) -> &str {
35        self
36    }
37}
38
39impl From<String> for RawString<'static> {
40    fn from(value: String) -> Self {
41        Self::Owned(Arc::from(value))
42    }
43}
44
45impl<'a> From<&'a str> for RawString<'a> {
46    fn from(value: &'a str) -> Self {
47        Self::Borrowed(value)
48    }
49}
50
51#[cfg(feature = "json")]
52impl<'de, 'a> Deserialize<'de> for RawString<'a> {
53    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
54    where
55        D: serde::Deserializer<'de>,
56    {
57        String::deserialize(deserializer)
58            .map(|value| Self::Owned(Arc::from(value)))
59    }
60}
61
62#[derive(Debug, Clone)]
63pub enum RawList<'a, T> {
64    Borrowed(&'a [T]),
65    Owned(Vec<T>),
66}
67
68impl<T> Default for RawList<'_, T> {
69    fn default() -> Self {
70        Self::Owned(Vec::new())
71    }
72}
73
74impl<T> From<Vec<T>> for RawList<'static, T> {
75    fn from(value: Vec<T>) -> Self {
76        Self::Owned(value)
77    }
78}
79
80impl<'a, T> RawList<'a, T> {
81    pub const EMPTY: Self = Self::Borrowed(&[]);
82
83    pub const fn borrowed(values: &'a [T]) -> Self {
84        Self::Borrowed(values)
85    }
86}
87
88impl<T> Deref for RawList<'_, T> {
89    type Target = [T];
90
91    fn deref(&self) -> &Self::Target {
92        match self {
93            Self::Borrowed(values) => values,
94            Self::Owned(values) => values,
95        }
96    }
97}
98
99#[cfg(feature = "json")]
100impl<'de, 'a, T> Deserialize<'de> for RawList<'a, T>
101where
102    T: Deserialize<'de>,
103{
104    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
105    where
106        D: serde::Deserializer<'de>,
107    {
108        Vec::deserialize(deserializer).map(Self::Owned)
109    }
110}
111
112#[derive(Debug, Clone)]
113pub struct RawMapEntry<'a, T> {
114    pub key: &'a str,
115    pub value: T,
116}
117
118impl<T> Default for RawMap<'_, T> {
119    fn default() -> Self {
120        Self::Owned(BTreeMap::new())
121    }
122}
123
124impl<T> From<HashMap<String, T>> for RawMap<'static, T> {
125    fn from(value: HashMap<String, T>) -> Self {
126        Self::Owned(value.into_iter().collect())
127    }
128}
129
130impl<T> From<BTreeMap<String, T>> for RawMap<'static, T> {
131    fn from(value: BTreeMap<String, T>) -> Self {
132        Self::Owned(value)
133    }
134}
135
136impl<'a, T> RawMapEntry<'a, T> {
137    pub const fn new(key: &'a str, value: T) -> Self {
138        Self { key, value }
139    }
140}
141
142#[derive(Debug, Clone)]
143pub enum RawMap<'a, T> {
144    Borrowed(&'a [RawMapEntry<'a, T>]),
145    Owned(BTreeMap<String, T>),
146}
147
148impl<'a, T> RawMap<'a, T> {
149    pub const EMPTY: Self = Self::Borrowed(&[]);
150
151    pub const fn borrowed(values: &'a [RawMapEntry<'a, T>]) -> Self {
152        Self::Borrowed(values)
153    }
154
155    pub fn get(&self, key: &str) -> Option<&T> {
156        match self {
157            Self::Borrowed(values) => values
158                .iter()
159                .find(|entry| entry.key == key)
160                .map(|entry| &entry.value),
161            Self::Owned(values) => values.get(key),
162        }
163    }
164
165    pub fn is_empty(&self) -> bool {
166        match self {
167            Self::Borrowed(values) => values.is_empty(),
168            Self::Owned(values) => values.is_empty(),
169        }
170    }
171
172    pub fn iter(&self) -> RawMapIter<'_, 'a, T> {
173        match self {
174            Self::Borrowed(values) => RawMapIter::Borrowed(values.iter()),
175            Self::Owned(values) => RawMapIter::Owned(values.iter()),
176        }
177    }
178}
179
180#[cfg(feature = "json")]
181impl<'de, 'a, T> Deserialize<'de> for RawMap<'a, T>
182where
183    T: Deserialize<'de>,
184{
185    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
186    where
187        D: serde::Deserializer<'de>,
188    {
189        BTreeMap::deserialize(deserializer).map(Self::Owned)
190    }
191}
192
193pub enum RawMapIter<'b, 'a, T> {
194    Borrowed(std::slice::Iter<'b, RawMapEntry<'a, T>>),
195    Owned(std::collections::btree_map::Iter<'b, String, T>),
196}
197
198impl<'b, T> Iterator for RawMapIter<'b, '_, T> {
199    type Item = (&'b str, &'b T);
200
201    fn next(&mut self) -> Option<Self::Item> {
202        match self {
203            Self::Borrowed(values) => {
204                values.next().map(|entry| (entry.key, &entry.value))
205            }
206            Self::Owned(values) => {
207                values.next().map(|(key, value)| (key.as_str(), value))
208            }
209        }
210    }
211}