Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <radur@j2inn.com>"]
edition = "2024"
Expand Down
8 changes: 2 additions & 6 deletions src/haystack/defs/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions src/haystack/defs/reflection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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> {
Expand All @@ -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
Expand All @@ -52,24 +53,23 @@ 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);
}
}

if types_with_inheritance.len() == 1 {
// 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()
Expand All @@ -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,
}
}
}
4 changes: 2 additions & 2 deletions src/haystack/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions src/haystack/val/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> for Symbol {
fn from(value: String) -> Self {
Symbol { value }
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/haystack/val/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ impl From<String> 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())
}
}

Expand Down
Loading