frontmatter_gen::types

Enum Value

Source
pub enum Value {
    Null,
    String(String),
    Number(f64),
    Boolean(bool),
    Array(Vec<Value>),
    Object(Box<Frontmatter>),
    Tagged(String, Box<Value>),
}
Expand description

A flexible value type that can hold various types of data found in frontmatter.

Variants§

§

Null

Represents a null value.

§

String(String)

Represents a string value.

§

Number(f64)

Represents a numeric value.

§

Boolean(bool)

Represents a boolean value.

§

Array(Vec<Value>)

Represents an array of values.

§

Object(Box<Frontmatter>)

Represents an object (frontmatter).

§

Tagged(String, Box<Value>)

Represents a tagged value, containing a tag and a value.

Implementations§

Source§

impl Value

Source

pub fn as_str(&self) -> Option<&str>

Returns the value as a string slice, if it is of type String.

§Returns
  • Some(&str) if the value is a String.
  • None if the value is not a String.
§Examples
use frontmatter_gen::Value;

let string_value = Value::String("Hello".to_string());
assert_eq!(string_value.as_str(), Some("Hello"));

let number_value = Value::Number(42.0);
assert_eq!(number_value.as_str(), None);
Source

pub const fn as_f64(&self) -> Option<f64>

Returns the value as a float, if it is of type Number.

§Returns
  • Some(f64) if the value is a Number.
  • None if the value is not a Number.
§Examples
use frontmatter_gen::Value;

let number_value = Value::Number(3.14);
assert_eq!(number_value.as_f64(), Some(3.14));

let string_value = Value::String("Not a number".to_string());
assert_eq!(string_value.as_f64(), None);
Source

pub const fn as_bool(&self) -> Option<bool>

Returns the value as a boolean, if it is of type Boolean.

§Returns
  • Some(bool) if the value is a Boolean.
  • None if the value is not a Boolean.
§Examples
use frontmatter_gen::Value;

let bool_value = Value::Boolean(true);
assert_eq!(bool_value.as_bool(), Some(true));

let string_value = Value::String("Not a boolean".to_string());
assert_eq!(string_value.as_bool(), None);
Source

pub const fn as_array(&self) -> Option<&Vec<Value>>

Returns the value as an array, if it is of type Array.

§Returns
  • Some(&Vec<Value>) if the value is an Array.
  • None if the value is not an Array.
§Examples
use frontmatter_gen::Value;

let array_value = Value::Array(vec![Value::Number(1.0), Value::Number(2.0)]);
assert!(array_value.as_array().is_some());
assert_eq!(array_value.as_array().unwrap().len(), 2);

let string_value = Value::String("Not an array".to_string());
assert!(string_value.as_array().is_none());
Source

pub fn as_object(&self) -> Option<&Frontmatter>

Returns the value as an object (frontmatter), if it is of type Object.

§Returns
  • Some(&Frontmatter) if the value is an Object.
  • None if the value is not an Object.
§Examples
use frontmatter_gen::{Value, Frontmatter};

let mut fm = Frontmatter::new();
fm.insert("key".to_string(), Value::String("value".to_string()));
let object_value = Value::Object(Box::new(fm));
assert!(object_value.as_object().is_some());

let string_value = Value::String("Not an object".to_string());
assert!(string_value.as_object().is_none());
Source

pub fn as_tagged(&self) -> Option<(&str, &Value)>

Returns the value as a tagged value, if it is of type Tagged.

§Returns
  • Some((&str, &Value)) if the value is Tagged.
  • None if the value is not Tagged.
§Examples
use frontmatter_gen::Value;

let tagged_value = Value::Tagged("tag".to_string(), Box::new(Value::Number(42.0)));
assert_eq!(tagged_value.as_tagged(), Some(("tag", &Value::Number(42.0))));

let string_value = Value::String("Not tagged".to_string());
assert_eq!(string_value.as_tagged(), None);
Source

pub const fn is_null(&self) -> bool

Checks if the value is of type Null.

§Returns

true if the value is Null, otherwise false.

§Examples
use frontmatter_gen::Value;

let null_value = Value::Null;
assert!(null_value.is_null());

let string_value = Value::String("Not null".to_string());
assert!(!string_value.is_null());
Source

pub const fn is_string(&self) -> bool

Checks if the value is of type String.

§Returns

true if the value is a String, otherwise false.

§Examples
use frontmatter_gen::Value;

let string_value = Value::String("Hello".to_string());
assert!(string_value.is_string());

let number_value = Value::Number(42.0);
assert!(!number_value.is_string());
Source

pub const fn is_number(&self) -> bool

Checks if the value is of type Number.

§Returns

true if the value is a Number, otherwise false.

§Examples
use frontmatter_gen::Value;

let number_value = Value::Number(3.14);
assert!(number_value.is_number());

let string_value = Value::String("Not a number".to_string());
assert!(!string_value.is_number());
Source

pub const fn is_boolean(&self) -> bool

Checks if the value is of type Boolean.

§Returns

true if the value is a Boolean, otherwise false.

§Examples
use frontmatter_gen::Value;

let bool_value = Value::Boolean(true);
assert!(bool_value.is_boolean());

let string_value = Value::String("Not a boolean".to_string());
assert!(!string_value.is_boolean());
Source

pub const fn is_array(&self) -> bool

Checks if the value is of type Array.

§Returns

true if the value is an Array, otherwise false.

§Examples
use frontmatter_gen::Value;

let array_value = Value::Array(vec![Value::Number(1.0), Value::Number(2.0)]);
assert!(array_value.is_array());

let string_value = Value::String("Not an array".to_string());
assert!(!string_value.is_array());
Source

pub const fn is_object(&self) -> bool

Checks if the value is of type Object.

§Returns

true if the value is an Object, otherwise false.

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

let object_value = Value::Object(Box::new(Frontmatter::new()));
assert!(object_value.is_object());

let string_value = Value::String("Not an object".to_string());
assert!(!string_value.is_object());
Source

pub const fn is_tagged(&self) -> bool

Checks if the value is of type Tagged.

§Returns

true if the value is Tagged, otherwise false.

§Examples
use frontmatter_gen::Value;

let tagged_value = Value::Tagged("tag".to_string(), Box::new(Value::Number(42.0)));
assert!(tagged_value.is_tagged());

let string_value = Value::String("Not tagged".to_string());
assert!(!string_value.is_tagged());
Source

pub fn array_len(&self) -> Option<usize>

Returns the length of the array if the value is an array, otherwise returns None.

§Returns
  • Some(usize) with the length of the array if the value is an Array.
  • None if the value is not an Array.
§Examples
use frontmatter_gen::Value;

let array_value = Value::Array(vec![Value::Number(1.0), Value::Number(2.0)]);
assert_eq!(array_value.array_len(), Some(2));

let string_value = Value::String("Not an array".to_string());
assert_eq!(string_value.array_len(), None);
Source

pub fn to_object(self) -> Result<Frontmatter, String>

Attempts to convert the value into a Frontmatter.

§Returns
  • Ok(Frontmatter) if the value is an Object.
  • Err(String) with an error message if the value is not an Object.
§Examples
use frontmatter_gen::{Value, Frontmatter};

let object_value = Value::Object(Box::new(Frontmatter::new()));
assert!(object_value.to_object().is_ok());

let string_value = Value::String("Not an object".to_string());
assert!(string_value.to_object().is_err());
Source

pub fn to_string_representation(&self) -> String

Converts the value to a string representation regardless of its type.

§Returns

A String representation of the value.

§Examples
use frontmatter_gen::Value;

let number_value = Value::Number(3.14);
assert_eq!(number_value.to_string_representation(), "3.14");

let string_value = Value::String("Hello".to_string());
assert_eq!(string_value.to_string_representation(), "\"Hello\"");
Source

pub fn into_string(self) -> Result<String, String>

Attempts to convert the value into a String.

§Returns
  • Ok(String) if the value is a String.
  • Err(String) with an error message if the value is not a String.
§Examples
use frontmatter_gen::Value;

let string_value = Value::String("Hello".to_string());
assert_eq!(string_value.into_string(), Ok("Hello".to_string()));

let number_value = Value::Number(42.0);
assert!(number_value.into_string().is_err());
Source

pub fn into_f64(self) -> Result<f64, String>

Attempts to convert the value into an f64.

§Returns
  • Ok(f64) if the value is a Number.
  • Err(String) with an error message if the value is not a Number.
§Examples
use frontmatter_gen::Value;

let number_value = Value::Number(3.14);
assert_eq!(number_value.into_f64(), Ok(3.14));

let string_value = Value::String("Not a number".to_string());
assert!(string_value.into_f64().is_err());
Source

pub fn into_bool(self) -> Result<bool, String>

Attempts to convert the value into a bool.

§Returns
  • Ok(bool) if the value is a Boolean.
  • Err(String) with an error message if the value is not a Boolean.
§Examples
use frontmatter_gen::Value;

let bool_value = Value::Boolean(true);
assert_eq!(bool_value.into_bool(), Ok(true));

let string_value = Value::String("Not a boolean".to_string());
assert!(string_value.into_bool().is_err());
Source

pub fn get_mut_array(&mut self) -> Option<&mut Vec<Value>>

Attempts to get a mutable reference to the array if the value is an array.

§Returns
  • Some(&mut Vec<Value>) if the value is an Array.
  • None if the value is not an Array.
§Examples
use frontmatter_gen::Value;

let mut array_value = Value::Array(vec![Value::Number(1.0), Value::Number(2.0)]);
if let Some(arr) = array_value.get_mut_array() {
    arr.push(Value::Number(3.0));
}
assert_eq!(array_value.array_len(), Some(3));

let mut string_value = Value::String("Not an array".to_string());
assert!(string_value.get_mut_array().is_none());

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

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 Value

Source§

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

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

impl Default for Value

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for Value

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 Value

Implement Display for Value 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 From<&str> for Value

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Value

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(b: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(n: f64) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<Value> for Value

Source§

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

Creates a value from an iterator. Read more
Source§

impl FromStr for Value

Source§

type Err = &'static str

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for Value

Source§

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

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 Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnwindSafe for Value

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 u8)

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

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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§

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

Source§

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

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T