Extractor

Struct Extractor 

Source
pub struct Extractor<'opt> { /* private fields */ }
Expand description

Holds configuration for how front-matter and data should be extracted from a string.

  1. Instantiate using Extractor::new() with the desired Splitter
  2. (Optional) Call .with_modifier() with a desired Modifier; repeat as necessary
  3. Call .extract() with a string to get back a tuple of front-matter and data

§Usage

§Example 1 (Markdown with TOML)

Input:

[meta]
field_one = 10
field_two = [2, 4]
+++

## Markdown example

This is an example markdown document that contains the following TOML front-matter:

    [meta]
    field_one = 10
    field_two = [2, 4]

Code:

let (front_matter, data) = Extractor::new(Splitter::DelimiterLine("+++")).extract(input);

Front-matter output:

[meta]
field_one = 10
field_two = [2, 4]

Data output:

## Markdown example

This is an example markdown document that contains the following TOML front-matter:

    [meta]
    field_one = 10
    field_two = [2, 4]

§Example 2 (SQL with YAML)

Input:

-- meta:
--   field_one: 10
--   field_two:
--     - 2
--     - 4
SELECT version();

Code:

let (front_matter, data) = Extractor::new(Splitter::LinePrefix("-- "))
    .with_modifier(Modifier::StripPrefix("-- "))
    .extract(input);

Front-matter output:

meta:
  field_one: 10
  field_two:
    - 2
    - 4

Data output:

SELECT version();

Implementations§

Source§

impl<'opt> Extractor<'opt>

Source

pub const fn new(splitter: Splitter<'opt>) -> Self

Instantiate a new Extractor config.

Source

pub fn with_modifier(&mut self, modifier: Modifier<'opt>) -> &mut Self

Add a modifier to the front-matter returned by .extract().

Source

pub fn extract<'input>( &self, input: &'input str, ) -> (Cow<'input, str>, &'input str)

Split the given input string into a tuple of front-matter and data, applying any modifiers specified with .with_modifier().

Trait Implementations§

Source§

impl<'opt> Debug for Extractor<'opt>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'opt> Freeze for Extractor<'opt>

§

impl<'opt> RefUnwindSafe for Extractor<'opt>

§

impl<'opt> Send for Extractor<'opt>

§

impl<'opt> Sync for Extractor<'opt>

§

impl<'opt> Unpin for Extractor<'opt>

§

impl<'opt> UnwindSafe for Extractor<'opt>

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