1
// Inspired by David Tolnay's serde-rs
2
// https://github.com/serde-rs/json/blob/master/src/raw.rs
3
// Licensed under either of Apache License, Version 2.0 or MIT license at your option.
4

            
5
use alloc::{borrow::ToOwned, boxed::Box, format, string::String};
6
use core::{fmt, ops::Range};
7

            
8
use serde::{de, ser, Deserialize, Serialize};
9

            
10
use crate::{
11
    error::{Error, SpannedResult},
12
    options::Options,
13
};
14

            
15
// NOTE: Keep synchronised with fuzz/arbitrary::typed_data::RAW_VALUE_TOKEN
16
pub(crate) const RAW_VALUE_TOKEN: &str = "$ron::private::RawValue";
17

            
18
#[allow(clippy::module_name_repetitions)]
19
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
20
#[repr(transparent)]
21
pub struct RawValue {
22
    ron: str,
23
}
24

            
25
#[allow(unsafe_code)]
26
impl RawValue {
27
22518
    fn from_borrowed_str(ron: &str) -> &Self {
28
22518
        // Safety: RawValue is a transparent newtype around str
29
22518
        unsafe { &*(ron as *const str as *const RawValue) }
30
22518
    }
31

            
32
16958
    fn from_boxed_str(ron: Box<str>) -> Box<Self> {
33
16958
        // Safety: RawValue is a transparent newtype around str
34
16958
        unsafe { core::mem::transmute::<Box<str>, Box<RawValue>>(ron) }
35
16958
    }
36

            
37
3892
    fn into_boxed_str(raw_value: Box<Self>) -> Box<str> {
38
3892
        // Safety: RawValue is a transparent newtype around str
39
3892
        unsafe { core::mem::transmute::<Box<RawValue>, Box<str>>(raw_value) }
40
3892
    }
41

            
42
    #[allow(clippy::expect_used)]
43
7228
    fn trim_range(ron: &str) -> Range<usize> {
44
7228
        fn trim_range_inner(ron: &str) -> Result<Range<usize>, Error> {
45
7228
            let mut deserializer = crate::Deserializer::from_str(ron).map_err(Error::from)?;
46

            
47
7228
            deserializer.parser.skip_ws()?;
48
7228
            let start_offset = ron.len() - deserializer.parser.src().len();
49
7228

            
50
7228
            let _ = serde::de::IgnoredAny::deserialize(&mut deserializer)?;
51

            
52
7228
            deserializer.parser.skip_ws()?;
53
7228
            let end_offset = deserializer.parser.pre_ws_src().len();
54
7228

            
55
7228
            Ok(start_offset..(ron.len() - end_offset))
56
7228
        }
57

            
58
7228
        trim_range_inner(ron).expect("RawValue must contain valid ron")
59
7228
    }
60
}
61

            
62
impl Clone for Box<RawValue> {
63
3336
    fn clone(&self) -> Self {
64
3336
        (**self).to_owned()
65
3336
    }
66
}
67

            
68
impl ToOwned for RawValue {
69
    type Owned = Box<RawValue>;
70

            
71
4448
    fn to_owned(&self) -> Self::Owned {
72
4448
        RawValue::from_boxed_str(self.ron.to_owned().into_boxed_str())
73
4448
    }
74
}
75

            
76
impl fmt::Debug for RawValue {
77
834
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78
834
        f.debug_tuple("RawValue")
79
834
            .field(&format_args!("{}", &self.ron))
80
834
            .finish()
81
834
    }
82
}
83

            
84
impl fmt::Display for RawValue {
85
278
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86
278
        f.write_str(&self.ron)
87
278
    }
88
}
89

            
90
impl RawValue {
91
    /// Get the inner raw RON string, which is guaranteed to contain valid RON.
92
    #[must_use]
93
13066
    pub fn get_ron(&self) -> &str {
94
13066
        &self.ron
95
13066
    }
96

            
97
    /// Helper function to validate a RON string and turn it into a
98
    /// [`RawValue`].
99
9730
    pub fn from_ron(ron: &str) -> SpannedResult<&Self> {
100
9730
        let mut deserializer = crate::Deserializer::from_str(ron)?;
101

            
102
        // raw values can be used everywhere but extensions cannot
103
9730
        if !deserializer.extensions().is_empty() {
104
556
            return Err(deserializer.span_error(Error::Message(String::from(
105
556
                "ron::value::RawValue cannot enable extensions",
106
556
            ))));
107
9174
        }
108
9174

            
109
9174
        let _ = <&Self>::deserialize(&mut deserializer).map_err(|e| deserializer.span_error(e))?;
110

            
111
8062
        deserializer.end().map_err(|e| deserializer.span_error(e))?;
112

            
113
7784
        Ok(Self::from_borrowed_str(ron))
114
9730
    }
115

            
116
    /// Helper function to validate a RON string and turn it into a
117
    /// [`RawValue`].
118
2780
    pub fn from_boxed_ron(ron: Box<str>) -> SpannedResult<Box<Self>> {
119
2780
        match Self::from_ron(&ron) {
120
2224
            Ok(_) => Ok(Self::from_boxed_str(ron)),
121
556
            Err(err) => Err(err),
122
        }
123
2780
    }
124

            
125
    /// Helper function to deserialize the inner RON string into `T`.
126
8
    pub fn into_rust<'de, T: Deserialize<'de>>(&'de self) -> SpannedResult<T> {
127
8
        Options::default().from_str(&self.ron)
128
8
    }
129

            
130
    /// Helper function to serialize `value` into a RON string.
131
8
    pub fn from_rust<T: Serialize>(value: &T) -> Result<Box<Self>, Error> {
132
8
        let ron = Options::default().to_string(value)?;
133

            
134
8
        Ok(RawValue::from_boxed_str(ron.into_boxed_str()))
135
8
    }
136
}
137

            
138
impl RawValue {
139
    #[must_use]
140
    /// Trims any leadning and trailing whitespace off the raw RON string,
141
    /// including whitespace characters and comments.
142
3614
    pub fn trim(&self) -> &Self {
143
3614
        Self::from_borrowed_str(&self.ron[RawValue::trim_range(&self.ron)])
144
3614
    }
145

            
146
    #[must_use]
147
    #[allow(unsafe_code)]
148
    /// Trims any leadning and trailing whitespace off the boxed raw RON string,
149
    /// including whitespace characters and comments.
150
3614
    pub fn trim_boxed(self: Box<Self>) -> Box<Self> {
151
3614
        let trim_range = RawValue::trim_range(&self.ron);
152
3614
        let mut boxed_ron = RawValue::into_boxed_str(self).into_string();
153
3614
        // SAFETY: ron[trim_range] is a valid str, so draining everything
154
3614
        //         before and after leaves a valid str
155
3614
        unsafe {
156
3614
            boxed_ron.as_mut_vec().drain(trim_range.end..);
157
3614
            boxed_ron.as_mut_vec().drain(0..trim_range.start);
158
3614
        }
159
3614
        RawValue::from_boxed_str(boxed_ron.into_boxed_str())
160
3614
    }
161
}
162

            
163
impl From<Box<RawValue>> for Box<str> {
164
278
    fn from(raw_value: Box<RawValue>) -> Self {
165
278
        RawValue::into_boxed_str(raw_value)
166
278
    }
167
}
168

            
169
impl Serialize for RawValue {
170
88
    fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
171
88
        serializer.serialize_newtype_struct(RAW_VALUE_TOKEN, &self.ron)
172
88
    }
173
}
174

            
175
impl<'de: 'a, 'a> Deserialize<'de> for &'a RawValue {
176
13078
    fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
177
        struct ReferenceVisitor;
178

            
179
        impl<'de> de::Visitor<'de> for ReferenceVisitor {
180
            type Value = &'de RawValue;
181

            
182
556
            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
183
556
                // This error message only shows up with foreign Deserializers
184
556
                write!(formatter, "any valid borrowed RON-value-string")
185
556
            }
186

            
187
11124
            fn visit_borrowed_str<E: de::Error>(self, ron: &'de str) -> Result<Self::Value, E> {
188
11124
                match Options::default().from_str::<de::IgnoredAny>(ron) {
189
11120
                    Ok(_) => Ok(RawValue::from_borrowed_str(ron)),
190
4
                    Err(err) => Err(de::Error::custom(format!(
191
4
                        "invalid RON value at {}: {}",
192
4
                        err.position, err.code
193
4
                    ))),
194
                }
195
11124
            }
196

            
197
8
            fn visit_newtype_struct<D: de::Deserializer<'de>>(
198
8
                self,
199
8
                deserializer: D,
200
8
            ) -> Result<Self::Value, D::Error> {
201
8
                deserializer.deserialize_str(self)
202
8
            }
203
        }
204

            
205
13078
        deserializer.deserialize_newtype_struct(RAW_VALUE_TOKEN, ReferenceVisitor)
206
13078
    }
207
}
208

            
209
impl<'de> Deserialize<'de> for Box<RawValue> {
210
120
    fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
211
        struct BoxedVisitor;
212

            
213
        impl<'de> de::Visitor<'de> for BoxedVisitor {
214
            type Value = Box<RawValue>;
215

            
216
1668
            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
217
1668
                // This error message only shows up with foreign Deserializers
218
1668
                write!(formatter, "any valid RON-value-string")
219
1668
            }
220

            
221
88
            fn visit_str<E: de::Error>(self, ron: &str) -> Result<Self::Value, E> {
222
88
                match Options::default().from_str::<de::IgnoredAny>(ron) {
223
84
                    Ok(_) => Ok(RawValue::from_boxed_str(ron.to_owned().into_boxed_str())),
224
4
                    Err(err) => Err(de::Error::custom(format!(
225
4
                        "invalid RON value at {}: {}",
226
4
                        err.position, err.code
227
4
                    ))),
228
                }
229
88
            }
230

            
231
8
            fn visit_string<E: de::Error>(self, ron: String) -> Result<Self::Value, E> {
232
8
                match Options::default().from_str::<de::IgnoredAny>(&ron) {
233
4
                    Ok(_) => Ok(RawValue::from_boxed_str(ron.into_boxed_str())),
234
4
                    Err(err) => Err(de::Error::custom(format!(
235
4
                        "invalid RON value at {}: {}",
236
4
                        err.position, err.code
237
4
                    ))),
238
                }
239
8
            }
240

            
241
32
            fn visit_newtype_struct<D: de::Deserializer<'de>>(
242
32
                self,
243
32
                deserializer: D,
244
32
            ) -> Result<Self::Value, D::Error> {
245
32
                deserializer.deserialize_string(self)
246
32
            }
247
        }
248

            
249
120
        deserializer.deserialize_newtype_struct(RAW_VALUE_TOKEN, BoxedVisitor)
250
120
    }
251
}