pub fn from_reader_with_options<'a, R: Read + 'a, T: DeserializeOwned>(
reader: R,
options: Options,
) -> Result<T, Error>Expand description
Deserialize a single YAML document from any std::io::Read with configurable Options.
This is the reader-based counterpart to from_str_with_options. It consumes a
byte-oriented reader and streams events into the deserializer. BOM-marked
UTF-8, UTF-16LE, and UTF-16BE inputs are transcoded to UTF-8 internally
before parsing. If no recognized BOM is present, the input bytes are
treated as UTF-8.
This method parses as it reads, without loading the entire input into memory first. Hence, budget limits protect against large (potentially malicious) input.
Notes on limits and large inputs
- Parsing limits: Use
Options::budgetto constrain YAML complexity (events, nodes, nesting depth, total scalar bytes, total comment bytes, number of documents, anchors, aliases, etc.). These limits are enforced during parsing and are enabled by default viaOptions::default(). - Byte-level input cap:
Budget::max_reader_input_bytesis enforced while reading. The default budget sets this to 256 MiB. You can override it by customizingOptions::budget. When the cap is exceeded, deserialization fails early with a budget error.
Example: limit raw input bytes and customize options
use std::io::{Read, Cursor};
use serde::Deserialize;
use serde_saphyr::{Budget, Options};
#[derive(Debug, Deserialize, PartialEq)]
struct Point { x: i32, y: i32 }
let yaml = "x: 3\ny: 4\n";
let reader = Cursor::new(yaml.as_bytes());
let opts = serde_saphyr::options! {
budget: serde_saphyr::budget! {
max_events: 10_000,
max_reader_input_bytes: Some(1024),
},
};
let p: Point = serde_saphyr::from_reader_with_options(reader, opts).unwrap();
assert_eq!(p, Point { x: 3, y: 4 });Error behavior
- If an empty document is provided (no content), a type-mismatch (eof) error is returned when attempting to deserialize into non-null-like targets.
- If the reader contains multiple documents, an error is returned suggesting the
read/read_with_optionsiterator APIs. - If
Options::budgetis set and a limit is exceeded, an error is returned early.