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
impl Value
Sourcepub fn as_str(&self) -> Option<&str>
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 aString
.None
if the value is not aString
.
§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);
Sourcepub const fn as_f64(&self) -> Option<f64>
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 aNumber
.None
if the value is not aNumber
.
§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);
Sourcepub const fn as_bool(&self) -> Option<bool>
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 aBoolean
.None
if the value is not aBoolean
.
§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);
Sourcepub const fn as_array(&self) -> Option<&Vec<Value>>
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 anArray
.None
if the value is not anArray
.
§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());
Sourcepub fn as_object(&self) -> Option<&Frontmatter>
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 anObject
.None
if the value is not anObject
.
§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());
Sourcepub fn as_tagged(&self) -> Option<(&str, &Value)>
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 isTagged
.None
if the value is notTagged
.
§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);
Sourcepub const fn is_number(&self) -> bool
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());
Sourcepub const fn is_boolean(&self) -> bool
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());
Sourcepub const fn is_array(&self) -> bool
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());
Sourcepub const fn is_object(&self) -> bool
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());
Sourcepub const fn is_tagged(&self) -> bool
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());
Sourcepub fn array_len(&self) -> Option<usize>
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 anArray
.None
if the value is not anArray
.
§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);
Sourcepub fn to_object(self) -> Result<Frontmatter, String>
pub fn to_object(self) -> Result<Frontmatter, String>
Attempts to convert the value into a Frontmatter
.
§Returns
Ok(Frontmatter)
if the value is anObject
.Err(String)
with an error message if the value is not anObject
.
§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());
Sourcepub fn to_string_representation(&self) -> String
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\"");
Sourcepub fn into_string(self) -> Result<String, String>
pub fn into_string(self) -> Result<String, String>
Attempts to convert the value into a String
.
§Returns
Ok(String)
if the value is aString
.Err(String)
with an error message if the value is not aString
.
§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());
Sourcepub fn into_f64(self) -> Result<f64, String>
pub fn into_f64(self) -> Result<f64, String>
Attempts to convert the value into an f64
.
§Returns
Ok(f64)
if the value is aNumber
.Err(String)
with an error message if the value is not aNumber
.
§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());
Sourcepub fn into_bool(self) -> Result<bool, String>
pub fn into_bool(self) -> Result<bool, String>
Attempts to convert the value into a bool
.
§Returns
Ok(bool)
if the value is aBoolean
.Err(String)
with an error message if the value is not aBoolean
.
§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());
Sourcepub fn get_mut_array(&mut self) -> Option<&mut Vec<Value>>
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 anArray
.None
if the value is not anArray
.
§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<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
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 Value
Implement Display
for Value
to allow easy printing with escaped characters.
impl Display for Value
Implement Display
for Value
to allow easy printing with escaped characters.