Skip to content

Commit

Permalink
docs(toml): Help people serialize/deserialize values
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 23, 2023
1 parent 44d2ee8 commit 2069c72
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
30 changes: 29 additions & 1 deletion crates/toml/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/// This function will attempt to interpret `s` as a TOML document and
/// deserialize `T` from the document.
///
/// To deserializes TOML values, instead of documents, see [`ValueDeserializer`].
///
/// # Examples
///
/// ```
Expand Down Expand Up @@ -98,6 +100,8 @@ impl std::fmt::Display for Error {
impl std::error::Error for Error {}

/// Deserialization TOML document
///
/// To deserializes TOML values, instead of documents, see [`ValueDeserializer`].
#[cfg(feature = "parse")]
pub struct Deserializer<'a> {
input: &'a str,
Expand Down Expand Up @@ -200,7 +204,31 @@ impl<'de, 'a> serde::Deserializer<'de> for Deserializer<'a> {
}
}

/// Deserialization TOML value
/// Deserialization TOML [value][crate::Value]
///
/// # Example
///
/// ```
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Config {
/// title: String,
/// owner: Owner,
/// }
///
/// #[derive(Deserialize)]
/// struct Owner {
/// name: String,
/// }
///
/// let config = Config::deserialize(toml::de::ValueDeserializer::new(
/// r#"{ title = 'TOML Example', owner = { name = 'Lisa' } }"#
/// )).unwrap();
///
/// assert_eq!(config.title, "TOML Example");
/// assert_eq!(config.owner.name, "Lisa");
/// ```
#[cfg(feature = "parse")]
pub struct ValueDeserializer<'a> {
input: &'a str,
Expand Down
43 changes: 42 additions & 1 deletion crates/toml/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
/// fail, if `T` contains a map with non-string keys, or if `T` attempts to
/// serialize an unsupported datatype such as an enum, tuple, or tuple struct.
///
/// To serialize TOML values, instead of documents, see [`ValueSerializer`].
///
/// # Examples
///
/// ```
Expand Down Expand Up @@ -55,6 +57,8 @@ where
///
/// This is identical to `to_string` except the output string has a more
/// "pretty" output. See `Serializer::pretty` for more details.
///
/// To serialize TOML values, instead of documents, see [`ValueSerializer`].
#[cfg(feature = "display")]
pub fn to_string_pretty<T: ?Sized>(value: &T) -> Result<String, Error>
where
Expand Down Expand Up @@ -129,6 +133,8 @@ impl std::error::Error for Error {}
///
/// Currently a serializer always writes its output to an in-memory `String`,
/// which is passed in when creating the serializer itself.
///
/// To serialize TOML values, instead of documents, see [`ValueSerializer`].
#[non_exhaustive]
#[cfg(feature = "display")]
pub struct Serializer<'d> {
Expand Down Expand Up @@ -472,7 +478,7 @@ impl<'d> serde::ser::Serializer for Serializer<'d> {
}
}

/// Serialization for TOML values.
/// Serialization for TOML [values][crate::Value].
///
/// This structure implements serialization support for TOML to serialize an
/// arbitrary type to TOML. Note that the TOML format does not support all
Expand All @@ -481,6 +487,41 @@ impl<'d> serde::ser::Serializer for Serializer<'d> {
///
/// Currently a serializer always writes its output to an in-memory `String`,
/// which is passed in when creating the serializer itself.
///
/// # Examples
///
/// ```
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct Config {
/// database: Database,
/// }
///
/// #[derive(Serialize)]
/// struct Database {
/// ip: String,
/// port: Vec<u16>,
/// connection_max: u32,
/// enabled: bool,
/// }
///
/// let config = Config {
/// database: Database {
/// ip: "192.168.1.1".to_string(),
/// port: vec![8001, 8002, 8003],
/// connection_max: 5000,
/// enabled: false,
/// },
/// };
///
/// let mut value = String::new();
/// serde::Serialize::serialize(
/// &config,
/// toml::ser::ValueSerializer::new(&mut value)
/// ).unwrap();
/// println!("{}", value)
/// ```
#[non_exhaustive]
#[cfg(feature = "display")]
pub struct ValueSerializer<'d> {
Expand Down

0 comments on commit 2069c72

Please sign in to comment.