| 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 | fn from_borrowed_str(ron: &str) -> &Self { |
| 28 | // Safety: RawValue is a transparent newtype around str |
| 29 | unsafe { &*(ron as *const str as *const RawValue) } |
| 30 | } |
| 31 | |
| 32 | fn from_boxed_str(ron: Box<str>) -> Box<Self> { |
| 33 | // Safety: RawValue is a transparent newtype around str |
| 34 | unsafe { core::mem::transmute::<Box<str>, Box<RawValue>>(ron) } |
| 35 | } |
| 36 | |
| 37 | fn into_boxed_str(raw_value: Box<Self>) -> Box<str> { |
| 38 | // Safety: RawValue is a transparent newtype around str |
| 39 | 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 | |
| 62 | impl Clone for Box<RawValue> { |
| 63 | fn clone(&self) -> Self { |
| 64 | (**self).to_owned() |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | impl 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 | |
| 76 | impl 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 | |
| 84 | impl fmt::Display for RawValue { |
| 85 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 86 | f.write_str(&self.ron) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | impl RawValue { |
| 91 | /// Get the inner raw RON string, which is guaranteed to contain valid RON. |
| 92 | #[must_use] |
| 93 | pub fn get_ron(&self) -> &str { |
| 94 | &self.ron |
| 95 | } |
| 96 | |
| 97 | /// Helper function to validate a RON string and turn it into a |
| 98 | /// [`RawValue`]. |
| 99 | pub fn from_ron(ron: &str) -> SpannedResult<&Self> { |
| 100 | let mut deserializer = crate::Deserializer::from_str(ron)?; |
| 101 | |
| 102 | // raw values can be used everywhere but extensions cannot |
| 103 | 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 | /// Helper function to validate a RON string and turn it into a |
| 117 | /// [`RawValue`]. |
| 118 | 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 | /// Helper function to deserialize the inner RON string into `T`. |
| 126 | pub fn into_rust<'de, T: Deserialize<'de>>(&'de self) -> SpannedResult<T> { |
| 127 | Options::default().from_str(&self.ron) |
| 128 | } |
| 129 | |
| 130 | /// Helper function to serialize `value` into a RON string. |
| 131 | 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 | |
| 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 | 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 | /// Trims any leadning and trailing whitespace off the boxed raw RON string, |
| 149 | /// including whitespace characters and comments. |
| 150 | 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 | // SAFETY: ron[trim_range] is a valid str, so draining everything |
| 154 | // before and after leaves a valid str |
| 155 | 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 | |
| 163 | impl From<Box<RawValue>> for Box<str> { |
| 164 | fn from(raw_value: Box<RawValue>) -> Self { |
| 165 | RawValue::into_boxed_str(raw_value) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | impl 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 | |
| 175 | impl<'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 | // This error message only shows up with foreign Deserializers |
| 184 | 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 | |
| 206 | impl<'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 | // This error message only shows up with foreign Deserializers |
| 215 | 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 | } |