MetadataVisitor

Struct MetadataVisitor 

Source
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>

Source

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.

Source

pub fn into_inner(self) -> V

Get the inner visitor.

Trait Implementations§

Source§

impl<V> From<V> for MetadataVisitor<V>

Source§

fn from(inner: V) -> MetadataVisitor<V>

Converts to this type from the input type.
Source§

impl<V> Visitor for MetadataVisitor<V>
where V: Visitor,

Source§

fn begin(&mut self, region: &Region) -> Result<(), Error>

Source§

fn end(&mut self, region: &Region) -> Result<(), Error>

Source§

fn text(&mut self, text: &str) -> Result<(), Error>

Source§

fn line_break(&mut self) -> Result<(), Error>

Source§

fn code(&mut self, text: &str) -> Result<(), Error>

Source§

fn with_visited_str(self, s: &str) -> Result<Self, Error>
where Self: Sized,

Visit a Matthewdown string. Read more
Source§

fn with_parsed_str(self, parser: &Parser, s: &str) -> Result<Self, Error>
where Self: Sized,

Visit a Matthewdown string using a particular parser. Read more

Auto Trait Implementations§

§

impl<V> Freeze for MetadataVisitor<V>
where V: Freeze + ?Sized,

§

impl<V> RefUnwindSafe for MetadataVisitor<V>
where V: RefUnwindSafe + ?Sized,

§

impl<V> Send for MetadataVisitor<V>
where V: Send + ?Sized,

§

impl<V> Sync for MetadataVisitor<V>
where V: Sync + ?Sized,

§

impl<V> Unpin for MetadataVisitor<V>
where V: Unpin + ?Sized,

§

impl<V> UnwindSafe for MetadataVisitor<V>
where V: UnwindSafe + ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.