The YAML 1.2 Failsafe schema reads unquoted values like true and 007 as strings.
cargo add oneyamluse oneyaml::{Schema, Settings, Value}; fn main() -> Result<(), oneyaml::Error> { let yaml = "flags: [true, 007]\n"; let opts = Settings::new() .schema(Schema::Failsafe); let doc: Value = oneyaml::from_str_with(yaml, &opts)?; let flags = &doc["flags"]; println!("{:?}", flags[0].as_str()); println!("{:?}", flags[1].as_str()); Ok(()) }
The example is the full program below: unzip it and cargo run. To run only the code above, paste it into src/main.rs of a new cargo new example project and cargo run.
Both items print as strings:
Some("true") Some("007")
Full program
The download uses Rust 1.85 or later and reads this config.yaml:
flags: [true, 007]use oneyaml::{Schema, Settings, Value}; fn main() -> Result<(), Box<dyn std::error::Error>> { let yaml = std::fs::read_to_string("config.yaml")?; let settings = Settings::new().schema(Schema::Failsafe); let doc: Value = oneyaml::from_str_with(&yaml, &settings)?; let flags = &doc["flags"]; for index in 0..2 { println!("{:?}", flags[index].as_str()); } Ok(()) }
cargo run prints:
Some("true") Some("007")
Malformed YAML returns a parse error; Failsafe changes how scalar types are chosen, not which YAML syntax is valid.
Variations
Check a null-looking value. In the snippet at the top, replace the yaml line with this one. The same code prints Some("null") and Some("~").
let yaml = "flags: [null, ~]\n";Related recipes
The short answer
Pass Settings::new().schema(Schema::Failsafe) to from_str_with and read the YAML into a Value. In oneyaml-rust, Failsafe reads plain scalars (unquoted single values) as strings. The flags key still points to a list; only its two items change type compared with a default Core read. In particular, 007 stays "007" instead of becoming the number 7.
Do strings need quotes in YAML?
Not always. Under the YAML 1.2 Core schema, a plain word like ready is already a string, but true, 007, and null resolve to a boolean, an integer, and a null. If you control the source and only a few fields are text, quote those values in the file and leave the reader on Core. Here the ID is text and the other items still have their normal types:
flags: [true, "007", null]If you don’t control the file and the whole input is meant to be text, use Failsafe for that read rather than rewriting the YAML. This also affects plain mapping keys, not just values: a key written as 007 is a string key. The Failsafe schema still recognizes mappings and sequences; it’s not the same as reading the entire file into one string.
Does YAML support multiline strings?
Yes. A value can be a block scalar, where | preserves line breaks and > folds ordinary line breaks into spaces. Failsafe controls the scalar’s type, not how its text is decoded. If you need the exact source spelling, including quote marks, indentation, or comments, keep the source or use the document API instead of relying on Value to round-trip the file. Failsafe is useful for preserving 007 as a value; it doesn’t preserve the YAML presentation around it.