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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
use std::collections::BTreeSet; use std::ops::{Shl, Shr}; use {DataType, ErrorKind, HasDataType, Result}; #[cfg(feature = "bidir-map")] use bidir_map::BidirMap; pub trait IsParameterMask: Clone {} impl IsParameterMask for BTreeSet<String> {} impl IsParameterMask for u64 {} pub type DataValueRaw = DataValue<u64>; pub type DataValueDescriptive = DataValue<BTreeSet<String>>; #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "lowercase", untagged))] pub enum DataValue<M: IsParameterMask> { Boolean(bool), UInt8(u8), UInt16(u16), UInt32(u32), UInt64(u64), Int8(i8), Int16(i16), Int32(i32), Int64(i64), ParameterMask(M), } impl<T: IsParameterMask> From<bool> for DataValue<T> { fn from(v: bool) -> DataValue<T> { DataValue::Boolean(v) } } impl<T: IsParameterMask> From<i8> for DataValue<T> { fn from(v: i8) -> DataValue<T> { DataValue::Int8(v) } } impl<T: IsParameterMask> From<i16> for DataValue<T> { fn from(v: i16) -> DataValue<T> { DataValue::Int16(v) } } impl<T: IsParameterMask> From<i32> for DataValue<T> { fn from(v: i32) -> DataValue<T> { DataValue::Int32(v) } } impl<T: IsParameterMask> From<i64> for DataValue<T> { fn from(v: i64) -> DataValue<T> { DataValue::Int64(v) } } impl<T: IsParameterMask> From<u8> for DataValue<T> { fn from(v: u8) -> DataValue<T> { DataValue::UInt8(v) } } impl<T: IsParameterMask> From<u16> for DataValue<T> { fn from(v: u16) -> DataValue<T> { DataValue::UInt16(v) } } impl<T: IsParameterMask> From<u32> for DataValue<T> { fn from(v: u32) -> DataValue<T> { DataValue::UInt32(v) } } impl<T: IsParameterMask> From<u64> for DataValue<T> { fn from(v: u64) -> DataValue<T> { DataValue::UInt64(v) } } impl DataValueRaw { pub fn to_descriptive<'a, P>( &'a self, parameters: P, ) -> Result<DataValueDescriptive> where u64: DeconstructParameterMask<P>, { use DataValue::*; match *self { Boolean(v) => Ok(Boolean(v)), UInt8(v) => Ok(UInt8(v)), UInt16(v) => Ok(UInt16(v)), UInt32(v) => Ok(UInt32(v)), UInt64(v) => Ok(UInt64(v)), Int8(v) => Ok(Int8(v)), Int16(v) => Ok(Int16(v)), Int32(v) => Ok(Int32(v)), Int64(v) => Ok(Int64(v)), ParameterMask(v) => Ok(ParameterMask( v.deconstruct_parameter_mask(parameters)?, )), } } } impl DataValueDescriptive { pub fn to_raw<'a, P>(&'a self, parameters: P) -> Result<DataValueRaw> where &'a BTreeSet<String>: ConstructParameterMask<P>, { use DataValue::*; match *self { Boolean(ref v) => Ok(Boolean(*v)), UInt8(ref v) => Ok(UInt8(*v)), UInt16(ref v) => Ok(UInt16(*v)), UInt32(ref v) => Ok(UInt32(*v)), UInt64(ref v) => Ok(UInt64(*v)), Int8(ref v) => Ok(Int8(*v)), Int16(ref v) => Ok(Int16(*v)), Int32(ref v) => Ok(Int32(*v)), Int64(ref v) => Ok(Int64(*v)), ParameterMask(ref v) => Ok(ParameterMask( (&v).construct_parameter_mask(parameters)?, )), } } } impl<M: IsParameterMask> HasDataType for DataValue<M> { fn data_type(&self) -> DataType { use DataValue::*; match *self { Boolean(_) => DataType::Boolean, UInt8(_) => DataType::UInt8, UInt16(_) => DataType::UInt16, UInt32(_) => DataType::UInt32, UInt64(_) => DataType::UInt64, Int8(_) => DataType::Int8, Int16(_) => DataType::Int16, Int32(_) => DataType::Int32, Int64(_) => DataType::Int64, ParameterMask(_) => DataType::ParameterMask, } } } pub trait ConstructParameterMask<P> { fn construct_parameter_mask(&self, parameters: P) -> Result<u64>; } impl<'a> ConstructParameterMask<&'a [String]> for &'a BTreeSet<String> { fn construct_parameter_mask( &self, parameters: &[String], ) -> Result<u64> { (&self.iter() .map(|v| v.to_string()) .collect::<Vec<String>>()[..]) .construct_parameter_mask(parameters) } } #[cfg(feature = "bidir-map")] impl<'a> ConstructParameterMask<&'a BidirMap<String, u16>> for &'a BTreeSet<String> { fn construct_parameter_mask( &self, parameters: &BidirMap<String, u16>, ) -> Result<u64> { let mut mask = 0u64; let mut unused = BTreeSet::new(); for parameter in self.iter() { if let Some(i) = parameters.get_by_first(parameter) { if *i >= 64u16 { return Err(ErrorKind::ParameterOutOfMaskRange { parameter: parameter.to_string(), index: (*i) as usize, }.into()); } mask |= 1u64.shl(i); } else { unused.insert(parameter.to_string()); } } if !unused.is_empty() { return Err(ErrorKind::ParametersNotInParametrSet { parameters: unused, }.into()); } Ok(mask) } } impl<'a> ConstructParameterMask<&'a [String]> for &'a [String] { fn construct_parameter_mask( &self, parameters: &[String], ) -> Result<u64> { let mut mask = 0u64; let mut unused = BTreeSet::new(); for (i, parameter) in self.iter().enumerate() { if parameters.contains(¶meter) { if i >= 64 { return Err(ErrorKind::ParameterOutOfMaskRange { parameter: parameter.to_string(), index: i, }.into()); } else { mask |= 1u64.shl(i); } } else { unused.insert(parameter.to_string()); } } if !unused.is_empty() { return Err(ErrorKind::ParametersNotInParametrSet { parameters: unused, }.into()); } Ok(mask) } } pub trait DeconstructParameterMask<P> { fn deconstruct_parameter_mask( &self, parameters: P, ) -> Result<BTreeSet<String>>; } impl<'a> DeconstructParameterMask<&'a [String]> for u64 { fn deconstruct_parameter_mask( &self, parameters: &[String], ) -> Result<BTreeSet<String>> { let mut mask = BTreeSet::new(); for i in 0usize..64usize { if self.shr(i) & 1u64 == 1u64 { if let Some(p) = parameters.get(i) { mask.insert(p.to_string()); } else { return Err(ErrorKind::ParameterMaskOutOfRange { index: i, mask: *self, }.into()); } } } Ok(mask) } } #[cfg(feature = "bidir-map")] impl<'a> DeconstructParameterMask<&'a BidirMap<String, u16>> for u64 { fn deconstruct_parameter_mask( &self, parameters: &BidirMap<String, u16>, ) -> Result<BTreeSet<String>> { let mut mask = BTreeSet::new(); for i in 0u16..64u16 { if self.shr(i as usize) & 1u64 == 1u64 { if let Some(p) = parameters.get_by_second(&i) { mask.insert(p.to_string()); } else { return Err(ErrorKind::ParameterMaskOutOfRange { index: i as usize, mask: *self, }.into()); } } } Ok(mask) } }