use ifc_model::{EntityId, Model, Value};
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CurrencyError {
Unstated,
Ambiguous {
currencies: Vec<String>,
},
}
pub fn project_currency(model: &Model) -> Result<String, CurrencyError> {
let mut found: Vec<String> = Vec::new();
for (_, entity) in model.of_type("IFCMONETARYUNIT") {
let currency = match entity.attribute(0) {
Some(Value::Text(text)) => Some(text.to_string()),
Some(Value::Enum(token)) => Some(token.to_string()),
_ => None,
};
if let Some(currency) = currency {
if !found.contains(¤cy) {
found.push(currency);
}
}
}
match found.len() {
0 => Err(CurrencyError::Unstated),
1 => Ok(found.remove(0)),
_ => {
found.sort();
Err(CurrencyError::Ambiguous { currencies: found })
}
}
}
#[must_use]
pub fn monetary_units(model: &Model) -> Vec<EntityId> {
model.ids_of_type("IFCMONETARYUNIT").to_vec()
}