Skip to content

Commit

Permalink
docs(serde): Show how to use edit's ValueSerializer/ValueDeserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 24, 2023
1 parent 04a59e2 commit ed60e94
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/toml_edit/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ where
T::deserialize(deserializer)
}

/// Deserialization implementation for TOML.
/// Deserialization for TOML [documents][crate::Document].
pub struct Deserializer {
input: crate::Document,
}
Expand Down
26 changes: 25 additions & 1 deletion crates/toml_edit/src/de/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,31 @@ use serde::de::IntoDeserializer as _;
use crate::de::DatetimeDeserializer;
use crate::de::Error;

/// Deserialization implementation for TOML.
/// Deserialization implementation for TOML [values][crate::Value].
///
/// # Example
///
/// ```
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Config {
/// title: String,
/// owner: Owner,
/// }
///
/// #[derive(Deserialize)]
/// struct Owner {
/// name: String,
/// }
///
/// let value = r#"{ title = 'TOML Example', owner = { name = 'Lisa' } }"#;
/// let deserializer = value.parse::<toml_edit::de::ValueDeserializer>().unwrap();
/// let config = Config::deserialize(deserializer).unwrap();
///
/// assert_eq!(config.title, "TOML Example");
/// assert_eq!(config.owner.name, "Lisa");
/// ```
pub struct ValueDeserializer {
input: crate::Item,
validate_struct_keys: bool,
Expand Down
36 changes: 35 additions & 1 deletion crates/toml_edit/src/ser/value.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Error;

/// Serialization implementation for TOML.
/// 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 @@ -9,6 +9,40 @@ use super::Error;
///
/// 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 value = serde::Serialize::serialize(
/// &config,
/// toml_edit::ser::ValueSerializer::new()
/// ).unwrap();
/// println!("{}", value)
/// ```
#[derive(Default)]
#[non_exhaustive]
pub struct ValueSerializer {}
Expand Down

0 comments on commit ed60e94

Please sign in to comment.