arcs_ctf_yaml_parser/lists/
mod.rs1pub mod structs;
2
3use serde_yaml::Value as YamlValue;
4
5use crate::structs::{ValueType, get_type};
6
7pub trait StrList
8where Self: Sized {
9 type Error;
10 fn from_iter<'a>(iter: impl Iterator<Item = &'a str>) -> Result<Self, Self::Error>;
11
12 fn from_value_mismatch(iter: impl Iterator<Item = ValueType>) -> Self::Error;
13
14 fn not_sequence(type_enum: ValueType) -> Self::Error;
15}
16
17pub fn as_str_list<T: StrList>(value: &YamlValue) -> Result<T, T::Error> {
18
19 if !value.is_sequence() {
20 return Err(T::not_sequence(get_type(value)));
21 }
22
23
24 if let Some(sequence) = value.as_sequence() {
25 let mut strings = vec![];
26 let mut bad_type = vec![];
27
28 sequence.iter().for_each(
29 |val| if let Some(name) = val.as_str() {
30 strings.push(name);
31 } else {
32 bad_type.push(get_type(val));
33 }
34 );
35
36 if bad_type.is_empty() {
37 T::from_iter(strings.into_iter())
38 } else {
39 Err(T::from_value_mismatch(bad_type.into_iter()))
40 }
41 } else { unreachable!() }
42}