Constructor for MetadataField Also checks that all FieldValue's are of the same variant
(name: String, values: HashSet<FieldValue>)
| 85 | /// |
| 86 | /// Also checks that all FieldValue's are of the same variant |
| 87 | pub fn new(name: String, values: HashSet<FieldValue>) -> Result<Self, Error> { |
| 88 | // validate input |
| 89 | let unique_types = values |
| 90 | .iter() |
| 91 | .map(|value| value.type_as_str()) |
| 92 | .collect::<HashSet<&str>>(); |
| 93 | if unique_types.len() > 1 { |
| 94 | return Err(Error::InvalidFieldValues( |
| 95 | "Field values must be homogeneous in type".to_owned(), |
| 96 | )); |
| 97 | } |
| 98 | let value_index = set_to_value_index(values); |
| 99 | let cardinality = value_index.len(); |
| 100 | // @NOTE: No. of dimensions are calculated to support 1 value |
| 101 | // more than the cardinality because the index in value_index |
| 102 | // starts with 1 and not 0. In other words, 0 is not used to |
| 103 | // represent any value. |
| 104 | let num_dims = nearest_power_of_two((cardinality + 1) as u16) |
| 105 | .ok_or(Error::InvalidFieldCardinality(format!("Field = {name}")))?; |
| 106 | Ok(Self { |
| 107 | name, |
| 108 | value_index, |
| 109 | num_dims, |
| 110 | }) |
| 111 | } |
| 112 | |
| 113 | /// Returns a numeric identifier for the value from the `value_index` |
| 114 | pub fn value_id(&self, value: &FieldValue) -> Result<u16, Error> { |
nothing calls this directly
no test coverage detected