oneyaml is a free, open-source YAML library for Rust from Recursive AI, licensed MIT or Apache-2.0. crates.io · GitHub
cargo add oneyaml@=1.2.0
Keep your Serde derives and swap the crate for typed reads and writes. To check values, output, and errors before dropping serde_yaml 0.9.34+deprecated, put these entries under [dependencies] in a Rust binary crate.
oneyaml = "=1.2.0" serde = { version = "=1.0.228", features = ["derive"] } serde_yaml = "=0.9.34"
For the same Config and input, replace the first line with the second:
let config: Config = serde_yaml::from_str(input)?; let yaml = serde_yaml::to_string(&config)?; let config: Config = oneyaml::from_str(input)?; let yaml = oneyaml::to_string(&config)?;
Save the following as src/main.rs and run cargo run.
use serde::{Deserialize, Serialize}; #[derive(Debug, PartialEq, Deserialize, Serialize)] struct Config { tags: Vec<String>, } fn main() -> Result<(), Box<dyn std::error::Error>> { let input = "tags:\n- api\n"; let old: Config = serde_yaml::from_str(input)?; let new: Config = oneyaml::from_str(input)?; let compat = oneyaml::Compat::serde_yaml(); assert_eq!(old, new); println!("serde_yaml: {:?}", serde_yaml::to_string(&old)?); println!("oneyaml: {:?}", oneyaml::to_string(&new)?); println!("compat: {:?}", oneyaml::to_string_with(&new, &compat)?); let input = "mask: 0b1010\n"; let old: serde_yaml::Value = serde_yaml::from_str(input)?; let new: oneyaml::Value = oneyaml::from_str(input)?; let migrated: oneyaml::Value = oneyaml::from_str_with(input, &compat)?; println!("binary mask: {:?} / {:?} / {:?}", old["mask"].as_u64(), new["mask"].as_str(), migrated["mask"].as_u64()); let duplicates = "port: 80\nport: 443\n"; let old_rejects = serde_yaml::from_str::<serde_yaml::Value>(duplicates).is_err(); let error = oneyaml::from_str::<oneyaml::Value>(duplicates).unwrap_err(); let migrated: oneyaml::Value = oneyaml::from_str_with(duplicates, &compat)?; println!("duplicates: old rejects = {old_rejects}, new = {}, compat = {}", error.code(), migrated["port"].as_u64().unwrap()); Ok(()) }
serde_yaml: "tags:\n- api\n" oneyaml: "tags:\n - api\n" compat: "tags:\n- api\n" binary mask: Some(10) / Some("0b1010") / Some(10) duplicates: old rejects = true, new = Y1013, compat = 443
The two typed reads produce the same Config. The default writer indents the list, while the compatibility writer matches the old output for this fixture. The binary mask becomes a string under the default settings. The preset restores the integer but also accepts the second port value, which both default reads rejected. Y1013 is oneyaml’s duplicate-key error code. Pass the compatibility settings only to the calls whose old behavior you need to keep.
Map the calls before swapping imports
Use the serde_yaml 0.9.34 API and oneyaml 1.2.0 migration map to check any call that does more than a typed string round trip.
| serde_yaml | oneyaml 1.2.0 |
|---|---|
from_str, from_reader | Same entry-point names. Use the _with variants to pass settings. |
from_slice into a borrowed type | from_slice requires DeserializeOwned. For borrowed fields in UTF-8 input, validate with std::str::from_utf8 and then use from_str. |
to_string, to_writer | Same names. Use to_string_with or to_writer_with and the compatibility settings where old layout matters. Check golden files; this is not a byte-for-byte guarantee for all inputs. |
Value, from_value, to_value | Same names under the new crate. The Value types are distinct; switch imports and check scalar and tag behavior. |
value::Tag, value::TaggedValue | oneyaml::Tag, oneyaml::TaggedValue. |
Deserializer::from_str for a stream of documents | Same iteration entry point. Test the complete stream, not just the first document. |
Error, Location | Same type names under the new crate. Use Error::code() for assertions on oneyaml errors rather than matching the displayed sentence. |
A checklist for production fixtures
- Start with default settings. Exercise real configs with missing fields, quoted scalars, and numeric spellings. Include both typed reads and
Valuereads if your app uses them. Include tagged enum variants with scalar payloads: the preset opts into resolving those payloads for Serde. - Keep the duplicate-key policy explicit. Leave defaults on if you want duplicate keys rejected. If you need the rest of the preset, override its
duplicate_keyssetting withDuplicateKeys::Erroror inspect warnings withfrom_str_with_diagnosticsif you intentionally accept last-wins behavior. - Review merge and alias handling separately. Include a file that uses
<<if your old path expanded merges. The compatibility preset is not the switch for merge expansion. See the merge-key migration notes for the explicit settings and value-tree operation. - Test output where it is consumed. Compare bytes for golden files or signed artifacts and read back the emitted YAML to test actual values. For edits that must keep existing comments, use the document API rather than rewriting a Serde struct.
Remove the old dependency once callers and fixtures agree. If you are still choosing a replacement rather than porting code, start with the serde_yaml deprecation guide.