expression

Bounded YAML input and validation

Reject oversized text before parsing and check decoded values. A byte limit alone cannot bound nesting, alias expansion, parser CPU or memory; use reviewed parser policies and isolation for untrusted documents. This example uses external crates; see the staged Cargo manifest. Expected stdout (not executed): 3 bad count too large Common mistake: Claiming a byte-size limit guarantees safe parsing of arbitrary YAML.

Formal Syntax & Structure

rust syntaxrust
input.len(); yaml_serde::from_str; domain checks

Working Example

Example Snippetrust
fn main() {    fn workers(input: &str) -> Result<u8, &'static str> {        if input.len() > 128 { return Err("too large"); }        #[derive(serde::Deserialize)]        struct Settings { workers: u8 }        let s: Settings = yaml_serde::from_str(input).map_err(|_| "bad yaml")?;        if !(1..=8).contains(&s.workers) { return Err("bad count"); }        Ok(s.workers)    }    println!("{}", workers("workers: 3").unwrap());    println!("{}", workers("workers: 0").unwrap_err());    println!("{}", workers(&" ".repeat(129)).unwrap_err());}
Rust Reference