Function dotproperties::parse_from_slice [] [src]

pub fn parse_from_slice(input: &[u8]) -> Result<Vec<(String, String)>, Error>

Parse the bytes and return a vec of key/value pairs. The order of the elements in the parsed content is preserved.

Keys are not guaranteed to be unique.

Examples

let parsed = dotproperties::parse_from_slice(b"knowledge = power\n1+1 = 2").unwrap();
assert_eq!(parsed, vec![("knowledge".to_string(), "power".to_string()),
                        ("1+1".to_string(), "2".to_string())]);

It is often more convenient to work with a map, instead of a vec of pairs. The conversion can be done using the tools from std.

use std::collections::HashMap;

let parsed = dotproperties::parse_from_slice(b"a:b\nc:d").unwrap();
let mapped : HashMap<_,_> = parsed.into_iter().collect();

Note that if you use collect to create a map from a vec containing duplicate keys, only the value of the last one is retained.

use std::collections::HashMap;

let parsed = dotproperties::parse_from_slice(b"a:x\na:y").unwrap();
let mapped : HashMap<_,_> = parsed.into_iter().collect();
assert_eq!(mapped.len(), 1);
// The `map` converts the `Option<&String>` to an `Option<&str>`
assert_eq!(mapped.get("a").map(|v| &**v), Some("y"));

Failures

There is no fine-grained error reporting, yet. If the function encounters a parse error, then an empty struct Error is returned.