from_reader_with_options

Function from_reader_with_options 

Source
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, decodes it to UTF-8, and streams events into the deserializer.

Notes on limits and large inputs

  • Parsing limits: Use Options::budget to constrain YAML complexity (events, nodes, nesting depth, total scalar bytes, number of documents, anchors, aliases, etc.). These limits are enforced during parsing and are enabled by default via Options::default().
  • Byte-level input cap: A hard cap on input bytes is enforced via Options::budget.max_reader_input_bytes. The default budget sets this to 256 MiB. You can override it by customizing Options::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());

// Cap the reader to at most 1 KiB of input bytes.
let capped = reader.take(1024);

// Tighten the parsing budget as well (optional).
let mut opts = Options::default();
opts.budget = Some(Budget { max_events: 10_000, ..Budget::default() });

let p: Point = serde_saphyr::from_reader_with_options(capped, 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_options iterator APIs.
  • If Options::budget is set and a limit is exceeded, an error is returned early.