local_fmt_macros_internal/def_local_fmt/internal/file/
yaml.rs1use super::MessageLoader;
2
3pub struct YamlMessageLoader;
4
5impl MessageLoader for YamlMessageLoader {
6 const EXTENSION: &'static str = "yaml";
7
8 type Value = yaml_rust::Yaml;
9 type NestValue = yaml_rust::yaml::Hash;
10 const NEST_VALUE_NAME: &'static str = "hash";
11
12 fn value_to_nest(value: Self::Value) -> Option<Self::NestValue> {
13 value.into_hash()
14 }
15
16 fn value_as_str(value: &Self::Value) -> Option<&str> {
17 value.as_str()
18 }
19
20 fn value_from_str(content: &str) -> Result<Self::Value, String> {
21 yaml_rust::YamlLoader::load_from_str(content)
22 .map_err(|v| v.to_string())
23 .and_then(|mut docs| {
24 docs.pop()
25 .ok_or_else(|| "yaml document is empty".to_string())
26 })
27 }
28
29 fn iter_nested(value: Self::NestValue) -> impl Iterator<Item = (String, Self::Value)> {
30 value.into_iter().map(|(k, v)| {
31 let k = match k {
32 yaml_rust::Yaml::String(s) => s,
33 _ => panic!("Expected a string key, but got {:?}", k),
34 };
35 (k, v)
36 })
37 }
38}