Skip to main content

from_str

Function from_str 

Source
pub fn from_str<T>(s: &str) -> Result<T>
where T: for<'de> Deserialize<'de> + 'static,
Expand description

Deserialize YAML from a &str into a typed T.

Default entry point for typed deserialisation. Drives the streaming fast-path when the input matches its eligibility rules (no custom merge-key policy, no ignore-binary-tag mode, no registered policies); otherwise routes through the Value-AST loader. The choice is transparent — both paths produce identical results.

§Errors

Returns Error when:

  • Error::Parse / Error::ParseWithLocation — s is not well-formed YAML 1.2 (missing closing bracket, indentation mismatch, invalid escape, …).
  • Error::Deserialize — the document parses but does not match T’s shape (wrong scalar type, missing required field on a struct without #[serde(default)], unknown enum variant, …). A rejection inside a nested value names the field it is about — server.port: invalid type: string "x", expected u16 — alongside the source location (#353).
  • Error::DepthLimit / Error::DocumentTooLong / Error::AliasLimit — input exceeds the default ParserConfig safety budgets. Use from_str_with_config with ParserConfig::strict() for tighter limits or with relaxed limits if the defaults reject a known-good document.
  • Error::DuplicateKey — only when duplicate_key_policy has been switched to Error. The default policy is Last, which deduplicates without erroring.
  • Error::Custom — surface for upstream serde_core::de::Error conversions; ordinarily the more specific variants above are produced first.

§Examples

let n: i32 = noyalib::from_str("42").unwrap();
assert_eq!(n, 42);