Skip to main content

extract_typed_borrowed

Function extract_typed_borrowed 

Source
pub fn extract_typed_borrowed<'a, T: Deserialize<'a>>(
    content: &'a str,
) -> Result<T, MetadataError>
Expand description

Deserialises the front matter into T while borrowing from content.

The zero-copy counterpart of extract_typed: a field typed &'a str (or Cow<'a, str>) points into content instead of being copied, for every format whose text already holds the value verbatim. Values that need unescaping (a YAML "line\nbreak", a JSON \u00e9) are materialised by the parser as owned strings, which is why a Cow<'a, str> field is the general choice.

§Errors

As extract_typed.

§Example

use metadata_gen::extract_typed_borrowed;
use serde::Deserialize;

#[derive(Deserialize)]
struct Post<'a> {
    title: &'a str,
    tags: Vec<&'a str>,
}

let doc = "{\"title\": \"Borrowed\", \"tags\": [\"a\", \"b\"]}\nbody";
let post: Post<'_> = extract_typed_borrowed(doc).unwrap();
assert_eq!(post.title, "Borrowed");
assert_eq!(post.tags, ["a", "b"]);