pub struct Vdf<'text> {
pub key: Key<'text>,
pub value: Value<'text>,
}Expand description
A loosely typed representation of VDF text
Vdf is represented as a single Key mapped to a single Value
§Parse
Vdfs will generally be created through the use of parse() or Parser::parse() which
takes a string representing VDF text and attempts to parse it to a Vdf representation.
§Mutate
From there you can manipulate/extract from the representation as desired by using the standard
conventions on the internal types (plain old BTreeMaps, Vecs, and Cows all the way down)
§Render
The Vdf can also be rendered back to its text form through its Display implementation
§Example
// Parse
let vdf_text = r#"
"Outer Key"
{
"Inner Key" "Inner Value"
"Inner Key"
{
}
}
"#;
let mut parsed = keyvalues_parser::parse(vdf_text)?;
// Mutate: i.e. remove the last "Inner Key" pair
parsed
.value
.get_mut_obj()
.unwrap()
.get_mut("Inner Key")
.unwrap()
.pop();
// Render: prints
// "Outer Key"
// {
// "Inner Key" "Inner Value"
// }
println!("{}", parsed);Fields§
§key: Key<'text>§value: Value<'text>Implementations§
Source§impl<'a> Vdf<'a>
impl<'a> Vdf<'a>
Sourcepub fn parse(s: &'a str) -> Result<Self>
👎Deprecated since 0.2.4: Please use parse().map(Vdf::from) instead
pub fn parse(s: &'a str) -> Result<Self>
Please use parse().map(Vdf::from) instead
Attempts to parse VDF text to a Vdf
pub fn parse_raw(s: &'a str) -> Result<Self>
Please use Parser::new().literal_special_chars(true).parse().map(Vdf::from) instead
Source§impl<'text> Vdf<'text>
impl<'text> Vdf<'text>
Sourcepub fn new(key: Key<'text>, value: Value<'text>) -> Self
pub fn new(key: Key<'text>, value: Value<'text>) -> Self
Creates a Vdf using a provided key and value
use keyvalues_parser::{Vdf, Value};
use std::borrow::Cow;
let vdf = Vdf::new(Cow::from("Much Key"), Value::Str(Cow::from("Such Wow")));
// prints
// "Much Key" "Such Wow"
println!("{}", vdf);Sourcepub fn into_owned(self) -> Vdf<'static>
pub fn into_owned(self) -> Vdf<'static>
Converts this Vdf into a fully owned variant
Internally a Vdf can reference the underlying text. This changes all of those
references to be be allocated instead, allowing for easier ownership
fn expect_owned_vdf(text: &'_ str) -> keyvalues_parser::Vdf<'static> {
keyvalues_parser::parse(text).unwrap().into_vdf().into_owned()
}
let vdf = expect_owned_vdf(
r#"foo { bar {} bar "allocates for ownership" }"#
);
assert_eq!(
vdf.value.unwrap_obj().get("bar").unwrap()[1].get_str().unwrap(),
"allocates for ownership",
);