Crate serde_taml

Crate serde_taml 

Source
Expand description

Serde-adapter for the taml crate.

For TAML syntax help, see the latter’s documentation and https://taml.schichler.dev.

§Features

§"serde-object-assist"

Iff this features is enabled, serde_taml supports variant spying compatible with serde-object@0.0.0-alpha.0.

It’s a bit of a hack, but you can deserialize unknown enums through the Serde API this way.

§Expecting data literals

Data literals are bytes towards Serde. Use for example the serde_bytes crate as follows:

use serde::Deserialize;
use serde_taml::de::from_taml_str;
use std::borrow::Cow;

#[derive(Deserialize)]
struct Configuration {
    #[serde(with = "serde_bytes")]
    binary_data: Vec<u8>,
}

let mut diagnostics = vec![]; // Alternatively `()` to skip reporting, though ideally always print diagnostics.
let configuration: Result<Configuration, _> = from_taml_str(
    "binary_data: <UTF-8:Arbitrary string input!>",
    &mut diagnostics,
    &[("UTF-8", &|str| Ok(Cow::Borrowed(str.as_bytes())))], // Encodings are consumer-defined.
);

assert_eq!(
    configuration.unwrap().binary_data,
    "Arbitrary string input!".as_bytes(),
);

§Type overrides

serde_taml (usually) infers a single expected TAML type based on the requested Serde type, but it’s possible to replace this expectation with a different one in many cases!

This can for example be used to parse arbitrary precision decimals:

use scientific::Scientific;
use serde::Deserialize;
use serde_taml::de::{
    from_taml_str,
    type_overrides::from_decimal,
};
use std::borrow::Cow;

#[derive(Deserialize)]
struct Precise {
    #[serde(deserialize_with = "from_decimal")]
    pi: Scientific, // Requests a string.
}

let mut diagnostics = vec![];
let configuration: Result<Precise, _> = from_taml_str(
    "pi: 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679",
    &mut diagnostics,
    &[],
);

assert_eq!(
    configuration.unwrap().pi.to_string(),
    "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679",
);

See de::type_overrides for the compatibility table and standard mappings.

Re-exports§

pub use taml;

Modules§

de
Serde-compatible TAML deserialisation facilities.

Functions§

validate
Validate the crate’s assumptions about String memory layout.