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
impl Frontmatter
Sourcepub fn insert(&mut self, key: String, value: Value) -> Option<Value>
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())));Sourcepub fn get(&self, key: &str) -> Option<&Value>
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);Sourcepub fn get_mut(&mut self, key: &str) -> Option<&mut Value>
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)));Sourcepub fn remove(&mut self, key: &str) -> Option<Value>
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);Sourcepub fn contains_key(&self, key: &str) -> bool
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"));Sourcepub fn len(&self) -> usize
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);Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn iter(&self) -> Iter<'_, String, Value>
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);
}Sourcepub fn iter_mut(&mut self) -> IterMut<'_, String, Value>
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)));Sourcepub fn merge(&mut self, other: Frontmatter)
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)));Sourcepub fn is_null(&self, key: &str) -> bool
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
impl Clone for Frontmatter
Source§fn clone(&self) -> Frontmatter
fn clone(&self) -> Frontmatter
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Frontmatter
impl Debug for Frontmatter
Source§impl Default for Frontmatter
impl Default for Frontmatter
Source§impl<'de> Deserialize<'de> for Frontmatter
impl<'de> Deserialize<'de> for Frontmatter
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for Frontmatter
impl Display for Frontmatter
Implement Display for Frontmatter to allow easy printing with escaped characters.
Source§impl FromIterator<(String, Value)> for Frontmatter
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
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 aStringand the value is aValue.
§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
impl IntoIterator for Frontmatter
Implement IntoIterator for Frontmatter to allow idiomatic iteration.