frontmatter_gen::types

Struct Frontmatter

source
pub struct Frontmatter(pub HashMap<String, Value>);
Expand description

Represents the frontmatter, a collection of key-value pairs.

Tuple Fields§

§0: HashMap<String, Value>

Implementations§

source§

impl Frontmatter

source

pub fn new() -> Self

Creates a new, empty frontmatter.

§Returns

A new Frontmatter instance with no key-value pairs.

§Examples
use frontmatter_gen::Frontmatter;

let fm = Frontmatter::new();
assert!(fm.is_empty());
source

pub fn insert(&mut self, key: String, value: Value) -> Option<Value>

Inserts a key-value pair into the frontmatter.

§Arguments
  • key - The key for the entry.
  • value - The value associated with the key.
§Returns

An option containing the old value if it was replaced.

§Examples
use frontmatter_gen::{Frontmatter, Value};

let mut fm = Frontmatter::new();
assert_eq!(fm.insert("key".to_string(), Value::String("value".to_string())), None);
assert_eq!(fm.insert("key".to_string(), Value::Number(42.0)), Some(Value::String("value".to_string())));
source

pub fn get(&self, key: &str) -> Option<&Value>

Retrieves a reference to a value associated with a key.

§Arguments
  • key - The key to look up.
§Returns

An option containing a reference to the value if the key exists.

§Examples
use frontmatter_gen::{Frontmatter, Value};

let mut fm = Frontmatter::new();
fm.insert("key".to_string(), Value::String("value".to_string()));
assert_eq!(fm.get("key"), Some(&Value::String("value".to_string())));
assert_eq!(fm.get("nonexistent"), None);
source

pub fn get_mut(&mut self, key: &str) -> Option<&mut Value>

Retrieves a mutable reference to a value associated with a key.

§Arguments
  • key - The key to look up.
§Returns

An option containing a mutable reference to the value if the key exists.

§Examples
use frontmatter_gen::{Frontmatter, Value};

let mut fm = Frontmatter::new();
fm.insert("key".to_string(), Value::String("value".to_string()));
if let Some(value) = fm.get_mut("key") {
    *value = Value::Number(42.0);
}
assert_eq!(fm.get("key"), Some(&Value::Number(42.0)));
source

pub fn remove(&mut self, key: &str) -> Option<Value>

Removes a key-value pair from the frontmatter.

§Arguments
  • key - The key to remove.
§Returns

An option containing the removed value if the key existed.

§Examples
use frontmatter_gen::{Frontmatter, Value};

let mut fm = Frontmatter::new();
fm.insert("key".to_string(), Value::String("value".to_string()));
assert_eq!(fm.remove("key"), Some(Value::String("value".to_string())));
assert_eq!(fm.remove("key"), None);
source

pub fn contains_key(&self, key: &str) -> bool

Checks if the frontmatter contains a given key.

§Arguments
  • key - The key to check for.
§Returns

true if the key exists in the frontmatter, false otherwise.

§Examples
use frontmatter_gen::{Frontmatter, Value};

let mut fm = Frontmatter::new();
fm.insert("key".to_string(), Value::String("value".to_string()));
assert!(fm.contains_key("key"));
assert!(!fm.contains_key("nonexistent"));
source

pub fn len(&self) -> usize

Returns the number of entries in the frontmatter.

§Returns

The number of key-value pairs in the frontmatter.

§Examples
use frontmatter_gen::{Frontmatter, Value};

let mut fm = Frontmatter::new();
assert_eq!(fm.len(), 0);
fm.insert("key".to_string(), Value::String("value".to_string()));
assert_eq!(fm.len(), 1);
source

pub fn is_empty(&self) -> bool

Checks if the frontmatter is empty.

§Returns

true if the frontmatter contains no key-value pairs, false otherwise.

§Examples
use frontmatter_gen::{Frontmatter, Value};

let mut fm = Frontmatter::new();
assert!(fm.is_empty());
fm.insert("key".to_string(), Value::String("value".to_string()));
assert!(!fm.is_empty());
source

pub fn iter(&self) -> Iter<'_, String, Value>

Returns an iterator over the key-value pairs of the frontmatter.

§Returns

An iterator over references to the key-value pairs.

§Examples
use frontmatter_gen::{Frontmatter, Value};

let mut fm = Frontmatter::new();
fm.insert("key1".to_string(), Value::String("value1".to_string()));
fm.insert("key2".to_string(), Value::Number(42.0));

for (key, value) in fm.iter() {
    println!("{}: {:?}", key, value);
}
source

pub fn iter_mut(&mut self) -> IterMut<'_, String, Value>

Returns a mutable iterator over the key-value pairs of the frontmatter.

§Returns

A mutable iterator over references to the key-value pairs.

§Examples
use frontmatter_gen::{Frontmatter, Value};

let mut fm = Frontmatter::new();
fm.insert("key1".to_string(), Value::String("value1".to_string()));
fm.insert("key2".to_string(), Value::Number(42.0));

for (_, value) in fm.iter_mut() {
    if let Value::Number(n) = value {
        *n += 1.0;
    }
}

assert_eq!(fm.get("key2"), Some(&Value::Number(43.0)));
source

pub fn merge(&mut self, other: Frontmatter)

Merges another frontmatter into this one. If a key exists, it will be overwritten.

§Arguments
  • other - The frontmatter to merge into this one.
§Examples
use frontmatter_gen::{Frontmatter, Value};

let mut fm1 = Frontmatter::new();
fm1.insert("key1".to_string(), Value::String("value1".to_string()));

let mut fm2 = Frontmatter::new();
fm2.insert("key2".to_string(), Value::Number(42.0));

fm1.merge(fm2);
assert_eq!(fm1.len(), 2);
assert_eq!(fm1.get("key2"), Some(&Value::Number(42.0)));
source

pub fn is_null(&self, key: &str) -> bool

Checks if a given key exists and its value is null.

§Arguments
  • key - The key to check.
§Returns

true if the key exists and its value is null, false otherwise.

§Examples
use frontmatter_gen::{Frontmatter, Value};

let mut fm = Frontmatter::new();
fm.insert("null_key".to_string(), Value::Null);
fm.insert("non_null_key".to_string(), Value::String("value".to_string()));

assert!(fm.is_null("null_key"));
assert!(!fm.is_null("non_null_key"));
assert!(!fm.is_null("nonexistent_key"));

Trait Implementations§

source§

impl Clone for Frontmatter

source§

fn clone(&self) -> Frontmatter

Returns a copy 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 Frontmatter

source§

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

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

impl Default for Frontmatter

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for Frontmatter

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Frontmatter

Implement Display for Frontmatter to allow easy printing with escaped characters.

source§

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

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

impl FromIterator<(String, Value)> for Frontmatter

Implement FromIterator for Frontmatter to create a frontmatter from an iterator.

source§

fn from_iter<I: IntoIterator<Item = (String, Value)>>(iter: I) -> Self

Creates a Frontmatter from an iterator of key-value pairs.

§Arguments
  • iter - An iterator of key-value pairs where the key is a String and the value is a Value.
§Returns

A Frontmatter containing the key-value pairs from the iterator.

§Examples
use frontmatter_gen::{Frontmatter, Value};
use std::iter::FromIterator;

let pairs = vec![
    ("key1".to_string(), Value::String("value1".to_string())),
    ("key2".to_string(), Value::Number(42.0)),
];

let fm = Frontmatter::from_iter(pairs);
assert_eq!(fm.len(), 2);
assert_eq!(fm.get("key1"), Some(&Value::String("value1".to_string())));
assert_eq!(fm.get("key2"), Some(&Value::Number(42.0)));
source§

impl IntoIterator for Frontmatter

Implement IntoIterator for Frontmatter to allow idiomatic iteration.

source§

type Item = (String, Value)

The type of the elements being iterated over.
source§

type IntoIter = IntoIter<String, Value>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl PartialEq for Frontmatter

source§

fn eq(&self, other: &Frontmatter) -> 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 Serialize for Frontmatter

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl StructuralPartialEq for Frontmatter

Auto Trait Implementations§

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,