Recursive AI
oneyamlRustCookbook

oneyaml Rust cookbook

Short recipes for common YAML tasks in Rust with oneyaml, each a complete program that was compiled and run against oneyaml 1.2.0.

Recipes

Parse YAML into Rust structsRead a YAML file into Rust structs and enums with serde and oneyaml, with renamed fields, defaults, optional sections and an enum.Read YAML without a schemaRead YAML of unknown shape into oneyaml::Value in Rust, index it by key and position, and walk a mapping in file order.Edit YAML and keep its commentsChange a value, remove a list item and add a key with a comment in a YAML file from Rust, with every other byte and comment kept.Merge or patch YAMLApply a merge patch (RFC 7396) as an environment overlay and a guarded JSON Patch (RFC 6902) to a YAML file in Rust, keeping its comments.Convert YAML to JSON and backConvert YAML to JSON with oneyaml and serde_json in Rust, convert JSON back to YAML, and get an error instead of a silent change for values JSON can’t hold.Read Kubernetes manifests and multi-document YAMLRead every document of a multi-document YAML file such as a Kubernetes manifest into Rust structs, then edit one document and keep the rest byte for byte.Report errors with line and columnGet the Y#### code, line, column and path of a YAML error in Rust, print a rustc-style snippet, and collect every error in a file at once.Anchors, aliases and merge keysRead YAML anchors and aliases in Rust, turn on << merge keys for docker-compose-style files, and follow an alias to its anchor in a Document.Read untrusted YAML safelyRead YAML from the network in Rust with Limits::untrusted(), and refuse billion-laughs alias bombs and deep nesting with a clear error instead of running out of memory.Read YAML 1.1 files: yes, no and 0755Why oneyaml reads yes, no and 0755 the YAML 1.2 way in Rust, and how to read a YAML 1.1 file the old way with one setting or the Compat::yaml_1_1() preset.