tags :
- reference
- rust
- serialization
aliases :
- toml- serde
- toml
- serde
date created : Tuesday, March 31st 2026, 12: 14: 44 am
date modified : Tuesday, March 31st 2026, 3: 53: 45 am
TOML & Serde - Config Parsing Referenceuse serde:: Deserialize ;
#[derive(Deserialize)]
struct Config {
vault: VaultConfig ,
modules: std:: collections:: HashMap < String , ModuleConfig > ,
}
#[derive(Deserialize)]
struct VaultConfig {
base_path: String ,
api_port: u16 ,
}
#[derive(Deserialize)]
struct ModuleConfig {
mode: String ,
path: String ,
append_under_header: Option < String > ,
fields: Vec < FieldConfig > ,
}
#[derive(Deserialize)]
struct FieldConfig {
name: String ,
#[serde(rename = "type" )]
field_type: String ,
prompt: String ,
source: Option < String > ,
}
let config: Config = toml:: from_str ( & std:: fs:: read_to_string ( path) ? ) ? ;
Function
Purpose
toml::from_str::<T>(s)
Deserialize TOML string into T
toml::to_string(&val)
Serialize to TOML string
toml::to_string_pretty(&val)
Serialize to pretty TOML
toml::Value
Dynamic TOML value type
Serde YAML (Frontmatter Generation)use serde:: Serialize ;
use serde_yaml;
#[derive(Serialize)]
struct CoffeeFrontmatter {
bean: String ,
yield_g: u32 ,
date: String ,
tags: Vec < String > ,
}
let fm = CoffeeFrontmatter {
bean: "Ethiopian Yirgacheffe" . into ( ) ,
yield_g: 36 ,
date: "2026-03-30" . into ( ) ,
tags: vec! [ "coffee" . into ( ) , "v60" . into ( ) ] ,
} ;
let yaml = serde_yaml:: to_string ( & fm) ? ;
let frontmatter = format! ( "---\n{}---\n" , yaml) ;
Function
Purpose
serde_yaml::to_string(&val)
Serialize to YAML string
serde_yaml::to_writer(w, &val)
Serialize to writer
serde_yaml::from_str::<T>(s)
Deserialize YAML string
serde_yaml::Value
Dynamic YAML value type
Serde JSON (API communication)use serde_json;
let files: serde_json:: Value = serde_json:: from_str ( & response_body) ? ;
let body = serde_json:: to_string ( & data) ? ;