pub struct MetadataVisitor<V: ?Sized> { /* private fields */ }Expand description
A Matthewdown visitor that extracts all the metadata from a document.
An inner Matthewdown visitor, V, handles all the non-metadata content
in the document, while the MetadataVisitor consumes all of the metadata
in the document.
A MetadataVisitor can be created by calling the MeatadataVisitor::from
method on an inner Matthewdown visitor. It can then be used to visit a
Matthewdown document. The MetadataVisitor::deserialize_metadata can then
be used to deserialize any metadata into a type.
§Examples
Metadata is deserialized using the serde library, allowing it to be
extracted to custom structures.
use matthewdown::{NullVisitor, MetadataVisitor, Visitor};
#[derive(serde::Deserialize)]
struct Data
{
apple_shape: String,
banana_shape: String
}
let data: Data = MetadataVisitor::from(NullVisitor)
.with_visited_str(r#"
$$metadata
* `apple_shape` - `round`
* `banana_shape` - `long`"#)
.unwrap()
.deserialize_metadata()
.unwrap();
assert_eq!(data.apple_shape, "round");
assert_eq!(data.banana_shape, "long");The MetadataVisitor sits on top of another visitor, allowing metadata and
content to be extracted in a single pass.
use matthewdown::{HtmlVisitor, MetadataVisitor, Visitor};
let visitor = MetadataVisitor::from(HtmlVisitor::new())
.with_visited_str(r#"
Content
$$metadata
1. `One`
2. `Two`"#)
.unwrap();
let data: Vec<String> = visitor.deserialize_metadata().unwrap();
let html = visitor.into_inner().into_output();
assert_eq!(data, vec!["One".to_owned(), "Two".to_owned()]);
assert_eq!(html, "<p>Content</p>");Implementations§
Source§impl<V> MetadataVisitor<V>
impl<V> MetadataVisitor<V>
Sourcepub fn deserialize_metadata<'de, M>(&self) -> Result<M, Error>where
M: Deserialize<'de>,
pub fn deserialize_metadata<'de, M>(&self) -> Result<M, Error>where
M: Deserialize<'de>,
Deserialize the metadata collected while visiting the matthewdown.
This must be called after visiting is completed.
Sourcepub fn into_inner(self) -> V
pub fn into_inner(self) -> V
Get the inner visitor.