From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- third_party/rust/ron/src/options.rs | 183 ++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 third_party/rust/ron/src/options.rs (limited to 'third_party/rust/ron/src/options.rs') diff --git a/third_party/rust/ron/src/options.rs b/third_party/rust/ron/src/options.rs new file mode 100644 index 0000000000..1e35118122 --- /dev/null +++ b/third_party/rust/ron/src/options.rs @@ -0,0 +1,183 @@ +//! Roundtrip serde Options module. + +use std::io; + +use serde::{de, ser, Deserialize, Serialize}; + +use crate::de::Deserializer; +use crate::error::{Result, SpannedResult}; +use crate::extensions::Extensions; +use crate::ser::{PrettyConfig, Serializer}; + +/// Roundtrip serde options. +/// +/// # Examples +/// +/// ``` +/// use ron::{Options, extensions::Extensions}; +/// +/// let ron = Options::default() +/// .with_default_extension(Extensions::IMPLICIT_SOME); +/// +/// let de: Option = ron.from_str("42").unwrap(); +/// let ser = ron.to_string(&de).unwrap(); +/// +/// assert_eq!(ser, "42"); +/// ``` +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(default)] +#[non_exhaustive] +pub struct Options { + /// Extensions that are enabled by default during serialization and + /// deserialization. + /// During serialization, these extensions do NOT have to be explicitly + /// enabled in the parsed RON. + /// During deserialization, these extensions are used, but their explicit + /// activation is NOT included in the output RON. + /// No extensions are enabled by default. + pub default_extensions: Extensions, +} + +impl Default for Options { + fn default() -> Self { + Self { + default_extensions: Extensions::empty(), + } + } +} + +impl Options { + #[must_use] + /// Enable `default_extension` by default during serialization and deserialization. + pub fn with_default_extension(mut self, default_extension: Extensions) -> Self { + self.default_extensions |= default_extension; + self + } + + #[must_use] + /// Do NOT enable `default_extension` by default during serialization and deserialization. + pub fn without_default_extension(mut self, default_extension: Extensions) -> Self { + self.default_extensions &= !default_extension; + self + } +} + +impl Options { + /// A convenience function for building a deserializer + /// and deserializing a value of type `T` from a reader. + pub fn from_reader(&self, mut rdr: R) -> SpannedResult + where + R: io::Read, + T: de::DeserializeOwned, + { + let mut bytes = Vec::new(); + rdr.read_to_end(&mut bytes)?; + + self.from_bytes(&bytes) + } + + /// A convenience function for building a deserializer + /// and deserializing a value of type `T` from a string. + pub fn from_str<'a, T>(&self, s: &'a str) -> SpannedResult + where + T: de::Deserialize<'a>, + { + self.from_bytes(s.as_bytes()) + } + + /// A convenience function for building a deserializer + /// and deserializing a value of type `T` from bytes. + pub fn from_bytes<'a, T>(&self, s: &'a [u8]) -> SpannedResult + where + T: de::Deserialize<'a>, + { + self.from_bytes_seed(s, std::marker::PhantomData) + } + + /// A convenience function for building a deserializer + /// and deserializing a value of type `T` from a reader + /// and a seed. + pub fn from_reader_seed(&self, mut rdr: R, seed: S) -> SpannedResult + where + R: io::Read, + S: for<'a> de::DeserializeSeed<'a, Value = T>, + { + let mut bytes = Vec::new(); + rdr.read_to_end(&mut bytes)?; + + self.from_bytes_seed(&bytes, seed) + } + + /// A convenience function for building a deserializer + /// and deserializing a value of type `T` from a string + /// and a seed. + pub fn from_str_seed<'a, S, T>(&self, s: &'a str, seed: S) -> SpannedResult + where + S: de::DeserializeSeed<'a, Value = T>, + { + self.from_bytes_seed(s.as_bytes(), seed) + } + + /// A convenience function for building a deserializer + /// and deserializing a value of type `T` from bytes + /// and a seed. + pub fn from_bytes_seed<'a, S, T>(&self, s: &'a [u8], seed: S) -> SpannedResult + where + S: de::DeserializeSeed<'a, Value = T>, + { + let mut deserializer = Deserializer::from_bytes_with_options(s, self.clone())?; + + let value = seed + .deserialize(&mut deserializer) + .map_err(|e| deserializer.span_error(e))?; + + deserializer.end().map_err(|e| deserializer.span_error(e))?; + + Ok(value) + } + + /// Serializes `value` into `writer` + pub fn to_writer(&self, writer: W, value: &T) -> Result<()> + where + W: io::Write, + T: ?Sized + ser::Serialize, + { + let mut s = Serializer::with_options(writer, None, self.clone())?; + value.serialize(&mut s) + } + + /// Serializes `value` into `writer` in a pretty way. + pub fn to_writer_pretty(&self, writer: W, value: &T, config: PrettyConfig) -> Result<()> + where + W: io::Write, + T: ?Sized + ser::Serialize, + { + let mut s = Serializer::with_options(writer, Some(config), self.clone())?; + value.serialize(&mut s) + } + + /// Serializes `value` and returns it as string. + /// + /// This function does not generate any newlines or nice formatting; + /// if you want that, you can use `to_string_pretty` instead. + pub fn to_string(&self, value: &T) -> Result + where + T: ?Sized + ser::Serialize, + { + let mut output = Vec::new(); + let mut s = Serializer::with_options(&mut output, None, self.clone())?; + value.serialize(&mut s)?; + Ok(String::from_utf8(output).expect("Ron should be utf-8")) + } + + /// Serializes `value` in the recommended RON layout in a pretty way. + pub fn to_string_pretty(&self, value: &T, config: PrettyConfig) -> Result + where + T: ?Sized + ser::Serialize, + { + let mut output = Vec::new(); + let mut s = Serializer::with_options(&mut output, Some(config), self.clone())?; + value.serialize(&mut s)?; + Ok(String::from_utf8(output).expect("Ron should be utf-8")) + } +} -- cgit v1.2.3