Enum Filter

Source
pub enum Filter {
    Ceil,
    Floor,
    Round {
        precision: u8,
    },
    Markdown,
    Replace {
        find: String,
        replacement: String,
        limit: Option<u8>,
    },
    Reverse,
    Text {
        case: TextCase,
    },
    Truncate {
        characters: u8,
        trail: String,
    },
}
Expand description

Predefined functions names that will be used within render_filter to convert a value.

Variants§

§

Ceil

Rounds a numeric value up to the nearest whole number.

§Example

use blogs_md_easy::{render_filter, Filter};

let input = "1.234".to_string();
let filter = Filter::Ceil;
let output = render_filter(input, &filter);

assert_eq!(output, "2");
§

Floor

Rounds a numeric value down to the nearest whole number.

§Example

use blogs_md_easy::{render_filter, Filter};

let input = "4.567".to_string();
let filter = Filter::Floor;
let output = render_filter(input, &filter);

assert_eq!(output, "4");
§

Round

Round a number to a given precision.

Default argument: precision

§Examples

Precision of 0 to remove decimal place.

use blogs_md_easy::{render_filter, Filter};

let input = "1.234".to_string();
let filter = Filter::Round { precision: 0 };
let output = render_filter(input, &filter);

assert_eq!(output, "1");

Precision of 3 for three decimal places.

use blogs_md_easy::{render_filter, Filter};

let input = "1.23456789".to_string();
let filter = Filter::Round { precision: 3 };
let output = render_filter(input, &filter);

assert_eq!(output, "1.235");

Fields

§precision: u8

The number of decimal places to round to. A half is rounded down.

Default: 0

§Examples

Providing no arguments.

use blogs_md_easy::{parse_filter, Filter, Span};

let input = Span::new("round");
let (_, filter) = parse_filter(input).unwrap();

assert!(matches!(filter, Filter::Round { .. }));
assert_eq!(filter, Filter::Round { precision: 0 });

Providing the default argument.

use blogs_md_easy::{parse_filter, Filter, Span};

let input = Span::new("round = 3");
let (_, filter) = parse_filter(input).unwrap();

assert!(matches!(filter, Filter::Round { .. }));
assert_eq!(filter, Filter::Round { precision: 3 });

Alternatively, it is possible to be more explicit.

use blogs_md_easy::{parse_filter, Filter, Span};

let input = Span::new("round = precision: 42");
let (_, filter) = parse_filter(input).unwrap();

assert!(matches!(filter, Filter::Round { .. }));
assert_eq!(filter, Filter::Round { precision: 42 });
§

Markdown

Converts a string from Markdown into HTML.

§Example

use blogs_md_easy::{render_filter, Filter};

let input = r#"# Markdown Title
First paragraph.

[example.com](https://example.com)

* Unordered list

1. Ordered list"#.to_string();
let filter = Filter::Markdown;
let output = render_filter(input, &filter);

assert_eq!(output, r#"<h1>Markdown Title</h1>
<p>First paragraph.</p>
<p><a href="https://example.com">example.com</a></p>
<ul>
<li>Unordered list</li>
</ul>
<ol>
<li>Ordered list</li>
</ol>"#);
§

Replace

Replace a given substring with another. Optionally, limit the number of replacements from the start of the string.

Default argument: find

§Example

use blogs_md_easy::{render_filter, Filter};

let input = "Hello, World!".to_string();
let filter = Filter::Replace {
    find: "World".to_string(),
    replacement: "Rust".to_string(),
    limit: None,
};
let output = render_filter(input, &filter);

assert_eq!(output, "Hello, Rust!");

Fields

§find: String

The substring that we are looking for.

§replacement: String

The substring that will replace what we find.

§limit: Option<u8>

Limit the number of replacements from the start of the string.

Default: None

§Examples

Without an argument, this will default to doing nothing.

use blogs_md_easy::{parse_placeholder, render_filter, Filter, Span};

let input = Span::new("{{ £greeting | replace }}");
let (_, placeholder) = parse_placeholder(input).unwrap();

assert!(matches!(placeholder.filters[0], Filter::Replace { .. }));
assert_eq!(placeholder.filters[0], Filter::Replace {
    find: "".to_string(),
    replacement: "".to_string(),
    limit: None,
});

let greeting = "Hello, World!".to_string();
// Cloning here, only so we can reuse the `greeting` variable in
// assert, to prove that they are identical.
let output = render_filter(greeting.clone(), &placeholder.filters[0]);
assert_eq!(output, greeting);

Providing the default argument. In this case the value will be assigned to find, and the replacement will be an empty String, essentially removing this phrase from the string.

use blogs_md_easy::{parse_placeholder, render_filter, Filter, Span};

let input = Span::new("{{ £greeting | replace = World }}");
let (_, placeholder) = parse_placeholder(input).unwrap();

assert!(matches!(placeholder.filters[0], Filter::Replace { .. }));
assert_eq!(placeholder.filters[0], Filter::Replace {
    find: "World".to_string(),
    replacement: "".to_string(),
    limit: None,
});

let greeting = "Hello, World!".to_string();
let output = render_filter(greeting, &placeholder.filters[0]);
assert_eq!(output, "Hello, !".to_string());

Specify the number of replacements.

use blogs_md_easy::{parse_placeholder, render_filter, Filter, Span};

let input = Span::new("{{ £greeting | replace = !, limit: 2 }}");
let (_, placeholder) = parse_placeholder(input).unwrap();

assert!(matches!(placeholder.filters[0], Filter::Replace { .. }));
assert_eq!(placeholder.filters[0], Filter::Replace {
    find: "!".to_string(),
    replacement: "".to_string(),
    limit: Some(2),
});

let greeting = "Hello, World!!!".to_string();
let output = render_filter(greeting, &placeholder.filters[0]);
assert_eq!(output, "Hello, World!".to_string());

Setting all arguments explicitly.

use blogs_md_easy::{parse_placeholder, render_filter, Filter, Span};

let input = Span::new("{{ £greeting | replace = find: World, replacement: Rust, limit: 1 }}");
let (_, placeholder) = parse_placeholder(input).unwrap();

assert!(matches!(placeholder.filters[0], Filter::Replace { .. }));
assert_eq!(placeholder.filters[0], Filter::Replace {
    find: "World".to_string(),
    replacement: "Rust".to_string(),
    limit: Some(1),
});

let greeting = "Hello, World! Hello, World!".to_string();
let output = render_filter(greeting, &placeholder.filters[0]);
assert_eq!(output, "Hello, Rust! Hello, World!".to_string());
§

Reverse

Reverse a string, character by character.

§Example

use blogs_md_easy::{render_filter, Filter};

let input = "Hello, World!".to_string();
let filter = Filter::Reverse;
let output = render_filter(input, &filter);

assert_eq!(output, "!dlroW ,olleH");
§

Text

Converts text to another format.

Currently, the only argument is case.

Default argument: case

§Example

use blogs_md_easy::{render_filter, Filter, TextCase};

let input = "Hello, World!".to_string();
let filter = Filter::Text { case: TextCase::Upper };
let output = render_filter(input, &filter);

assert_eq!(output, "HELLO, WORLD!");

Fields

§case: TextCase

Specifies the TextCase that the font should use.

Default: lower

§Examples

Without an argument, this will default to lowercase.

use blogs_md_easy::{parse_filter, Filter, Span, TextCase};

let input = Span::new("text");
let (_, filter) = parse_filter(input).unwrap();

assert!(matches!(filter, Filter::Text { .. }));
assert_eq!(filter, Filter::Text { case: TextCase::Lower });

Passing in a case, without an argument is possible too.

use blogs_md_easy::{parse_filter, Filter, Span, TextCase};

let input = Span::new("text = upper");
let (_, filter) = parse_filter(input).unwrap();

assert!(matches!(filter, Filter::Text { .. }));
assert_eq!(filter, Filter::Text { case: TextCase::Upper });

Alternatively, it is possible to be more explicit.

use blogs_md_easy::{parse_filter, Filter, Span, TextCase};

let input = Span::new("text = case: snake");
let (_, filter) = parse_filter(input).unwrap();

assert!(matches!(filter, Filter::Text { .. }));
assert_eq!(filter, Filter::Text { case: TextCase::Snake });
§

Truncate

Truncates a string to a given length, and applies a trailing string, if the string was truncated.

Default argument: characters

§Example

use blogs_md_easy::{render_filter, Filter};

let input = "Hello, World!".to_string();
let filter = Filter::Truncate { characters: 5, trail: "...".to_string() };
let output = render_filter(input, &filter);

assert_eq!(output, "Hello...");

Fields

§characters: u8

The number of characters the String will be cut to.

If this number is greater than the String’s length, then nothing happens to the String.

Default: 100

§Example
use blogs_md_easy::{parse_filter, Filter, Span};

let input = Span::new("truncate = trail: --");
let (_, filter) = parse_filter(input).unwrap();

assert!(matches!(filter, Filter::Truncate { .. }));
assert_eq!(filter, Filter::Truncate {
    characters: 100,
    trail: "--".to_string(),
});
§trail: String

The trailing characters to be appended to a truncated String.

Due to this being appended, that means that your string will exceed the characters length.
To counter this, you will need to reduce your characters value.

Default: "..."

§Example
use blogs_md_easy::{parse_filter, Filter, Span};

let input = Span::new("truncate = characters: 42");
let (_, filter) = parse_filter(input).unwrap();

assert!(matches!(filter, Filter::Truncate { .. }));
assert_eq!(filter, Filter::Truncate {
    characters: 42,
    trail: "...".to_string(),
});

Trait Implementations§

Source§

impl Clone for Filter

Source§

fn clone(&self) -> Filter

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Filter

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Filter

Source§

fn eq(&self, other: &Filter) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Filter

Auto Trait Implementations§

§

impl Freeze for Filter

§

impl RefUnwindSafe for Filter

§

impl Send for Filter

§

impl Sync for Filter

§

impl Unpin for Filter

§

impl UnwindSafe for Filter

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.