/// Roundtrip serde options. /// /// # Examples /// /// ``` /// use ron::{Options, extensions::Extensions}; /// /// let ron = Options::default() /// .with_default_extension(Extensions::IMPLICIT_SOME); /// /// let de: Option<i32> = ron.from_str("42").unwrap(); /// let ser = ron.to_string(&de).unwrap(); /// /// assert_eq!(ser, "42"); /// ``` #[derive(Clone, Debug, Serialize, Deserialize)] // GRCOV_EXCL_LINE #[serde(default)] #[non_exhaustive] pubstruct 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, /// Default recursion limit that is checked during serialization and /// deserialization. /// If set to `None`, infinite recursion is allowed and stack overflow /// errors can crash the serialization or deserialization process. /// Defaults to `Some(128)`, i.e. 128 recursive calls are allowed. pub recursion_limit: Option<usize>,
}
impl Options { #[must_use] /// Enable `default_extension` by default during serialization and deserialization. pubfn with_default_extension(mutself, default_extension: Extensions) -> Self { self.default_extensions |= default_extension; self
}
#[must_use] /// Do NOT enable `default_extension` by default during serialization and deserialization. pubfn without_default_extension(mutself, default_extension: Extensions) -> Self { self.default_extensions &= !default_extension; self
}
#[must_use] /// Set a maximum recursion limit during serialization and deserialization. pubfn with_recursion_limit(mutself, recursion_limit: usize) -> Self { self.recursion_limit = Some(recursion_limit); self
}
#[must_use] /// Disable the recursion limit during serialization and deserialization. /// /// If you expect to handle highly recursive datastructures, consider wrapping /// `ron` with [`serde_stacker`](https://docs.rs/serde_stacker/latest/serde_stacker/). pubfn without_recursion_limit(mutself) -> Self { self.recursion_limit = None; self
}
}
impl Options { /// A convenience function for building a deserializer /// and deserializing a value of type `T` from a reader. #[cfg(feature = "std")] pubfn from_reader<R, T>(&self, rdr: R) -> SpannedResult<T> where
R: io::Read,
T: de::DeserializeOwned,
{ self.from_reader_seed(rdr, core::marker::PhantomData)
}
/// A convenience function for building a deserializer /// and deserializing a value of type `T` from a string. pubfn from_str<'a, T>(&self, s: &'a str) -> SpannedResult<T> where
T: de::Deserialize<'a>,
{ self.from_str_seed(s, core::marker::PhantomData)
}
/// A convenience function for building a deserializer /// and deserializing a value of type `T` from bytes. pubfn from_bytes<'a, T>(&self, s: &'a [u8]) -> SpannedResult<T> where
T: de::Deserialize<'a>,
{ self.from_bytes_seed(s, core::marker::PhantomData)
}
/// A convenience function for building a deserializer /// and deserializing a value of type `T` from a reader /// and a seed. // FIXME: panic is not actually possible, remove once utf8_chunks is stabilized #[allow(clippy::missing_panics_doc)] #[cfg(feature = "std")] pubfn from_reader_seed<R, S, T>(&self, mut rdr: R, seed: S) -> SpannedResult<T> where
R: io::Read,
S: for<'a> de::DeserializeSeed<'a, Value = T>,
{ letmut bytes = Vec::new();
// Try to compute a good error position for the I/O error // FIXME: use [`utf8_chunks`](https://github.com/rust-lang/rust/issues/99543) once stabilised #[allow(clippy::expect_used)] let valid_input = match core::str::from_utf8(&bytes) {
Ok(valid_input) => valid_input,
Err(err) => core::str::from_utf8(&bytes[..err.valid_up_to()])
.expect("source is valid up to error"),
};
/// A convenience function for building a deserializer /// and deserializing a value of type `T` from a string /// and a seed. pubfn from_str_seed<'a, S, T>(&self, s: &'a str, seed: S) -> SpannedResult<T> where
S: de::DeserializeSeed<'a, Value = T>,
{ letmut deserializer = Deserializer::from_str_with_options(s, self)?;
let value = seed
.deserialize(&mut deserializer)
.map_err(|e| deserializer.span_error(e))?;
/// A convenience function for building a deserializer /// and deserializing a value of type `T` from bytes /// and a seed. pubfn from_bytes_seed<'a, S, T>(&self, s: &'a [u8], seed: S) -> SpannedResult<T> where
S: de::DeserializeSeed<'a, Value = T>,
{ letmut deserializer = Deserializer::from_bytes_with_options(s, self)?;
let value = seed
.deserialize(&mut deserializer)
.map_err(|e| deserializer.span_error(e))?;
/// Serializes `value` into `writer`. /// /// This function does not generate any newlines or nice formatting; /// if you want that, you can use /// [`to_writer_pretty`][Self::to_writer_pretty] instead. pubfn to_writer<W, T>(&self, writer: W, value: &T) -> Result<()> where
W: fmt::Write,
T: ?Sized + ser::Serialize,
{ letmut s = Serializer::with_options(writer, None, self)?;
value.serialize(&mut s)
}
/// Serializes `value` into `writer` in a pretty way. pubfn to_writer_pretty<W, T>(&self, writer: W, value: &T, config: PrettyConfig) -> Result<()> where
W: fmt::Write,
T: ?Sized + ser::Serialize,
{ letmut s = Serializer::with_options(writer, Some(config), self)?;
value.serialize(&mut s)
}
/// Serializes `value` into `writer`. /// /// This function does not generate any newlines or nice formatting; /// if you want that, you can use /// [`to_io_writer_pretty`][Self::to_io_writer_pretty] instead. #[cfg(feature = "std")] pubfn to_io_writer<W, T>(&self, writer: W, value: &T) -> Result<()> where
W: io::Write,
T: ?Sized + ser::Serialize,
{ letmut adapter = Adapter {
writer,
error: Ok(()),
}; let result = self.to_writer(&mut adapter, value);
adapter.error?;
result
}
/// Serializes `value` into `writer` in a pretty way. #[cfg(feature = "std")] pubfn to_io_writer_pretty<W, T>(
&self,
writer: W,
value: &T,
config: PrettyConfig,
) -> Result<()> where
W: io::Write,
T: ?Sized + ser::Serialize,
{ letmut adapter = Adapter {
writer,
error: Ok(()),
}; let result = self.to_writer_pretty(&mut adapter, value, config);
adapter.error?;
result
}
/// 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`][Self::to_string_pretty] instead. pubfn to_string<T>(&self, value: &T) -> Result<String> where
T: ?Sized + ser::Serialize,
{ letmut output = String::new(); letmut s = Serializer::with_options(&mut output, None, self)?;
value.serialize(&mut s)?;
Ok(output)
}
/// Serializes `value` in the recommended RON layout in a pretty way. pubfn to_string_pretty<T>(&self, value: &T, config: PrettyConfig) -> Result<String> where
T: ?Sized + ser::Serialize,
{ letmut output = String::new(); letmut s = Serializer::with_options(&mut output, Some(config), self)?;
value.serialize(&mut s)?;
Ok(output)
}
}
// Adapter from io::Write to fmt::Write that keeps the error #[cfg(feature = "std")] struct Adapter<W: io::Write> {
writer: W,
error: io::Result<()>,
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.