1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
use crate::parse::kvp::{KVPFile, KVPSection}; use crate::parse::util::{parse_loose_number, parse_loose_numeric_bool}; use crate::parse::Span; use cgmath::{Vector1, Vector2, Vector3, Vector4}; #[derive(Debug, Clone, PartialEq)] pub struct KVPGenericWarning { pub span: Span, pub kind: KVPGenericWarningKind, } #[derive(Debug, Clone, PartialEq)] pub enum KVPGenericWarningKind { UnknownSection { name: String }, UnknownField { name: String }, TooManyFields { idx: u64, max: u64 }, InvalidValue { value: String }, } pub trait FromKVPFile: Default { type Warnings; #[must_use] fn from_kvp_file(k: &KVPFile<'_>) -> (Self, Vec<Self::Warnings>); } pub trait FromKVPSection: Default { type Warnings; #[must_use] fn from_kvp_section(section: &KVPSection<'_>) -> (Self, Vec<Self::Warnings>); } pub trait FromKVPValue { #[must_use] fn from_kvp_value(value: &str) -> Option<Self> where Self: Sized; } macro_rules! impl_from_kvp_value_primitive { ($($prim:ident),+) => {$( impl FromKVPValue for $prim { fn from_kvp_value(value: &str) -> Option<$prim> { parse_loose_number::<$prim>(value).map(|v| v.0) } } )*}; } impl_from_kvp_value_primitive!(u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, usize, isize, f32, f64); impl FromKVPValue for bool { fn from_kvp_value(value: &str) -> Option<Self> { parse_loose_numeric_bool(value).map(|v| v.0) } } impl<T> FromKVPValue for Option<T> where T: FromKVPValue, { fn from_kvp_value(value: &str) -> Option<Self> { Some(T::from_kvp_value(value)) } } impl FromKVPValue for String { fn from_kvp_value(value: &str) -> Option<Self> { Some(Self::from(value)) } } impl<T> FromKVPValue for Vec<T> where T: FromKVPValue + Default, { fn from_kvp_value(value: &str) -> Option<Self> { let split = value.split(',').map(str::trim); Some(split.map(T::from_kvp_value).map(Option::unwrap_or_default).collect()) } } impl<T> FromKVPValue for Vector1<T> where T: FromKVPValue + Default, { fn from_kvp_value(value: &str) -> Option<Self> { Some(Self::new(T::from_kvp_value(value).unwrap_or_default())) } } impl<T> FromKVPValue for Vector2<T> where T: FromKVPValue + Default, { fn from_kvp_value(value: &str) -> Option<Self> { let split: Vec<&str> = value.split(',').map(str::trim).collect(); Some(Self::new( split.get(0).and_then(|v| T::from_kvp_value(v)).unwrap_or_default(), split.get(1).and_then(|v| T::from_kvp_value(v)).unwrap_or_default(), )) } } impl<T> FromKVPValue for Vector3<T> where T: FromKVPValue + Default, { fn from_kvp_value(value: &str) -> Option<Self> { let split: Vec<&str> = value.split(',').map(str::trim).collect(); Some(Self::new( split.get(0).and_then(|v| T::from_kvp_value(v)).unwrap_or_default(), split.get(1).and_then(|v| T::from_kvp_value(v)).unwrap_or_default(), split.get(2).and_then(|v| T::from_kvp_value(v)).unwrap_or_default(), )) } } impl<T> FromKVPValue for Vector4<T> where T: FromKVPValue + Default, { fn from_kvp_value(value: &str) -> Option<Self> { let split: Vec<&str> = value.split(',').map(str::trim).collect(); Some(Self::new( split.get(0).and_then(|v| T::from_kvp_value(v)).unwrap_or_default(), split.get(1).and_then(|v| T::from_kvp_value(v)).unwrap_or_default(), split.get(2).and_then(|v| T::from_kvp_value(v)).unwrap_or_default(), split.get(3).and_then(|v| T::from_kvp_value(v)).unwrap_or_default(), )) } }