1use alloc::{borrow::ToOwned, boxed::Box, format, string::String};
6use core::{fmt, ops::Range};
7
8use serde::{de, ser, Deserialize, Serialize};
9
10use crate::{
11 error::{Error, SpannedResult},
12 options::Options,
13};
14
15pub(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)]
21pub struct RawValue {
22 ron: str,
23}
24
25#[allow(unsafe_code)]
26impl RawValue {
27 fn from_borrowed_str(ron: &str) -> &Self {
28 unsafe { &*(ron as *const str as *const RawValue) }
30 }
31
32 fn from_boxed_str(ron: Box<str>) -> Box<Self> {
33 unsafe { core::mem::transmute::<Box<str>, Box<RawValue>>(ron) }
35 }
36
37 fn into_boxed_str(raw_value: Box<Self>) -> Box<str> {
38 unsafe { core::mem::transmute::<Box<RawValue>, Box<str>>(raw_value) }
40 }
41
42 #[allow(clippy::expect_used)]
43 fn trim_range(ron: &str) -> Range<usize> {
44 fn trim_range_inner(ron: &str) -> Result<Range<usize>, Error> {
45 let mut deserializer = crate::Deserializer::from_str(ron).map_err(Error::from)?;
46
47 deserializer.parser.skip_ws()?;
48 let start_offset = ron.len() - deserializer.parser.src().len();
49
50 let _ = serde::de::IgnoredAny::deserialize(&mut deserializer)?;
51
52 deserializer.parser.skip_ws()?;
53 let end_offset = deserializer.parser.pre_ws_src().len();
54
55 Ok(start_offset..(ron.len() - end_offset))
56 }
57
58 trim_range_inner(ron).expect("RawValue must contain valid ron")
59 }
60}
61
62impl Clone for Box<RawValue> {
63 fn clone(&self) -> Self {
64 (**self).to_owned()
65 }
66}
67
68impl ToOwned for RawValue {
69 type Owned = Box<RawValue>;
70
71 fn to_owned(&self) -> Self::Owned {
72 RawValue::from_boxed_str(self.ron.to_owned().into_boxed_str())
73 }
74}
75
76impl fmt::Debug for RawValue {
77 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78 f.debug_tuple("RawValue")
79 .field(&format_args!("{}", &self.ron))
80 .finish()
81 }
82}
83
84impl fmt::Display for RawValue {
85 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86 f.write_str(&self.ron)
87 }
88}
89
90impl RawValue {
91 #[must_use]
93 pub fn get_ron(&self) -> &str {
94 &self.ron
95 }
96
97 pub fn from_ron(ron: &str) -> SpannedResult<&Self> {
100 let mut deserializer = crate::Deserializer::from_str(ron)?;
101
102 if !deserializer.extensions().is_empty() {
104 return Err(deserializer.span_error(Error::Message(String::from(
105 "ron::value::RawValue cannot enable extensions",
106 ))));
107 }
108
109 let _ = <&Self>::deserialize(&mut deserializer).map_err(|e| deserializer.span_error(e))?;
110
111 deserializer.end().map_err(|e| deserializer.span_error(e))?;
112
113 Ok(Self::from_borrowed_str(ron))
114 }
115
116 pub fn from_boxed_ron(ron: Box<str>) -> SpannedResult<Box<Self>> {
119 match Self::from_ron(&ron) {
120 Ok(_) => Ok(Self::from_boxed_str(ron)),
121 Err(err) => Err(err),
122 }
123 }
124
125 pub fn into_rust<'de, T: Deserialize<'de>>(&'de self) -> SpannedResult<T> {
127 Options::default().from_str(&self.ron)
128 }
129
130 pub fn from_rust<T: Serialize>(value: &T) -> Result<Box<Self>, Error> {
132 let ron = Options::default().to_string(value)?;
133
134 Ok(RawValue::from_boxed_str(ron.into_boxed_str()))
135 }
136}
137
138impl RawValue {
139 #[must_use]
140 pub fn trim(&self) -> &Self {
143 Self::from_borrowed_str(&self.ron[RawValue::trim_range(&self.ron)])
144 }
145
146 #[must_use]
147 #[allow(unsafe_code)]
148 pub fn trim_boxed(self: Box<Self>) -> Box<Self> {
151 let trim_range = RawValue::trim_range(&self.ron);
152 let mut boxed_ron = RawValue::into_boxed_str(self).into_string();
153 unsafe {
156 boxed_ron.as_mut_vec().drain(trim_range.end..);
157 boxed_ron.as_mut_vec().drain(0..trim_range.start);
158 }
159 RawValue::from_boxed_str(boxed_ron.into_boxed_str())
160 }
161}
162
163impl From<Box<RawValue>> for Box<str> {
164 fn from(raw_value: Box<RawValue>) -> Self {
165 RawValue::into_boxed_str(raw_value)
166 }
167}
168
169impl Serialize for RawValue {
170 fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
171 serializer.serialize_newtype_struct(RAW_VALUE_TOKEN, &self.ron)
172 }
173}
174
175impl<'de: 'a, 'a> Deserialize<'de> for &'a RawValue {
176 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 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
183 write!(formatter, "any valid borrowed RON-value-string")
185 }
186
187 fn visit_borrowed_str<E: de::Error>(self, ron: &'de str) -> Result<Self::Value, E> {
188 match Options::default().from_str::<de::IgnoredAny>(ron) {
189 Ok(_) => Ok(RawValue::from_borrowed_str(ron)),
190 Err(err) => Err(de::Error::custom(format!("invalid RON value at {}", err))),
191 }
192 }
193
194 fn visit_newtype_struct<D: de::Deserializer<'de>>(
195 self,
196 deserializer: D,
197 ) -> Result<Self::Value, D::Error> {
198 deserializer.deserialize_str(self)
199 }
200 }
201
202 deserializer.deserialize_newtype_struct(RAW_VALUE_TOKEN, ReferenceVisitor)
203 }
204}
205
206impl<'de> Deserialize<'de> for Box<RawValue> {
207 fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
208 struct BoxedVisitor;
209
210 impl<'de> de::Visitor<'de> for BoxedVisitor {
211 type Value = Box<RawValue>;
212
213 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
214 write!(formatter, "any valid RON-value-string")
216 }
217
218 fn visit_str<E: de::Error>(self, ron: &str) -> Result<Self::Value, E> {
219 match Options::default().from_str::<de::IgnoredAny>(ron) {
220 Ok(_) => Ok(RawValue::from_boxed_str(ron.to_owned().into_boxed_str())),
221 Err(err) => Err(de::Error::custom(format!("invalid RON value at {}", err))),
222 }
223 }
224
225 fn visit_string<E: de::Error>(self, ron: String) -> Result<Self::Value, E> {
226 match Options::default().from_str::<de::IgnoredAny>(&ron) {
227 Ok(_) => Ok(RawValue::from_boxed_str(ron.into_boxed_str())),
228 Err(err) => Err(de::Error::custom(format!("invalid RON value at {}", err))),
229 }
230 }
231
232 fn visit_newtype_struct<D: de::Deserializer<'de>>(
233 self,
234 deserializer: D,
235 ) -> Result<Self::Value, D::Error> {
236 deserializer.deserialize_string(self)
237 }
238 }
239
240 deserializer.deserialize_newtype_struct(RAW_VALUE_TOKEN, BoxedVisitor)
241 }
242}