1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
#![warn(clippy::cargo, clippy::nursery, clippy::pedantic)]
//! A Rust library that allows a user to extract an arbitrary number of lines of "front-matter" from the start of any
//! multiline string.
//!
//! Note that absolutely no parsing of extracted front-matter is performed; this is designed to output its results for
//! another library to then parse.
//!
//! See [`Extractor`] for how to use this library.
pub mod config;
mod extraction;
mod modification;
use crate::config::{Modifier, Splitter};
use crate::extraction::{extract_by_delimiter_line, extract_by_line_index, extract_by_line_prefix};
use crate::modification::{strip_first_line, strip_last_line, strip_prefix, trim_whitespace};
use std::borrow::Cow;
/// 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
///
/// [`.with_modifier()`]: Self::with_modifier
/// [`.extract()`]: Self::extract
///
/// # Usage
///
/// ## Example 1 (Markdown with TOML)
///
/// Input:
///
/// ```md
/// [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:
///
/// ```rust
/// # use extract_frontmatter::config::Splitter;
/// # use extract_frontmatter::Extractor;
/// # let input = include_str!("../resources/examples/example1.md");
/// let (front_matter, data) = Extractor::new(Splitter::DelimiterLine(String::from("+++")))
/// .extract(input);
/// # assert_eq!(front_matter.trim(), include_str!("../resources/examples/example1-meta.toml").trim());
/// # assert_eq!(data.trim(), include_str!("../resources/examples/example1-data.md").trim());
/// ```
///
/// Front-matter output:
///
/// ```toml
/// [meta]
/// field_one = 10
/// field_two = [2, 4]
/// ```
///
/// Data output:
///
/// ```md
/// ## 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:
///
/// ```sql
/// -- meta:
/// -- field_one: 10
/// -- field_two:
/// -- - 2
/// -- - 4
/// SELECT version();
/// ```
///
/// Code:
///
/// ```rust
/// # use extract_frontmatter::config::{Modifier, Splitter};
/// # use extract_frontmatter::Extractor;
/// # let input = include_str!("../resources/examples/example2.sql");
/// let (front_matter, data) = Extractor::new(Splitter::LinePrefix(String::from("-- ")))
/// .with_modifier(Modifier::StripPrefix(String::from("-- ")))
/// .extract(input);
/// # assert_eq!(front_matter.trim(), include_str!("../resources/examples/example2-meta.yml").trim());
/// # assert_eq!(data.trim(), include_str!("../resources/examples/example2-data.sql").trim());
/// ```
///
/// Front-matter output:
///
/// ```yaml
/// meta:
/// field_one: 10
/// field_two:
/// - 2
/// - 4
/// ```
///
/// Data output:
///
/// ```sql
/// SELECT version();
/// ```
#[derive(Debug)]
pub struct Extractor {
splitter: Splitter,
modifiers: Vec<Modifier>,
}
impl Extractor {
/// Instantiate a new [`Extractor`] config.
#[must_use]
pub const fn new(splitter: Splitter) -> Self {
Self { splitter, modifiers: Vec::new() }
}
/// Add a modifier to the front-matter returned by [`.extract()`](Self::extract).
pub fn with_modifier(&mut self, modifier: Modifier) -> &mut Self {
self.modifiers.push(modifier);
self
}
/// Split the given input string into a tuple of front-matter and data, applying any modifiers specified with
/// [`.with_modifier()`].
///
/// [`.with_modifier()`]:Self::with_modifier
#[must_use]
pub fn extract<'input>(&self, input: &'input str) -> (Cow<'input, str>, &'input str) {
let (meta, data) = match self.splitter {
Splitter::LineIndex(index) => extract_by_line_index(input, index),
Splitter::DelimiterLine(ref delim) => extract_by_delimiter_line(input, delim),
Splitter::LinePrefix(ref prefix) => extract_by_line_prefix(input, prefix),
};
let mut meta = Cow::from(meta);
for modifier in &self.modifiers {
meta = Cow::from(match modifier {
Modifier::StripPrefix(prefix) => strip_prefix(&meta, prefix),
Modifier::TrimWhitespace => trim_whitespace(&meta),
Modifier::StripFirstLine => strip_first_line(&meta),
Modifier::StripLastLine => strip_last_line(&meta),
});
}
(meta, data)
}
}