pub fn decode<T: DeserializeOwned>(
input: &str,
options: &DecodeOptions,
) -> ToonResult<T>Expand description
Decode a TOON string into any deserializable type.
This function accepts any type implementing serde::Deserialize, including:
- Custom structs with
#[derive(Deserialize)] serde_json::Value- Built-in types (Vec, HashMap, etc.)
ยงExamples
With custom structs:
use serde::Deserialize;
use toon_format::{
decode,
DecodeOptions,
};
#[derive(Deserialize, Debug, PartialEq)]
struct User {
name: String,
age: u32,
}
let toon = "name: Alice\nage: 30";
let user: User = decode(toon, &DecodeOptions::default())?;
assert_eq!(user.name, "Alice");
assert_eq!(user.age, 30);With JSON values:
use serde_json::{
json,
Value,
};
use toon_format::{
decode,
DecodeOptions,
};
let input = "name: Alice\nage: 30";
let result: Value = decode(input, &DecodeOptions::default())?;
assert_eq!(result["name"], json!("Alice"));