pub trait ToToml {
// Provided methods
fn to_toml<'a>(&'a self, arena: &'a Arena) -> Result<Item<'a>, ToTomlError> { ... }
fn to_optional_toml<'a>(
&'a self,
arena: &'a Arena,
) -> Result<Option<Item<'a>>, ToTomlError> { ... }
}to-toml only.Expand description
Converts a Rust type into a TOML Item tree.
#[derive(Toml)] with #[toml(ToToml)] generates the implementation
(the derive defaults to FromToml without it). See the
Toml derive macro for the full set of attributes
and field options like skip_if and style.
For manual implementations, implement to_toml for
values that are always present, or
to_optional_toml for values that may be
absent (returning Ok(None) to omit the field). Default implementations
bridge between the two so only one needs to be provided.
The simplest entry point is to_string for default
formatting, or Formatting for format preservation
and customizing formatting defaults.
§Examples
use toml_spanner::Toml;
#[derive(Toml)]
#[toml(ToToml)]
struct Config {
name: String,
port: u16,
}
let config = Config { name: "app".into(), port: 8080 };
let output = toml_spanner::to_string(&config).unwrap();
assert!(output.contains("name = \"app\""));
assert!(output.contains("port = 8080"));Provided Methods§
Sourcefn to_toml<'a>(&'a self, arena: &'a Arena) -> Result<Item<'a>, ToTomlError>
fn to_toml<'a>(&'a self, arena: &'a Arena) -> Result<Item<'a>, ToTomlError>
Produces a TOML Item representing this value.
Override when the value is always present. The default delegates to
to_optional_toml and returns an error if
None is produced.