diff --git a/Cargo.lock b/Cargo.lock index bbe341a..e806e29 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -425,7 +425,7 @@ checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d" [[package]] name = "libhaystack" -version = "3.1.4" +version = "3.1.5" dependencies = [ "chrono", "chrono-tz", diff --git a/Cargo.toml b/Cargo.toml index 0e7799e..0725f5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libhaystack" -version = "3.1.4" +version = "3.1.5" description = "Rust implementation of the Haystack 4 data types, defs, filter, units, and encodings" authors = ["J2 Innovations", "Radu Racariu "] edition = "2024" diff --git a/src/haystack/defs/namespace.rs b/src/haystack/defs/namespace.rs index f0ee3ee..c4aaa5c 100644 --- a/src/haystack/defs/namespace.rs +++ b/src/haystack/defs/namespace.rs @@ -551,7 +551,7 @@ impl Namespace { reflected.into_iter().collect() } - pub fn def_of_dict(&self, subject: &Dict) -> Dict { + pub fn def_of_dict(&self, subject: &Dict) -> &Dict { self.reflect(subject).entity_type } @@ -799,11 +799,7 @@ impl Namespace { let mut ref_tag = ref_target.as_ref().cloned(); let mut subjects = vec![subject.clone()]; - 'search: loop { - let Some(mut cur_subject) = subjects.pop() else { - break; - }; - + 'search: while let Some(mut cur_subject) = subjects.pop() { let id = cur_subject.get_ref("id").cloned(); while let Some((subject_key, subject_val)) = cur_subject.pop_first() { let subject_def = self.get_by_name(&subject_key); diff --git a/src/haystack/defs/reflection.rs b/src/haystack/defs/reflection.rs index 85b1f91..746540c 100644 --- a/src/haystack/defs/reflection.rs +++ b/src/haystack/defs/reflection.rs @@ -5,6 +5,7 @@ use std::collections::BTreeMap; use super::namespace::Namespace; +use crate::defs::namespace::EMPTY_DICT; use crate::haystack::defs::namespace::DefDict; use crate::haystack::val::{Dict, HaystackDict, Symbol}; @@ -17,7 +18,7 @@ pub struct Reflection<'a> { /// The def namespace used for the reflection pub ns: &'a Namespace, /// The entity type of the target dictionary - pub entity_type: Dict, + pub entity_type: &'a Dict, } impl<'a> Reflection<'a> { @@ -26,7 +27,7 @@ impl<'a> Reflection<'a> { subject: subject.clone(), defs, ns, - entity_type: Dict::default(), + entity_type: &EMPTY_DICT, }; reflect.compute_entity_type(); reflect @@ -52,7 +53,7 @@ impl<'a> Reflection<'a> { for def in &self.defs { let inheritance = self.ns.inheritance(def.def_symbol()); if inheritance.contains(&entity) { - types_with_inheritance.insert(def, inheritance.clone()); + types_with_inheritance.insert(def, inheritance); } } @@ -60,16 +61,15 @@ impl<'a> Reflection<'a> { // Just get the first entity if only one has been found. types_with_inheritance .keys() - .copied() .next() - .map_or(Dict::default(), |def| def.clone()) + .map_or(&EMPTY_DICT, |def| def) } else { // If multiple entity tags have been found then we need to find which tag is the most specific. // This can happen if a record has a tag like `ahu` and `equip`. The `ahu` tag extends `equip`. // Therefore, we need to check all the inheritance to find the first tag that isn't any of the // other tag's inheritance. This tag should be the most specific entity. - let all_defs = types_with_inheritance.keys().copied(); + let all_defs = types_with_inheritance.keys(); all_defs .into_iter() @@ -79,13 +79,13 @@ impl<'a> Reflection<'a> { !types_with_inheritance .iter() .any(|(inner_def, inheritance)| { - inner_def != def && inheritance.contains(def) + inner_def != *def && inheritance.contains(def) }) }) - .map_or(Dict::default(), |def| def.clone()) + .map_or(&EMPTY_DICT, |def| def) } } - None => Dict::default(), + None => &EMPTY_DICT, } } } diff --git a/src/haystack/units.rs b/src/haystack/units.rs index 49813d8..d36fd8a 100644 --- a/src/haystack/units.rs +++ b/src/haystack/units.rs @@ -43,8 +43,8 @@ pub fn match_units(dim: UnitDimensions, scale: f64) -> Vec<&'static Unit> { #[cfg(feature = "units-db")] { units_generated::UNITS - .iter() - .filter_map(|(_, u)| { + .values() + .filter_map(|u| { if u.dimensions.as_ref() == Some(&dim) && approx_eq(u.scale, scale) { Some(*u) } else { diff --git a/src/haystack/val/symbol.rs b/src/haystack/val/symbol.rs index 9966d3e..51c3fd9 100644 --- a/src/haystack/val/symbol.rs +++ b/src/haystack/val/symbol.rs @@ -35,9 +35,14 @@ impl Symbol { // Make a Haystack `Symbol` from a string value impl From<&str> for Symbol { fn from(value: &str) -> Self { - Symbol { - value: String::from(value), - } + Symbol::from(value.to_owned()) + } +} + +// Make a Haystack `Symbol` from a String value +impl From for Symbol { + fn from(value: String) -> Self { + Symbol { value } } } diff --git a/src/haystack/val/uri.rs b/src/haystack/val/uri.rs index 56c69d4..e17ecbe 100644 --- a/src/haystack/val/uri.rs +++ b/src/haystack/val/uri.rs @@ -40,9 +40,7 @@ impl From for Uri { // Make a Haystack `Uri` from a string value impl From<&str> for Uri { fn from(value: &str) -> Self { - Uri { - value: String::from(value), - } + Uri::from(value.to_owned()) } }