use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
use std::fmt;
use crate::model::Graph;
use crate::p21::{Instance, Literal, Param, Record};
use crate::policy::MAX_CONVERSION_DEPTH;
use crate::record;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[non_exhaustive]
pub enum UnitKind {
Length,
PlaneAngle,
SolidAngle,
}
impl UnitKind {
const ALL: [Self; 3] = [Self::Length, Self::PlaneAngle, Self::SolidAngle];
fn slot(self) -> usize {
match self {
Self::Length => 0,
Self::PlaneAngle => 1,
Self::SolidAngle => 2,
}
}
}
impl fmt::Display for UnitKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Length => "length",
Self::PlaneAngle => "plane angle",
Self::SolidAngle => "solid angle",
})
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Unit {
pub instance: u64,
pub kind: UnitKind,
pub name: String,
pub factor: f64,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Uncertainty {
pub instance: u64,
pub name: String,
pub description: Option<String>,
pub value: f64,
pub unit: Unit,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct ContextUnits {
pub context: u64,
pub dimension: Option<u32>,
#[cfg_attr(feature = "serde", serde(serialize_with = "outcome"))]
length: Result<Unit, UnitError>,
#[cfg_attr(feature = "serde", serde(serialize_with = "outcome"))]
plane_angle: Result<Unit, UnitError>,
#[cfg_attr(feature = "serde", serde(serialize_with = "outcome"))]
solid_angle: Result<Unit, UnitError>,
#[cfg_attr(feature = "serde", serde(serialize_with = "outcome"))]
uncertainty: Result<Vec<Uncertainty>, UnitError>,
}
#[cfg(feature = "serde")]
fn outcome<T, S>(result: &Result<T, UnitError>, serializer: S) -> Result<S::Ok, S::Error>
where
T: serde::Serialize,
S: serde::Serializer,
{
use serde::ser::SerializeMap;
let mut map = serializer.serialize_map(Some(1))?;
match result {
Ok(value) => map.serialize_entry("ok", value)?,
Err(error) => map.serialize_entry("error", error)?,
}
map.end()
}
impl ContextUnits {
pub fn of_context(graph: &Graph<'_>, context: u64) -> Result<Self, UnitError> {
let exchange = graph.exchange();
let instance = exchange.get(context).ok_or(UnitError::Undefined {
from: context,
to: context,
})?;
let records: Vec<Record<'_>> = exchange.records(instance).collect();
if !records.iter().any(|r| record::is_context_record(*r)) {
return Err(UnitError::NotAContext {
instance: context,
entity: record::entity(exchange, instance),
});
}
if record::repeats_partial(exchange, instance) {
return Err(UnitError::Malformed {
instance: context,
reason: REPEATED,
});
}
let simple = !instance.is_complex();
let own = |name: &'static str| -> Option<Result<Param<'_>, UnitError>> {
let found = records.iter().copied().find(|r| r.is(name))?;
Some(
record::exact_params(found, if simple { 3 } else { 1 })
.and_then(|params| params.into_iter().last())
.ok_or(UnitError::Malformed {
instance: context,
reason: match name {
GEOMETRIC => "GEOMETRIC_REPRESENTATION_CONTEXT has the wrong number of attributes",
UNITS => "GLOBAL_UNIT_ASSIGNED_CONTEXT has the wrong number of attributes",
_ => "GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT has the wrong number of attributes",
},
}),
)
};
let dimension = match own(GEOMETRIC).transpose()? {
None => None,
Some(Param::Integer(literal)) => Some(
literal
.to_i64()
.and_then(|value| u32::try_from(value).ok())
.filter(|&value| value > 0)
.ok_or(UnitError::Malformed {
instance: context,
reason: "coordinate_space_dimension is not a positive integer",
})?,
),
Some(_) => {
return Err(UnitError::Malformed {
instance: context,
reason: "coordinate_space_dimension is not a positive integer",
});
}
};
let units = match own(UNITS) {
None => Ok(Vec::new()),
Some(param) => param.and_then(|param| {
reference_set(¶m).ok_or(UnitError::Malformed {
instance: context,
reason: "the unit set is not a non-empty set of references",
})
}),
};
let [length, plane_angle, solid_angle] = match units {
Ok(units) => context_units(graph, context, &units),
Err(error) => [error.clone(), error.clone(), error].map(Err),
};
let uncertainty = match own(UNCERTAINTIES) {
None => Ok(Vec::new()),
Some(param) => param
.and_then(|param| {
reference_set(¶m).ok_or(UnitError::Malformed {
instance: context,
reason: "the uncertainty set is not a non-empty set of references",
})
})
.and_then(|ids| {
ids.into_iter()
.map(|id| read_uncertainty(graph, id, context))
.collect()
}),
};
Ok(Self {
context,
dimension,
length,
plane_angle,
solid_angle,
uncertainty,
})
}
pub fn of_representation(graph: &Graph<'_>, representation: u64) -> Result<Self, UnitError> {
Self::of_context(graph, representation_context(graph, representation)?)
}
pub fn of_item(graph: &Graph<'_>, item: u64) -> Result<Self, UnitError> {
let mut contexts: Vec<Self> = Vec::new();
let mut read = HashSet::new();
for (holder, context) in holders(graph, item)? {
if !read.insert(context) {
continue;
}
let units =
Self::of_context(graph, context).map_err(|reason| UnitError::HolderUnreadable {
item,
holder,
reason: Box::new(reason),
})?;
contexts.push(units);
}
let mut contexts = contexts.into_iter();
let Some(first) = contexts.next() else {
return Err(UnitError::NotHeld { item });
};
let others: Vec<Self> = contexts.collect();
if let Some(other) = others.iter().find(|o| o.dimension != first.dimension) {
return Err(UnitError::ContextsDisagree {
item,
kind: None,
contexts: [first.context, other.context],
});
}
let length = agree(item, &first, &others, UnitKind::Length);
let plane_angle = agree(item, &first, &others, UnitKind::PlaneAngle);
let solid_angle = agree(item, &first, &others, UnitKind::SolidAngle);
let uncertainty = agree_uncertainty(item, &first, &others);
Ok(Self {
length,
plane_angle,
solid_angle,
uncertainty,
..first
})
}
pub fn length(&self) -> Result<&Unit, UnitError> {
self.unit(UnitKind::Length)
}
pub fn plane_angle(&self) -> Result<&Unit, UnitError> {
self.unit(UnitKind::PlaneAngle)
}
pub fn solid_angle(&self) -> Result<&Unit, UnitError> {
self.unit(UnitKind::SolidAngle)
}
pub fn unit(&self, kind: UnitKind) -> Result<&Unit, UnitError> {
self.result(kind).as_ref().map_err(Clone::clone)
}
pub fn uncertainty(&self) -> Result<&[Uncertainty], UnitError> {
self.uncertainty.as_deref().map_err(Clone::clone)
}
fn result(&self, kind: UnitKind) -> &Result<Unit, UnitError> {
match kind {
UnitKind::Length => &self.length,
UnitKind::PlaneAngle => &self.plane_angle,
UnitKind::SolidAngle => &self.solid_angle,
}
}
}
const GEOMETRIC: &str = "GEOMETRIC_REPRESENTATION_CONTEXT";
const UNITS: &str = "GLOBAL_UNIT_ASSIGNED_CONTEXT";
const UNCERTAINTIES: &str = "GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT";
const REPEATED: &str = "a partial entity is repeated, so its attributes are stated twice";
pub fn unit(graph: &Graph<'_>, id: u64) -> Result<Unit, UnitError> {
unit_at(graph, id, id, 0)
}
pub fn representation_context(graph: &Graph<'_>, representation: u64) -> Result<u64, UnitError> {
let exchange = graph.exchange();
let instance = exchange.get(representation).ok_or(UnitError::Undefined {
from: representation,
to: representation,
})?;
let read = record::representation(exchange, representation).map_err(|_| {
UnitError::NotARepresentation {
instance: representation,
entity: record::entity(exchange, instance),
}
})?;
let context = exchange.get(read.context).ok_or(UnitError::Undefined {
from: representation,
to: read.context,
})?;
if !exchange.records(context).any(record::is_context_record) {
return Err(UnitError::NotAContext {
instance: read.context,
entity: record::entity(exchange, context),
});
}
Ok(read.context)
}
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "kind", rename_all = "snake_case"))]
#[non_exhaustive]
pub enum UnitError {
Undefined {
from: u64,
to: u64,
},
NotAUnit {
instance: u64,
entity: String,
},
Unsupported {
instance: u64,
entity: String,
},
Malformed {
instance: u64,
reason: &'static str,
},
NotPositive {
instance: u64,
value: f64,
},
KindMismatch {
instance: u64,
declared: UnitKind,
found: UnitKind,
},
TooDeep {
instance: u64,
},
NotAContext {
instance: u64,
entity: String,
},
NotARepresentation {
instance: u64,
entity: String,
},
Missing {
context: u64,
#[cfg_attr(feature = "serde", serde(rename = "unit_kind"))]
kind: UnitKind,
},
Ambiguous {
context: u64,
#[cfg_attr(feature = "serde", serde(rename = "unit_kind"))]
kind: UnitKind,
units: Vec<u64>,
},
Unreadable {
context: u64,
unit: u64,
reason: Box<UnitError>,
},
NotHeld {
item: u64,
},
NotFollowed {
item: u64,
container: u64,
entity: String,
},
HolderUnreadable {
item: u64,
holder: u64,
reason: Box<UnitError>,
},
ContextsDisagree {
item: u64,
#[cfg_attr(feature = "serde", serde(rename = "unit_kind"))]
kind: Option<UnitKind>,
contexts: [u64; 2],
},
}
impl fmt::Display for UnitError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Undefined { from, to } if from == to => write!(f, "#{to} is not defined"),
Self::Undefined { from, to } => {
write!(f, "#{from} refers to #{to}, which is not defined")
}
Self::NotAUnit { instance, entity } => {
write!(f, "#{instance} ({entity}) is not a unit")
}
Self::Unsupported { instance, entity } => write!(
f,
"#{instance} ({entity}) is not read: only SI and conversion-based units of \
length, plane angle and solid angle are"
),
Self::Malformed { instance, reason } => write!(f, "#{instance}: {reason}"),
Self::NotPositive { instance, value } => {
write!(f, "#{instance}: {value} is not a positive finite number")
}
Self::KindMismatch {
instance,
declared,
found,
} => write!(
f,
"#{instance} is a {declared} unit, but its definition states a {found}"
),
Self::TooDeep { instance } => write!(
f,
"#{instance}: conversion-based units nested more than {MAX_CONVERSION_DEPTH} \
deep, or in a cycle"
),
Self::NotAContext { instance, entity } => {
write!(f, "#{instance} ({entity}) is not a representation context")
}
Self::NotARepresentation { instance, entity } => {
write!(f, "#{instance} ({entity}) is not a representation")
}
Self::Missing { context, kind } => {
write!(f, "context #{context} assigns no {kind} unit")
}
Self::Ambiguous {
context,
kind,
units,
} => {
write!(f, "context #{context} assigns {kind} units that differ:")?;
for (i, unit) in units.iter().enumerate() {
write!(f, "{} #{unit}", if i == 0 { "" } else { "," })?;
}
Ok(())
}
Self::Unreadable {
context,
unit,
reason,
} => write!(
f,
"context #{context}: unit #{unit} cannot be read: {reason}"
),
Self::NotHeld { item } => write!(
f,
"#{item} is held by no representation, directly or through a shell, solid or \
surface model"
),
Self::NotFollowed {
item,
container,
entity,
} => write!(
f,
"#{item} may be held through #{container} ({entity}), which stepq does not follow"
),
Self::HolderUnreadable {
item,
holder,
reason,
} => write!(
f,
"#{item} is held by representation #{holder}, which cannot be read: {reason}"
),
Self::ContextsDisagree {
item,
kind,
contexts: [a, b],
} => match kind {
Some(kind) => write!(
f,
"#{item} is held in contexts #{a} and #{b}, which state different {kind} \
units or uncertainties"
),
None => write!(
f,
"#{item} is held in contexts #{a} and #{b}, which differ in dimension"
),
},
}
}
}
fn context_units(graph: &Graph<'_>, context: u64, units: &[u64]) -> [Result<Unit, UnitError>; 3] {
let mut found: [Vec<Unit>; 3] = Default::default();
let mut refused: [Option<(u64, UnitError)>; 3] = Default::default();
let mut unknown: Option<(u64, UnitError)> = None;
for &id in units {
match unit_at(graph, id, context, 0) {
Ok(unit) => found[unit.kind.slot()].push(unit),
Err(error) => match declared_kind(graph, id) {
Declared::Kind(kind) => {
refused[kind.slot()].get_or_insert((id, error));
}
Declared::Other => {}
Declared::Unknown => {
unknown.get_or_insert((id, error));
}
},
}
}
UnitKind::ALL.map(|kind| {
let slot = kind.slot();
if let Some((unit, error)) = refused[slot].as_ref().or(unknown.as_ref()) {
return Err(UnitError::Unreadable {
context,
unit: *unit,
reason: Box::new(error.clone()),
});
}
let units = &found[slot];
let first = units.first().ok_or(UnitError::Missing { context, kind })?;
if units
.iter()
.any(|unit| unit.factor.to_bits() != first.factor.to_bits())
{
return Err(UnitError::Ambiguous {
context,
kind,
units: units.iter().map(|unit| unit.instance).collect(),
});
}
Ok(first.clone())
})
}
fn agree(
item: u64,
first: &ContextUnits,
others: &[ContextUnits],
kind: UnitKind,
) -> Result<Unit, UnitError> {
let unit = first.result(kind).clone()?;
for other in others {
let theirs = other.result(kind).as_ref().map_err(Clone::clone)?;
if theirs.factor.to_bits() != unit.factor.to_bits() {
return Err(UnitError::ContextsDisagree {
item,
kind: Some(kind),
contexts: [first.context, other.context],
});
}
}
Ok(unit)
}
fn agree_uncertainty(
item: u64,
first: &ContextUnits,
others: &[ContextUnits],
) -> Result<Vec<Uncertainty>, UnitError> {
let ours = first.uncertainty.clone()?;
for other in others {
let theirs = other.uncertainty.as_ref().map_err(Clone::clone)?;
let same = |a: &Uncertainty, b: &Uncertainty| {
a.name == b.name && a.unit.kind == b.unit.kind && a.value.to_bits() == b.value.to_bits()
};
let differs =
(0..ours.len().max(theirs.len())).find(|&i| match (ours.get(i), theirs.get(i)) {
(Some(a), Some(b)) => !same(a, b),
_ => true,
});
if let Some(i) = differs {
let kind = ours.get(i).or(theirs.get(i)).map(|u| u.unit.kind);
return Err(UnitError::ContextsDisagree {
item,
kind,
contexts: [first.context, other.context],
});
}
}
Ok(ours)
}
fn holders(graph: &Graph<'_>, item: u64) -> Result<Vec<(u64, u64)>, UnitError> {
let exchange = graph.exchange();
let start = graph.node(item).ok_or(UnitError::Undefined {
from: item,
to: item,
})?;
let mut seen = HashSet::from([start]);
let mut stack = vec![start];
let mut users: HashMap<usize, Holding> = HashMap::new();
let mut listed = HashSet::new();
let mut found: Vec<(usize, u64)> = Vec::new();
while let Some(node) = stack.pop() {
let held = graph.instance(node).id;
for &user in graph.referenced_by(node) {
let holds = match users.entry(user) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(holding(graph, item, graph.instance(user))?),
};
match holds {
Holding::Container(ids) => {
if ids.contains(held) && seen.insert(user) {
stack.push(user);
}
}
Holding::Representation(ids, context) => {
if ids.contains(held) && listed.insert(user) {
let context =
context
.clone()
.map_err(|reason| UnitError::HolderUnreadable {
item,
holder: graph.instance(user).id,
reason: Box::new(reason),
})?;
found.push((user, context));
}
}
Holding::NotFollowed(ids) => {
if ids.contains(held) {
let user = graph.instance(user);
return Err(UnitError::NotFollowed {
item,
container: user.id,
entity: record::entity(exchange, user),
});
}
}
Holding::No => {}
}
}
}
found.sort_unstable();
Ok(found
.into_iter()
.map(|(node, context)| (graph.instance(node).id, context))
.collect())
}
enum Holding {
Container(Ids),
Representation(Ids, Result<u64, UnitError>),
NotFollowed(Ids),
No,
}
struct Ids {
list: Vec<u64>,
set: Option<HashSet<u64>>,
looked: bool,
}
impl Ids {
fn new(list: Vec<u64>) -> Self {
Self {
list,
set: None,
looked: false,
}
}
fn contains(&mut self, id: u64) -> bool {
if !self.looked {
self.looked = true;
return self.list.contains(&id);
}
let list = &self.list;
self.set
.get_or_insert_with(|| list.iter().copied().collect())
.contains(&id)
}
}
fn holding(graph: &Graph<'_>, item: u64, user: &Instance) -> Result<Holding, UnitError> {
let exchange = graph.exchange();
if user.is_complex() {
if record::partial(exchange, user, "REPRESENTATION").is_none() {
if exchange
.records(user)
.any(|partial| record::container(partial).is_some())
{
return Err(UnitError::NotFollowed {
item,
container: user.id,
entity: record::entity(exchange, user),
});
}
return Ok(Holding::No);
}
let Ok(read) = record::representation(exchange, user.id) else {
return Err(UnitError::HolderUnreadable {
item,
holder: user.id,
reason: Box::new(UnitError::NotARepresentation {
instance: user.id,
entity: record::entity(exchange, user),
}),
});
};
return Ok(Holding::Representation(
Ids::new(read.items),
representation_context(graph, user.id),
));
}
let Some(simple) = exchange.records(user).next() else {
return Ok(Holding::No);
};
if let Some(container) = record::container(simple) {
let params =
record::exact_params(simple, container.params).ok_or(UnitError::Malformed {
instance: user.id,
reason: "a face, shell, solid or surface model with the wrong number of attributes",
})?;
let ids = container
.holds
.iter()
.flat_map(|&index| record::held(¶ms[index]))
.collect();
return Ok(Holding::Container(Ids::new(ids)));
}
let Some(items) = simple.param(1).and_then(|param| param.list()) else {
return Ok(Holding::No);
};
let ids = Ids::new(items.filter_map(|item| item.reference()).collect());
let names_context = match simple.param(2) {
Some(Param::Reference(context)) => exchange
.get(context)
.is_none_or(|context| exchange.records(context).any(record::is_context_record)),
_ => false,
};
if names_context || record::upper(simple.name()).contains("REPRESENTATION") {
return Ok(Holding::Representation(
ids,
representation_context(graph, user.id),
));
}
if matches!(simple.param(2), Some(Param::Reference(_))) {
return Ok(Holding::NotFollowed(ids));
}
Ok(Holding::No)
}
fn reference_set(param: &Param<'_>) -> Option<Vec<u64>> {
let ids = param
.list()?
.map(|item| item.reference())
.collect::<Option<Vec<u64>>>()?;
(!ids.is_empty()).then_some(ids)
}
enum Declared {
Kind(UnitKind),
Other,
Unknown,
}
fn declared_kind(graph: &Graph<'_>, id: u64) -> Declared {
let exchange = graph.exchange();
let Some(instance) = exchange.get(id) else {
return Declared::Unknown;
};
if record::repeats_partial(exchange, instance) {
return Declared::Unknown;
}
let records: Vec<Record<'_>> = exchange.records(instance).collect();
match kind_record(&records) {
Ok(Some(kind)) => return Declared::Kind(kind),
Ok(None) => {}
Err(_) => return Declared::Unknown,
}
let other = records.iter().any(|r| is_other_quantity(*r));
if let Some(si) = records.iter().find(|r| r.is("SI_UNIT")) {
let name = si
.params()
.last()
.and_then(|p| p.literal())
.and_then(Literal::enumeration);
return match name {
Some(name) => match si_kind(name) {
Some(kind) if !other => Declared::Kind(kind),
None if is_si_name(name) => Declared::Other,
_ => Declared::Unknown,
},
None => Declared::Unknown,
};
}
if other && !states_length_or_angle(graph, &records, id) {
return Declared::Other;
}
Declared::Unknown
}
fn states_length_or_angle(graph: &Graph<'_>, records: &[Record<'_>], id: u64) -> bool {
let Some(factor) = records
.iter()
.find(|r| r.is("CONVERSION_BASED_UNIT"))
.and_then(|r| r.params().last())
.and_then(|param| param.reference())
else {
return false;
};
match factor_measure(graph, factor, id) {
Ok(measure) => !measure.kinds.is_empty() || unit_at(graph, measure.unit, factor, 1).is_ok(),
Err(_) => false,
}
}
fn kind_record(records: &[Record<'_>]) -> Result<Option<UnitKind>, &'static str> {
let mut kind = None;
for record in records {
let this = if record.is("LENGTH_UNIT") {
UnitKind::Length
} else if record.is("PLANE_ANGLE_UNIT") {
UnitKind::PlaneAngle
} else if record.is("SOLID_ANGLE_UNIT") {
UnitKind::SolidAngle
} else {
continue;
};
if kind.is_some_and(|k| k != this) {
return Err("declared as two kinds of unit");
}
kind = Some(this);
}
Ok(kind)
}
fn is_other_quantity(record: Record<'_>) -> bool {
const READ: &[&str] = &[
"NAMED_UNIT",
"SI_UNIT",
"CONVERSION_BASED_UNIT",
"CONTEXT_DEPENDENT_UNIT",
"LENGTH_UNIT",
"PLANE_ANGLE_UNIT",
"SOLID_ANGLE_UNIT",
];
let name = record::upper(record.name());
name.ends_with("_UNIT") && !name.contains("_WITH_") && !READ.iter().any(|n| record.is(n))
}
fn is_si_name(name: &str) -> bool {
const NAMES: [&str; 28] = [
"METRE",
"GRAM",
"SECOND",
"AMPERE",
"KELVIN",
"MOLE",
"CANDELA",
"RADIAN",
"STERADIAN",
"HERTZ",
"NEWTON",
"PASCAL",
"JOULE",
"WATT",
"COULOMB",
"VOLT",
"FARAD",
"OHM",
"SIEMENS",
"WEBER",
"TESLA",
"HENRY",
"DEGREE_CELSIUS",
"LUMEN",
"LUX",
"BECQUEREL",
"GRAY",
"SIEVERT",
];
NAMES.iter().any(|known| known.eq_ignore_ascii_case(name))
}
fn si_kind(name: &str) -> Option<UnitKind> {
match name.to_ascii_uppercase().as_str() {
"METRE" => Some(UnitKind::Length),
"RADIAN" => Some(UnitKind::PlaneAngle),
"STERADIAN" => Some(UnitKind::SolidAngle),
_ => None,
}
}
fn si_factor(kind: UnitKind, prefix: Option<&str>) -> Option<f64> {
let Some(prefix) = prefix else {
return Some(if kind == UnitKind::Length { 1e3 } else { 1.0 });
};
let (metre, other) = match prefix.to_ascii_uppercase().as_str() {
"EXA" => (1e21, 1e18),
"PETA" => (1e18, 1e15),
"TERA" => (1e15, 1e12),
"GIGA" => (1e12, 1e9),
"MEGA" => (1e9, 1e6),
"KILO" => (1e6, 1e3),
"HECTO" => (1e5, 1e2),
"DECA" => (1e4, 1e1),
"DECI" => (1e2, 1e-1),
"CENTI" => (1e1, 1e-2),
"MILLI" => (1.0, 1e-3),
"MICRO" => (1e-3, 1e-6),
"NANO" => (1e-6, 1e-9),
"PICO" => (1e-9, 1e-12),
"FEMTO" => (1e-12, 1e-15),
"ATTO" => (1e-15, 1e-18),
_ => return None,
};
Some(if kind == UnitKind::Length {
metre
} else {
other
})
}
#[derive(Clone, Copy)]
enum Evidence {
Kind(UnitKind),
Angle,
}
fn check(instance: u64, kind: UnitKind, evidence: Option<Evidence>) -> Result<(), UnitError> {
let found = match evidence {
Some(Evidence::Kind(found)) if found != kind => found,
Some(Evidence::Angle) if kind == UnitKind::Length => UnitKind::PlaneAngle,
_ => return Ok(()),
};
Err(UnitError::KindMismatch {
instance,
declared: kind,
found,
})
}
fn unit_at(graph: &Graph<'_>, id: u64, from: u64, depth: usize) -> Result<Unit, UnitError> {
if depth > MAX_CONVERSION_DEPTH {
return Err(UnitError::TooDeep { instance: id });
}
let exchange = graph.exchange();
let instance = exchange
.get(id)
.ok_or(UnitError::Undefined { from, to: id })?;
if record::repeats_partial(exchange, instance) {
return Err(UnitError::Malformed {
instance: id,
reason: REPEATED,
});
}
let records: Vec<Record<'_>> = exchange.records(instance).collect();
let declared = kind_record(&records).map_err(|reason| UnitError::Malformed {
instance: id,
reason,
})?;
let unsupported = || UnitError::Unsupported {
instance: id,
entity: record::entity(exchange, instance),
};
if records
.iter()
.any(|r| r.is("CONTEXT_DEPENDENT_UNIT") || is_other_quantity(*r))
{
return Err(unsupported());
}
let dimensions = if instance.is_complex() {
match records.iter().find(|r| r.is("NAMED_UNIT")) {
Some(named) => Some(
record::exact_params(*named, 1)
.and_then(|params| params.into_iter().next())
.ok_or(UnitError::Malformed {
instance: id,
reason: "NAMED_UNIT has the wrong number of attributes",
})?,
),
None => None,
}
} else {
None
};
let simple = !instance.is_complex();
let si = records.iter().find(|r| r.is("SI_UNIT"));
let conversion = records.iter().find(|r| r.is("CONVERSION_BASED_UNIT"));
let unit = Read {
graph,
id,
declared,
};
match (si, conversion) {
(Some(_), Some(_)) => Err(UnitError::Malformed {
instance: id,
reason: "both an SI unit and a conversion-based unit",
}),
(Some(&si), None) => unit.si(si, simple, dimensions),
(None, Some(&conversion)) => unit.conversion(conversion, simple, dimensions, depth),
(None, None) if declared.is_some() || records.iter().any(|r| r.is("NAMED_UNIT")) => {
Err(unsupported())
}
(None, None) => Err(UnitError::NotAUnit {
instance: id,
entity: record::entity(exchange, instance),
}),
}
}
struct Read<'g, 'a> {
graph: &'g Graph<'a>,
id: u64,
declared: Option<UnitKind>,
}
impl Read<'_, '_> {
fn malformed(&self, reason: &'static str) -> UnitError {
UnitError::Malformed {
instance: self.id,
reason,
}
}
fn si(
&self,
si: Record<'_>,
simple: bool,
dimensions: Option<Param<'_>>,
) -> Result<Unit, UnitError> {
let params = record::exact_params(si, if simple { 3 } else { 2 })
.ok_or_else(|| self.malformed("SI_UNIT has the wrong number of attributes"))?;
let (dimensions, prefix, name) = match params.as_slice() {
[dimensions, prefix, name] => (Some(dimensions.clone()), prefix, name),
[prefix, name] => (dimensions, prefix, name),
_ => return Err(self.malformed("SI_UNIT has the wrong number of attributes")),
};
let prefix = match prefix {
Param::Unset => None,
Param::Enumeration(literal) => literal.enumeration(),
_ => return Err(self.malformed("the SI prefix is neither $ nor an enumeration")),
};
let Some(name) = name.literal().and_then(Literal::enumeration) else {
return Err(self.malformed("the SI unit name is not an enumeration"));
};
if !is_si_name(name) {
return Err(self.malformed("the SI unit name is not an si_unit_name"));
}
let Some(kind) = si_kind(name) else {
let exchange = self.graph.exchange();
return Err(UnitError::Unsupported {
instance: self.id,
entity: exchange
.get(self.id)
.map(|instance| record::entity(exchange, instance))
.unwrap_or_default(),
});
};
if let Some(declared) = self.declared {
check(self.id, declared, Some(Evidence::Kind(kind)))?;
}
if let Some(dimensions) = dimensions {
check(self.id, kind, self.dimensions(&dimensions)?)?;
}
let factor = si_factor(kind, prefix).ok_or_else(|| self.malformed("unknown SI prefix"))?;
Ok(Unit {
instance: self.id,
kind,
name: format!("{}{name}", prefix.unwrap_or_default()).to_ascii_lowercase(),
factor,
})
}
fn conversion(
&self,
conversion: Record<'_>,
simple: bool,
dimensions: Option<Param<'_>>,
depth: usize,
) -> Result<Unit, UnitError> {
let params =
record::exact_params(conversion, if simple { 3 } else { 2 }).ok_or_else(|| {
self.malformed("CONVERSION_BASED_UNIT has the wrong number of attributes")
})?;
let (dimensions, name, factor) = match params.as_slice() {
[dimensions, name, factor] => (Some(dimensions.clone()), name, factor),
[name, factor] => (dimensions, name, factor),
_ => {
return Err(
self.malformed("CONVERSION_BASED_UNIT has the wrong number of attributes")
);
}
};
let name = string(name)
.ok_or_else(|| self.malformed("the conversion-based unit's name is not a string"))?;
let factor = factor
.reference()
.ok_or_else(|| self.malformed("the conversion factor is not a reference"))?;
let measure = factor_measure(self.graph, factor, self.id)?;
let base = unit_at(self.graph, measure.unit, factor, depth + 1)?;
let kind = self.declared.unwrap_or(base.kind);
check(self.id, kind, Some(Evidence::Kind(base.kind)))?;
for evidence in measure.kinds {
check(self.id, kind, Some(Evidence::Kind(evidence)))?;
}
if let Some(dimensions) = dimensions {
check(self.id, kind, self.dimensions(&dimensions)?)?;
}
let value = measure.value * base.factor;
if !(value.is_finite() && value > 0.0) {
return Err(UnitError::NotPositive {
instance: factor,
value,
});
}
Ok(Unit {
instance: self.id,
kind,
name: name.to_lowercase(),
factor: value,
})
}
fn dimensions(&self, param: &Param<'_>) -> Result<Option<Evidence>, UnitError> {
let id = match param {
Param::Derived => return Ok(None),
Param::Reference(id) => *id,
_ => {
return Err(self.malformed("named_unit.dimensions is neither * nor a reference"));
}
};
let exchange = self.graph.exchange();
let instance = exchange.get(id).ok_or(UnitError::Undefined {
from: self.id,
to: id,
})?;
let malformed = |reason| UnitError::Malformed {
instance: id,
reason,
};
let exponents: Vec<f64> = exchange
.records(instance)
.next()
.filter(|r| !instance.is_complex() && r.is("DIMENSIONAL_EXPONENTS"))
.and_then(|r| record::exact_params(r, 7))
.and_then(|params| params.iter().map(record::real).collect())
.ok_or_else(|| malformed("not a DIMENSIONAL_EXPONENTS with seven numbers"))?;
#[expect(
clippy::float_cmp,
reason = "exponents are small integers written as reals; any other value is not a length or angle"
)]
let is = |expected: [f64; 7]| exponents.iter().zip(expected).all(|(a, b)| *a == b);
if is([1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) {
Ok(Some(Evidence::Kind(UnitKind::Length)))
} else if is([0.0; 7]) {
Ok(Some(Evidence::Angle))
} else {
Err(malformed(
"the dimensional exponents are those of neither a length nor an angle",
))
}
}
}
enum Subtype {
Neutral,
Kind(UnitKind),
Other,
}
fn measure_subtype(name: &str) -> Option<Subtype> {
Some(match name {
"MEASURE_WITH_UNIT" | "RATIO_MEASURE_WITH_UNIT" => Subtype::Neutral,
"LENGTH_MEASURE_WITH_UNIT" | "POSITIVE_LENGTH_MEASURE_WITH_UNIT" => {
Subtype::Kind(UnitKind::Length)
}
"PLANE_ANGLE_MEASURE_WITH_UNIT" | "POSITIVE_PLANE_ANGLE_MEASURE_WITH_UNIT" => {
Subtype::Kind(UnitKind::PlaneAngle)
}
"SOLID_ANGLE_MEASURE_WITH_UNIT" => Subtype::Kind(UnitKind::SolidAngle),
other if other.ends_with("MEASURE_WITH_UNIT") => Subtype::Other,
_ => return None,
})
}
struct Measure {
value: f64,
unit: u64,
kinds: Vec<UnitKind>,
}
fn factor_measure(graph: &Graph<'_>, id: u64, from: u64) -> Result<Measure, UnitError> {
let exchange = graph.exchange();
let instance = exchange
.get(id)
.ok_or(UnitError::Undefined { from, to: id })?;
let malformed = |reason| UnitError::Malformed {
instance: id,
reason,
};
if record::repeats_partial(exchange, instance) {
return Err(malformed(REPEATED));
}
let records: Vec<Record<'_>> = exchange.records(instance).collect();
let mut kinds = Vec::new();
let mut base = None;
for partial in &records {
let name = record::upper(partial.name());
let Some(subtype) = measure_subtype(&name) else {
continue;
};
if !instance.is_complex() || name == "MEASURE_WITH_UNIT" {
base = Some(*partial);
}
match subtype {
Subtype::Neutral => {}
Subtype::Kind(kind) => kinds.push(kind),
Subtype::Other => {
return Err(malformed(
"the conversion factor measures something other than a length or an angle",
));
}
}
}
let params = base
.and_then(|partial| record::exact_params(partial, 2))
.ok_or_else(|| malformed("not a measure with unit with a value and a unit"))?;
let (value, typed) =
measure_value(¶ms[0]).ok_or_else(|| malformed("the value is not a number"))?;
if let Some(kind) = typed.map_err(malformed)? {
kinds.push(kind);
}
let unit = params[1]
.reference()
.ok_or_else(|| malformed("the unit is not a reference"))?;
Ok(Measure { value, unit, kinds })
}
type TypedKind = Result<Option<UnitKind>, &'static str>;
fn measure_value(param: &Param<'_>) -> Option<(f64, TypedKind)> {
let (literal, kind) = match param {
Param::Typed(typed) => {
let [value] = record::exact_params(*typed, 1)?.try_into().ok()?;
let kind = match record::upper(typed.name()).as_str() {
"LENGTH_MEASURE" | "POSITIVE_LENGTH_MEASURE" => Ok(Some(UnitKind::Length)),
"PLANE_ANGLE_MEASURE" | "POSITIVE_PLANE_ANGLE_MEASURE" => {
Ok(Some(UnitKind::PlaneAngle))
}
"SOLID_ANGLE_MEASURE" => Ok(Some(UnitKind::SolidAngle)),
"RATIO_MEASURE" | "POSITIVE_RATIO_MEASURE" => Ok(None),
_ => Err("the value is typed as a measure of another quantity"),
};
(value, kind)
}
other => (other.clone(), Ok(None)),
};
match literal {
Param::Integer(literal) | Param::Real(literal) => Some((literal.to_f64()?, kind)),
_ => None,
}
}
fn string(param: &Param<'_>) -> Option<String> {
match param {
Param::String(literal) => literal.decode().ok().map(std::borrow::Cow::into_owned),
_ => None,
}
}
fn read_uncertainty(graph: &Graph<'_>, id: u64, from: u64) -> Result<Uncertainty, UnitError> {
let exchange = graph.exchange();
let instance = exchange
.get(id)
.ok_or(UnitError::Undefined { from, to: id })?;
let malformed = |reason| UnitError::Malformed {
instance: id,
reason,
};
if record::repeats_partial(exchange, instance) {
return Err(malformed(REPEATED));
}
let params: Vec<Param<'_>> = if instance.is_complex() {
let measure = record::partial(exchange, instance, "MEASURE_WITH_UNIT")
.and_then(|r| record::exact_params(r, 2));
let own = record::partial(exchange, instance, "UNCERTAINTY_MEASURE_WITH_UNIT")
.and_then(|r| record::exact_params(r, 2));
measure.zip(own).map(|(mut a, b)| {
a.extend(b);
a
})
} else {
exchange
.records(instance)
.next()
.filter(|r| r.is("UNCERTAINTY_MEASURE_WITH_UNIT"))
.and_then(|r| record::exact_params(r, 4))
}
.ok_or_else(|| malformed("not an UNCERTAINTY_MEASURE_WITH_UNIT with four attributes"))?;
let [value, unit, name, description] = params.as_slice() else {
return Err(malformed(
"not an UNCERTAINTY_MEASURE_WITH_UNIT with four attributes",
));
};
let (raw, typed) =
measure_value(value).ok_or_else(|| malformed("the value is not a number"))?;
let unit = unit
.reference()
.ok_or_else(|| malformed("the unit is not a reference"))?;
let unit = unit_at(graph, unit, id, 0)?;
if let Some(found) = typed.map_err(malformed)? {
check(id, unit.kind, Some(Evidence::Kind(found)))?;
}
if instance.is_complex() {
for partial in exchange.records(instance) {
if partial.is("UNCERTAINTY_MEASURE_WITH_UNIT") {
continue;
}
match measure_subtype(&record::upper(partial.name())) {
Some(Subtype::Kind(found)) => check(id, unit.kind, Some(Evidence::Kind(found)))?,
Some(Subtype::Other) => {
return Err(malformed(
"the uncertainty measures something other than a length or an angle",
));
}
Some(Subtype::Neutral) | None => {}
}
}
}
let name = string(name).ok_or_else(|| malformed("the name is not a string"))?;
let description = match description {
Param::Unset => None,
other => Some(string(other).ok_or_else(|| malformed("the description is not a string"))?),
};
let value = raw * unit.factor;
if !(value.is_finite() && value > 0.0) {
return Err(UnitError::NotPositive {
instance: id,
value,
});
}
Ok(Uncertainty {
instance: id,
name,
description,
value,
unit,
})
}
#[cfg(test)]
mod tests {
use std::fmt::Write as _;
use super::*;
use crate::p21::parse;
fn graph(data: &str) -> Graph<'static> {
let src = format!(
"ISO-10303-21;HEADER;FILE_SCHEMA(('X'));ENDSEC;DATA;{data}ENDSEC;END-ISO-10303-21;"
);
let src: &'static [u8] = Box::leak(src.into_bytes().into_boxed_slice());
Graph::build(parse(src).unwrap())
}
const BASE: &str = "#1=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));\
#2=(NAMED_UNIT(*)PLANE_ANGLE_UNIT()SI_UNIT($,.RADIAN.));\
#3=(NAMED_UNIT(*)SI_UNIT($,.STERADIAN.)SOLID_ANGLE_UNIT());\
#4=DIMENSIONAL_EXPONENTS(1.,0.,0.,0.,0.,0.,0.);\
#5=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(25.4),#1);\
#6=(CONVERSION_BASED_UNIT('INCH',#5)LENGTH_UNIT()NAMED_UNIT(#4));\
#7=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.CENTI.,.METRE.));\
#8=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(2.54),#7);\
#9=(CONVERSION_BASED_UNIT('INCH',#8)LENGTH_UNIT()NAMED_UNIT(#4));\
#10=PLANE_ANGLE_MEASURE_WITH_UNIT(PLANE_ANGLE_MEASURE(0.0174532925),#2);\
#11=DIMENSIONAL_EXPONENTS(0.,0.,0.,0.,0.,0.,0.);\
#12=(CONVERSION_BASED_UNIT('DEGREE',#10)NAMED_UNIT(#11)PLANE_ANGLE_UNIT());\
#13=MEASURE_WITH_UNIT(LENGTH_MEASURE(12.),#6);\
#14=(CONVERSION_BASED_UNIT('FOOT',#13)LENGTH_UNIT()NAMED_UNIT(#4));\
#15=SI_UNIT(*,.MICRO.,.METRE.);\
#16=UNCERTAINTY_MEASURE_WITH_UNIT(LENGTH_MEASURE(1.E-03),#6,'distance_accuracy_value','');";
fn context(units: &str, uncertainty: &str, extra: &str) -> Graph<'static> {
graph(&format!(
"{BASE}#100=(GEOMETRIC_REPRESENTATION_CONTEXT(3)\
GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT(({uncertainty}))\
GLOBAL_UNIT_ASSIGNED_CONTEXT(({units}))REPRESENTATION_CONTEXT('',''));{extra}"
))
}
fn bits(unit: Result<Unit, UnitError>) -> u64 {
unit.unwrap().factor.to_bits()
}
#[test]
fn si_prefixes_come_from_an_exact_table() {
const PREFIXES: [(&str, i32); 16] = [
("EXA", 18),
("PETA", 15),
("TERA", 12),
("GIGA", 9),
("MEGA", 6),
("KILO", 3),
("HECTO", 2),
("DECA", 1),
("DECI", -1),
("CENTI", -2),
("MILLI", -3),
("MICRO", -6),
("NANO", -9),
("PICO", -12),
("FEMTO", -15),
("ATTO", -18),
];
let mut data = String::new();
for (i, (prefix, _)) in PREFIXES.iter().enumerate() {
write!(
data,
"#{}=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.{prefix}.,.METRE.));\
#{}=(NAMED_UNIT(*)PLANE_ANGLE_UNIT()SI_UNIT(.{prefix}.,.RADIAN.));",
100 + i,
200 + i
)
.unwrap();
}
let g = graph(&format!("{BASE}{data}"));
let power = |n: i32| format!("1e{n}").parse::<f64>().unwrap().to_bits();
for (i, (prefix, exponent)) in PREFIXES.iter().enumerate() {
let id = 100 + u64::try_from(i).unwrap();
let metre = unit(&g, id).unwrap();
assert_eq!(metre.factor.to_bits(), power(exponent + 3), "{prefix}METRE");
assert_eq!(metre.name, format!("{prefix}metre").to_ascii_lowercase());
assert_eq!(bits(unit(&g, id + 100)), power(*exponent), "{prefix}RADIAN");
}
assert_eq!(
bits(unit(&g, 1)),
1.0f64.to_bits(),
"a millimetre is exactly 1"
);
assert_eq!(bits(unit(&g, 7)), 10.0f64.to_bits());
assert_eq!(bits(unit(&g, 2)), 1.0f64.to_bits());
assert_eq!(bits(unit(&g, 3)), 1.0f64.to_bits());
let micrometre = unit(&g, 15).unwrap();
assert_eq!(
(micrometre.kind, micrometre.name.as_str()),
(UnitKind::Length, "micrometre"),
"a simple SI unit takes its kind from its name"
);
assert_eq!(micrometre.factor.to_bits(), 1e-3f64.to_bits());
}
#[test]
fn conversion_based_units_multiply_their_stated_factors() {
let g = graph(BASE);
let inch = unit(&g, 6).unwrap();
assert_eq!(
(inch.instance, inch.kind, inch.name.as_str()),
(6, UnitKind::Length, "inch")
);
assert_eq!(inch.factor.to_bits(), 25.4f64.to_bits());
assert_eq!(
bits(unit(&g, 9)),
(2.54 * 10.0f64).to_bits(),
"an inch of 2.54 centimetres is the f64 product"
);
let foot = unit(&g, 14).unwrap();
assert_eq!(foot.name, "foot");
assert_eq!(
foot.factor.to_bits(),
(12.0 * 25.4f64).to_bits(),
"a foot of 12 inches, through a plain MEASURE_WITH_UNIT"
);
let degree = unit(&g, 12).unwrap();
assert_eq!(
(degree.kind, degree.name.as_str()),
(UnitKind::PlaneAngle, "degree")
);
assert_eq!(
degree.factor.to_bits(),
0.017_453_292_5f64.to_bits(),
"used as written, never snapped to pi/180"
);
}
#[test]
fn kinds_come_from_the_base_and_neutral_types_are_accepted() {
let g = graph(&format!(
"{BASE}#20=MEASURE_WITH_UNIT(2.54,#7);\
#21=(CONVERSION_BASED_UNIT('inch',#20)NAMED_UNIT(#4));\
#22=MEASURE_WITH_UNIT(RATIO_MEASURE(0.0174532925),#2);\
#23=(CONVERSION_BASED_UNIT('degree',#22)NAMED_UNIT(*));\
#24=CONVERSION_BASED_UNIT(#4,'INCH',#5);"
));
let inch = unit(&g, 21).unwrap();
assert_eq!((inch.kind, inch.name.as_str()), (UnitKind::Length, "inch"));
assert_eq!(inch.factor.to_bits(), (2.54 * 10.0f64).to_bits());
assert_eq!(unit(&g, 23).unwrap().kind, UnitKind::PlaneAngle);
let simple = unit(&g, 24).unwrap();
assert_eq!(
(simple.kind, simple.factor.to_bits()),
(UnitKind::Length, 25.4f64.to_bits())
);
}
#[test]
fn positive_measures_are_lengths_and_angles() {
let g = graph(&format!(
"{BASE}#20=POSITIVE_LENGTH_MEASURE_WITH_UNIT(POSITIVE_LENGTH_MEASURE(25.4),#1);\
#21=(CONVERSION_BASED_UNIT('INCH',#20)LENGTH_UNIT()NAMED_UNIT(#4));\
#22=POSITIVE_PLANE_ANGLE_MEASURE_WITH_UNIT(\
POSITIVE_PLANE_ANGLE_MEASURE(0.0174532925),#2);\
#23=(CONVERSION_BASED_UNIT('DEGREE',#22)NAMED_UNIT(#11)PLANE_ANGLE_UNIT());\
#24=(LENGTH_MEASURE_WITH_UNIT()MEASURE_WITH_UNIT(POSITIVE_LENGTH_MEASURE(25.4),#1)\
POSITIVE_LENGTH_MEASURE_WITH_UNIT());\
#25=(CONVERSION_BASED_UNIT('INCH',#24)LENGTH_UNIT()NAMED_UNIT(#4));\
#26=POSITIVE_LENGTH_MEASURE_WITH_UNIT(POSITIVE_LENGTH_MEASURE(25.4),#2);\
#27=(CONVERSION_BASED_UNIT('X',#26)LENGTH_UNIT()NAMED_UNIT(#4));\
#100=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNIT_ASSIGNED_CONTEXT((#21,#23))\
REPRESENTATION_CONTEXT('',''));"
));
assert_eq!(bits(unit(&g, 21)), 25.4f64.to_bits());
assert_eq!(bits(unit(&g, 23)), 0.017_453_292_5f64.to_bits());
assert_eq!(bits(unit(&g, 25)), 25.4f64.to_bits(), "as a partial");
assert_eq!(
unit(&g, 27),
Err(UnitError::KindMismatch {
instance: 27,
declared: UnitKind::Length,
found: UnitKind::PlaneAngle
}),
"a positive length over a radian"
);
let units = ContextUnits::of_context(&g, 100).unwrap();
assert_eq!(units.length().unwrap().instance, 21);
assert_eq!(units.plane_angle().unwrap().instance, 23);
}
#[test]
fn names_are_labelled_as_info_labels_them() {
let g = graph(BASE);
let info = crate::info::Info::new(&g);
for (id, labels) in [
(1, &info.units.length),
(6, &info.units.length),
(7, &info.units.length),
(14, &info.units.length),
(2, &info.units.plane_angle),
(12, &info.units.plane_angle),
(3, &info.units.solid_angle),
] {
let name = unit(&g, id).unwrap().name;
assert!(labels.contains(&name), "#{id}: {name} not in {labels:?}");
}
}
#[test]
fn units_that_cannot_be_read_say_why() {
let g = graph(&format!(
"{BASE}#20=(CONTEXT_DEPENDENT_UNIT('PIXEL')LENGTH_UNIT()NAMED_UNIT(#4));\
#21=(MASS_UNIT()NAMED_UNIT(*)SI_UNIT(.KILO.,.GRAM.));\
#22=SI_UNIT(*,$,.SECOND.);\
#23=DERIVED_UNIT((#24));#24=DERIVED_UNIT_ELEMENT(#1,2.);\
#25=MASS_MEASURE_WITH_UNIT(MASS_MEASURE(1000.),#21);\
#26=(CONVERSION_BASED_UNIT('ton',#25)MASS_UNIT()NAMED_UNIT(#4));\
#40=(CONVERSION_BASED_UNIT('X',#998)LENGTH_UNIT()NAMED_UNIT(#4));\
#41=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(1.),#997);\
#42=(CONVERSION_BASED_UNIT('X',#41)LENGTH_UNIT()NAMED_UNIT(#4));\
#43=(NAMED_UNIT(#4)LENGTH_UNIT());"
));
let unsupported = |instance, entity: &str| {
Err(UnitError::Unsupported {
instance,
entity: entity.to_owned(),
})
};
assert_eq!(
unit(&g, 999),
Err(UnitError::Undefined { from: 999, to: 999 })
);
assert_eq!(
unit(&g, 40),
Err(UnitError::Undefined { from: 40, to: 998 })
);
assert_eq!(
unit(&g, 42),
Err(UnitError::Undefined { from: 41, to: 997 })
);
assert_eq!(
unit(&g, 4),
Err(UnitError::NotAUnit {
instance: 4,
entity: "DIMENSIONAL_EXPONENTS".to_owned()
})
);
assert_eq!(
unit(&g, 20),
unsupported(20, "CONTEXT_DEPENDENT_UNIT+LENGTH_UNIT+NAMED_UNIT")
);
assert_eq!(
unit(&g, 21),
unsupported(21, "MASS_UNIT+NAMED_UNIT+SI_UNIT")
);
assert_eq!(unit(&g, 22), unsupported(22, "SI_UNIT"), "an SI second");
assert_eq!(unit(&g, 23), unsupported(23, "DERIVED_UNIT"));
assert_eq!(
unit(&g, 26),
unsupported(26, "CONVERSION_BASED_UNIT+MASS_UNIT+NAMED_UNIT")
);
assert_eq!(
unit(&g, 43),
unsupported(43, "NAMED_UNIT+LENGTH_UNIT"),
"a named unit that states no factor"
);
}
#[test]
fn malformed_units_say_what_is_wrong() {
let g = graph(&format!(
"{BASE}#27=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.QUECTO.,.METRE.));\
#28=(CONVERSION_BASED_UNIT('X',#5)LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));\
#29=(LENGTH_UNIT()NAMED_UNIT(*)PLANE_ANGLE_UNIT()SI_UNIT(.MILLI.,.METRE.));\
#30=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.,$));\
#31=(CONVERSION_BASED_UNIT(#5,#5)LENGTH_UNIT()NAMED_UNIT(#4));\
#32=(CONVERSION_BASED_UNIT('X',25.4)LENGTH_UNIT()NAMED_UNIT(#4));\
#33=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(25.4),#1,$);\
#34=(CONVERSION_BASED_UNIT('X',#33)LENGTH_UNIT()NAMED_UNIT(#4));\
#35=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE('x'),#1);\
#36=(CONVERSION_BASED_UNIT('X',#35)LENGTH_UNIT()NAMED_UNIT(#4));\
#37=(CONVERSION_BASED_UNIT('X',#5)LENGTH_UNIT()NAMED_UNIT($));\
#38=DIMENSIONAL_EXPONENTS(0.,1.,0.,0.,0.,0.,0.);\
#39=(CONVERSION_BASED_UNIT('X',#5)LENGTH_UNIT()NAMED_UNIT(#38));\
#44=TIME_MEASURE_WITH_UNIT(TIME_MEASURE(1.),#1);\
#45=(CONVERSION_BASED_UNIT('X',#44)LENGTH_UNIT()NAMED_UNIT(#4));\
#46=LENGTH_MEASURE_WITH_UNIT(COUNT_MEASURE(1.),#1);\
#47=(CONVERSION_BASED_UNIT('X',#46)LENGTH_UNIT()NAMED_UNIT(#4));"
));
let malformed = |instance, reason| Err(UnitError::Malformed { instance, reason });
assert_eq!(unit(&g, 27), malformed(27, "unknown SI prefix"));
assert_eq!(
unit(&g, 28),
malformed(28, "both an SI unit and a conversion-based unit")
);
assert_eq!(unit(&g, 29), malformed(29, "declared as two kinds of unit"));
assert_eq!(
unit(&g, 30),
malformed(30, "SI_UNIT has the wrong number of attributes")
);
assert_eq!(
unit(&g, 31),
malformed(31, "the conversion-based unit's name is not a string")
);
assert_eq!(
unit(&g, 32),
malformed(32, "the conversion factor is not a reference")
);
assert_eq!(
unit(&g, 34),
malformed(33, "not a measure with unit with a value and a unit")
);
assert_eq!(unit(&g, 36), malformed(35, "the value is not a number"));
assert_eq!(
unit(&g, 37),
malformed(37, "named_unit.dimensions is neither * nor a reference")
);
assert_eq!(
unit(&g, 39),
malformed(
38,
"the dimensional exponents are those of neither a length nor an angle"
)
);
assert_eq!(
unit(&g, 45),
malformed(
44,
"the conversion factor measures something other than a length or an angle"
)
);
assert_eq!(
unit(&g, 47),
malformed(46, "the value is typed as a measure of another quantity")
);
}
#[test]
fn factors_must_be_positive_and_finite() {
let g = graph(&format!(
"{BASE}#20=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(0.),#1);\
#21=(CONVERSION_BASED_UNIT('ZERO',#20)LENGTH_UNIT()NAMED_UNIT(#4));\
#22=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(-25.4),#1);\
#23=(CONVERSION_BASED_UNIT('NEGATIVE',#22)LENGTH_UNIT()NAMED_UNIT(#4));\
#24=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(1.E999),#1);\
#25=(CONVERSION_BASED_UNIT('HUGE',#24)LENGTH_UNIT()NAMED_UNIT(#4));"
));
assert_eq!(
unit(&g, 21),
Err(UnitError::NotPositive {
instance: 20,
value: 0.0
})
);
assert_eq!(
unit(&g, 23),
Err(UnitError::NotPositive {
instance: 22,
value: -25.4
})
);
assert_eq!(
unit(&g, 25),
Err(UnitError::NotPositive {
instance: 24,
value: f64::INFINITY
})
);
}
#[test]
fn every_statement_of_a_kind_must_agree() {
use UnitKind::{Length, PlaneAngle};
let g = graph(&format!(
"{BASE}#20=(CONVERSION_BASED_UNIT('BAD',#10)LENGTH_UNIT()NAMED_UNIT(#4));\
#21=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT($,.RADIAN.));\
#22=PLANE_ANGLE_MEASURE_WITH_UNIT(PLANE_ANGLE_MEASURE(25.4),#1);\
#23=(CONVERSION_BASED_UNIT('X',#22)NAMED_UNIT(*));\
#24=LENGTH_MEASURE_WITH_UNIT(PLANE_ANGLE_MEASURE(25.4),#1);\
#25=(CONVERSION_BASED_UNIT('X',#24)LENGTH_UNIT()NAMED_UNIT(#4));\
#26=(CONVERSION_BASED_UNIT('X',#5)LENGTH_UNIT()NAMED_UNIT(#11));\
#27=(CONVERSION_BASED_UNIT('X',#10)NAMED_UNIT(#4)PLANE_ANGLE_UNIT());\
#28=(LENGTH_UNIT()NAMED_UNIT(#11)SI_UNIT(.MILLI.,.METRE.));\
#29=(LENGTH_MEASURE_WITH_UNIT()MEASURE_WITH_UNIT(PLANE_ANGLE_MEASURE(1.),#2));\
#30=(CONVERSION_BASED_UNIT('X',#29)NAMED_UNIT(*));"
));
let mismatch = |instance, declared, found| {
Err(UnitError::KindMismatch {
instance,
declared,
found,
})
};
assert_eq!(
unit(&g, 20),
mismatch(20, Length, PlaneAngle),
"a length in degrees"
);
assert_eq!(
unit(&g, 21),
mismatch(21, Length, PlaneAngle),
"a length in SI radians"
);
assert_eq!(
unit(&g, 23),
mismatch(23, Length, PlaneAngle),
"an angle measure over a millimetre"
);
assert_eq!(
unit(&g, 25),
mismatch(25, Length, PlaneAngle),
"a plane angle value as a length"
);
assert_eq!(
unit(&g, 26),
mismatch(26, Length, PlaneAngle),
"a length with an angle's exponents"
);
assert_eq!(
unit(&g, 27),
mismatch(27, PlaneAngle, Length),
"an angle with a length's exponents"
);
assert_eq!(
unit(&g, 28),
mismatch(28, Length, PlaneAngle),
"SI exponents"
);
assert_eq!(
unit(&g, 30),
mismatch(30, PlaneAngle, Length),
"a complex measure's subtype partial"
);
}
#[test]
fn conversion_chains_are_bounded() {
let chain = |n: u64| {
let mut links = String::new();
for i in 0..n {
let (unit, measure, base) = (
100 + 2 * i,
101 + 2 * i,
if i == 0 { 1 } else { 98 + 2 * i },
);
write!(
links,
"#{measure}=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(2.),#{base});\
#{unit}=(CONVERSION_BASED_UNIT('X',#{measure})LENGTH_UNIT()NAMED_UNIT(*));"
)
.unwrap();
}
let data = format!("#1=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));{links}");
(graph(&data), 98 + 2 * n)
};
let (g, top) = chain(8);
assert_eq!(
bits(unit(&g, top)),
256.0f64.to_bits(),
"8 conversions are read"
);
let (g, top) = chain(9);
assert!(
matches!(unit(&g, top), Err(UnitError::TooDeep { .. })),
"9 are not"
);
let g = graph(&format!(
"{BASE}#20=(CONVERSION_BASED_UNIT('LOOP',#21)LENGTH_UNIT()NAMED_UNIT(#4));\
#21=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(2.),#20);"
));
assert!(
matches!(unit(&g, 20), Err(UnitError::TooDeep { .. })),
"a cycle"
);
}
#[test]
fn a_context_reads_each_kind_on_its_own() {
let g = context("#6,#12,#3", "#16", "");
let units = ContextUnits::of_context(&g, 100).unwrap();
assert_eq!((units.context, units.dimension), (100, Some(3)));
assert_eq!(units.length().unwrap().instance, 6);
assert_eq!(units.plane_angle().unwrap().instance, 12);
assert_eq!(units.solid_angle().unwrap().instance, 3);
let [tolerance] = units.uncertainty().unwrap() else {
panic!("one uncertainty");
};
assert_eq!(
(
tolerance.instance,
tolerance.name.as_str(),
tolerance.description.as_deref()
),
(16, "distance_accuracy_value", Some(""))
);
assert_eq!(
tolerance.value.to_bits(),
(1e-3 * 25.4f64).to_bits(),
"converted with its own unit, the inch"
);
let g = context("#6,#12", "#16", "");
let units = ContextUnits::of_context(&g, 100).unwrap();
assert_eq!(
units.solid_angle(),
Err(UnitError::Missing {
context: 100,
kind: UnitKind::SolidAngle
})
);
assert!(
units.length().is_ok(),
"a missing solid angle unit blocks nothing else"
);
let g = context("#1,#6,#12", "#16", "");
let units = ContextUnits::of_context(&g, 100).unwrap();
assert_eq!(
units.length(),
Err(UnitError::Ambiguous {
context: 100,
kind: UnitKind::Length,
units: vec![1, 6]
})
);
assert!(units.plane_angle().is_ok());
let g = context(
"#1,#20,#12",
"#16",
"#20=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));",
);
let units = ContextUnits::of_context(&g, 100).unwrap();
assert_eq!(
units.length().unwrap().instance,
1,
"units of one kind with bit-equal factors agree"
);
}
#[test]
fn a_context_refuses_rather_than_assumes() {
let extra = "#20=(CONTEXT_DEPENDENT_UNIT('PIXEL')LENGTH_UNIT()NAMED_UNIT(#4));\
#21=(MASS_UNIT()NAMED_UNIT(*)SI_UNIT(.KILO.,.GRAM.));";
let g = context("#1,#20,#12", "#16", extra);
let units = ContextUnits::of_context(&g, 100).unwrap();
assert!(
matches!(
units.length(),
Err(UnitError::Unreadable {
context: 100,
unit: 20,
..
})
),
"an unreadable length unit is not made up for by a readable one: {:?}",
units.length()
);
assert!(units.plane_angle().is_ok());
let g = context("#1,#21,#12", "#16", extra);
let units = ContextUnits::of_context(&g, 100).unwrap();
assert_eq!(
units.length().unwrap().instance,
1,
"a mass unit is not a length unit"
);
for (listed, reason) in [
(
"#1,#4,#12",
UnitError::NotAUnit {
instance: 4,
entity: "DIMENSIONAL_EXPONENTS".to_owned(),
},
),
("#1,#999,#12", UnitError::Undefined { from: 100, to: 999 }),
] {
let g = context(listed, "#16", "");
let units = ContextUnits::of_context(&g, 100).unwrap();
let unit = if listed.contains("#4") { 4 } else { 999 };
for kind in UnitKind::ALL {
assert_eq!(
units.unit(kind),
Err(UnitError::Unreadable {
context: 100,
unit,
reason: Box::new(reason.clone())
}),
"{listed}"
);
}
}
}
#[test]
fn malformed_sets_are_never_empty() {
for units in ["$", "", "#1,'x'"] {
let g = graph(&format!(
"{BASE}#100=(GEOMETRIC_REPRESENTATION_CONTEXT(3)\
GLOBAL_UNIT_ASSIGNED_CONTEXT({}) REPRESENTATION_CONTEXT('',''));",
if units == "$" {
"$".to_owned()
} else {
format!("({units})")
}
));
let read = ContextUnits::of_context(&g, 100).unwrap();
for kind in UnitKind::ALL {
assert_eq!(
read.unit(kind),
Err(UnitError::Malformed {
instance: 100,
reason: "the unit set is not a non-empty set of references"
}),
"{units:?}"
);
}
assert_eq!(read.uncertainty(), Ok(&[][..]), "none stated");
}
let g = context("#1,#2", "$", "");
let units = ContextUnits::of_context(&g, 100).unwrap();
assert_eq!(
units.uncertainty(),
Err(UnitError::Malformed {
instance: 100,
reason: "the uncertainty set is not a non-empty set of references"
})
);
assert!(units.length().is_ok());
}
#[test]
fn uncertainties_use_their_own_unit() {
let g = context(
"#6,#12",
"#20,#21",
"#20=UNCERTAINTY_MEASURE_WITH_UNIT(LENGTH_MEASURE(0.01),#1,'closure',$);\
#21=UNCERTAINTY_MEASURE_WITH_UNIT(PLANE_ANGLE_MEASURE(0.5),#12,'angle','x');",
);
let units = ContextUnits::of_context(&g, 100).unwrap();
let [mm, angle] = units.uncertainty().unwrap() else {
panic!("two uncertainties");
};
assert_eq!(
(mm.value.to_bits(), mm.description.as_deref()),
(0.01f64.to_bits(), None),
"in millimetres although the context is in inches"
);
assert_eq!(angle.value.to_bits(), (0.5 * 0.017_453_292_5f64).to_bits());
assert_eq!(angle.unit.kind, UnitKind::PlaneAngle);
for (uncertainty, expected) in [
(
"#20=UNCERTAINTY_MEASURE_WITH_UNIT(LENGTH_MEASURE(-1.),#1,'closure','');",
UnitError::NotPositive {
instance: 20,
value: -1.0,
},
),
(
"#20=UNCERTAINTY_MEASURE_WITH_UNIT(PLANE_ANGLE_MEASURE(1.),#1,'closure','');",
UnitError::KindMismatch {
instance: 20,
declared: UnitKind::Length,
found: UnitKind::PlaneAngle,
},
),
(
"#20=UNCERTAINTY_MEASURE_WITH_UNIT(LENGTH_MEASURE(1.),#1,$,'');",
UnitError::Malformed {
instance: 20,
reason: "the name is not a string",
},
),
(
"#20=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(1.),#1);",
UnitError::Malformed {
instance: 20,
reason: "not an UNCERTAINTY_MEASURE_WITH_UNIT with four attributes",
},
),
] {
let g = context("#1,#12", "#20", uncertainty);
let units = ContextUnits::of_context(&g, 100).unwrap();
assert_eq!(units.uncertainty(), Err(expected));
assert!(units.length().is_ok(), "an uncertainty blocks no unit");
}
let g = graph(&format!(
"{BASE}#100=(GEOMETRIC_REPRESENTATION_CONTEXT(3)\
GLOBAL_UNIT_ASSIGNED_CONTEXT((#1,#2))REPRESENTATION_CONTEXT('',''));"
));
assert_eq!(
ContextUnits::of_context(&g, 100).unwrap().uncertainty(),
Ok(&[][..]),
"empty only if none is stated"
);
}
#[test]
fn complex_uncertainties_agree_with_their_measure_type() {
for (uncertainty, expected) in [
(
"#20=(LENGTH_MEASURE_WITH_UNIT()MEASURE_WITH_UNIT(0.5,#12)\
UNCERTAINTY_MEASURE_WITH_UNIT('closure',''));",
UnitError::KindMismatch {
instance: 20,
declared: UnitKind::PlaneAngle,
found: UnitKind::Length,
},
),
(
"#20=(MEASURE_WITH_UNIT(0.5,#1)PLANE_ANGLE_MEASURE_WITH_UNIT()\
UNCERTAINTY_MEASURE_WITH_UNIT('closure',''));",
UnitError::KindMismatch {
instance: 20,
declared: UnitKind::Length,
found: UnitKind::PlaneAngle,
},
),
(
"#20=(MASS_MEASURE_WITH_UNIT()MEASURE_WITH_UNIT(0.5,#1)\
UNCERTAINTY_MEASURE_WITH_UNIT('closure',''));",
UnitError::Malformed {
instance: 20,
reason: "the uncertainty measures something other than a length or an angle",
},
),
] {
let g = context("#1,#12", "#20", uncertainty);
let units = ContextUnits::of_context(&g, 100).unwrap();
assert_eq!(units.uncertainty(), Err(expected), "{uncertainty}");
}
for subtypes in [
"LENGTH_MEASURE_WITH_UNIT()",
"LENGTH_MEASURE_WITH_UNIT()POSITIVE_LENGTH_MEASURE_WITH_UNIT()",
] {
let g = context(
"#1,#12",
"#20",
&format!(
"#20=({subtypes}MEASURE_WITH_UNIT(LENGTH_MEASURE(0.01),#1)\
UNCERTAINTY_MEASURE_WITH_UNIT('closure',''));"
),
);
let units = ContextUnits::of_context(&g, 100).unwrap();
let [closure] = units.uncertainty().unwrap() else {
panic!("one uncertainty");
};
assert_eq!(
(closure.value.to_bits(), closure.unit.kind),
(0.01f64.to_bits(), UnitKind::Length),
"{subtypes}"
);
}
}
#[test]
fn contexts_simple_parametric_and_malformed() {
let g = graph(&format!(
"{BASE}#100=GLOBAL_UNIT_ASSIGNED_CONTEXT('','',(#1,#2));\
#101=(GEOMETRIC_REPRESENTATION_CONTEXT(2)PARAMETRIC_REPRESENTATION_CONTEXT()\
REPRESENTATION_CONTEXT('2D SPACE',''));\
#102=(GEOMETRIC_REPRESENTATION_CONTEXT(0)REPRESENTATION_CONTEXT('',''));\
#103=(GEOMETRIC_REPRESENTATION_CONTEXT(3.)REPRESENTATION_CONTEXT('',''));\
#104=GEOMETRIC_REPRESENTATION_CONTEXT('','',3);\
#105=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNIT_ASSIGNED_CONTEXT((#1),$)\
REPRESENTATION_CONTEXT('',''));\
#106=SHAPE_REPRESENTATION('',(),#101);"
));
let simple = ContextUnits::of_context(&g, 100).unwrap();
assert_eq!(simple.length().unwrap().instance, 1);
assert_eq!(simple.dimension, None);
let parametric = ContextUnits::of_context(&g, 101).unwrap();
assert_eq!(parametric.dimension, Some(2));
assert_eq!(
parametric.length(),
Err(UnitError::Missing {
context: 101,
kind: UnitKind::Length
})
);
assert_eq!(
ContextUnits::of_context(&g, 104).unwrap().dimension,
Some(3),
"a simple geometric context"
);
for id in [102, 103] {
assert_eq!(
ContextUnits::of_context(&g, id),
Err(UnitError::Malformed {
instance: id,
reason: "coordinate_space_dimension is not a positive integer"
})
);
}
assert_eq!(
ContextUnits::of_context(&g, 105).unwrap().length(),
Err(UnitError::Malformed {
instance: 105,
reason: "GLOBAL_UNIT_ASSIGNED_CONTEXT has the wrong number of attributes"
})
);
assert_eq!(
ContextUnits::of_context(&g, 106),
Err(UnitError::NotAContext {
instance: 106,
entity: "SHAPE_REPRESENTATION".to_owned()
})
);
assert_eq!(
ContextUnits::of_context(&g, 999),
Err(UnitError::Undefined { from: 999, to: 999 })
);
}
#[test]
fn representations_name_their_context() {
let g = context(
"#1,#2",
"#16",
"#200=ADVANCED_BREP_SHAPE_REPRESENTATION('',(#201),#100);\
#201=AXIS2_PLACEMENT_3D('',#202,$,$);#202=CARTESIAN_POINT('',(0.,0.,0.));\
#210=(DRAUGHTING_MODEL()REPRESENTATION('',(#201),#100)SHAPE_REPRESENTATION());\
#220=SHAPE_DEFINITION_REPRESENTATION(#221,#200);\
#230=SHAPE_REPRESENTATION('',(#201),#555);\
#240=SHAPE_REPRESENTATION('',(#201),#202);",
);
assert_eq!(representation_context(&g, 200), Ok(100));
assert_eq!(representation_context(&g, 210), Ok(100));
assert_eq!(
ContextUnits::of_representation(&g, 210)
.unwrap()
.length()
.unwrap()
.instance,
1
);
assert_eq!(
representation_context(&g, 220),
Err(UnitError::NotARepresentation {
instance: 220,
entity: "SHAPE_DEFINITION_REPRESENTATION".to_owned()
})
);
assert_eq!(
representation_context(&g, 230),
Err(UnitError::Undefined { from: 230, to: 555 })
);
assert_eq!(
representation_context(&g, 240),
Err(UnitError::NotAContext {
instance: 202,
entity: "CARTESIAN_POINT".to_owned()
})
);
}
fn solid(units: &str, extra: &str) -> Graph<'static> {
context(
units,
"#16",
&format!(
"#200=ADVANCED_BREP_SHAPE_REPRESENTATION('',(#201,#205),#100);\
#201=MANIFOLD_SOLID_BREP('',#202);#202=CLOSED_SHELL('',(#203));\
#203=ADVANCED_FACE('',(),#204,.T.);#204=PLANE('',#205);\
#205=AXIS2_PLACEMENT_3D('',#206,$,$);#206=CARTESIAN_POINT('',(0.,0.,0.));\
#101=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#16))\
GLOBAL_UNIT_ASSIGNED_CONTEXT((#1,#12))REPRESENTATION_CONTEXT('',''));\
#102=(GEOMETRIC_REPRESENTATION_CONTEXT(2)GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#16))\
GLOBAL_UNIT_ASSIGNED_CONTEXT((#6,#12))REPRESENTATION_CONTEXT('',''));\
{extra}"
),
)
}
#[test]
fn items_find_their_context_through_their_containers() {
let g = solid("#6,#12", "#300=ADVANCED_FACE('',(),#204,.T.);");
for item in [201, 202, 203, 205] {
let units = ContextUnits::of_item(&g, item);
assert_eq!(
units.map(|u| (u.context, u.length().unwrap().factor.to_bits())),
Ok((100, 25.4f64.to_bits())),
"#{item}"
);
}
assert_eq!(
ContextUnits::of_item(&g, 300),
Err(UnitError::NotHeld { item: 300 }),
"an orphan face"
);
assert_eq!(
ContextUnits::of_item(&g, 200),
Err(UnitError::NotHeld { item: 200 }),
"a representation is not an item: read it with of_representation"
);
assert_eq!(
ContextUnits::of_item(&g, 204),
Err(UnitError::NotHeld { item: 204 }),
"a surface is not followed up through its face"
);
assert_eq!(
ContextUnits::of_item(&g, 999),
Err(UnitError::Undefined { from: 999, to: 999 })
);
}
#[test]
fn void_faces_and_surface_models_are_held() {
let g = context(
"#6,#12",
"#16",
"#200=ADVANCED_BREP_SHAPE_REPRESENTATION('',(#201),#100);\
#201=BREP_WITH_VOIDS('',#202,(#210));#202=CLOSED_SHELL('',(#203));\
#203=ADVANCED_FACE('',(),#999,.T.);\
#210=ORIENTED_CLOSED_SHELL('',*,#211,.F.);#211=CLOSED_SHELL('',(#212));\
#212=ADVANCED_FACE('',(),#999,.T.);\
#300=MANIFOLD_SURFACE_SHAPE_REPRESENTATION('',(#301),#100);\
#301=SHELL_BASED_SURFACE_MODEL('',(#302));#302=OPEN_SHELL('',(#303));\
#303=ADVANCED_FACE('',(),#999,.T.);",
);
for item in [212, 211, 210, 303, 302, 301] {
assert_eq!(
ContextUnits::of_item(&g, item).map(|u| u.context),
Ok(100),
"#{item}"
);
}
}
#[test]
fn every_holder_must_be_readable_and_agree() {
let g = solid(
"#1,#12",
"#210=TESSELLATED_SHAPE_REPRESENTATION_WITH_ACCURACY_PARAMETERS('',(#201),#103,(0.1));\
#103=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#16))\
GLOBAL_UNIT_ASSIGNED_CONTEXT((#6,#12))REPRESENTATION_CONTEXT('',''));",
);
let units = ContextUnits::of_item(&g, 203).unwrap();
assert_eq!(
units.length(),
Err(UnitError::ContextsDisagree {
item: 203,
kind: Some(UnitKind::Length),
contexts: [100, 103]
})
);
assert!(
units.plane_angle().is_ok(),
"the kinds that agree still read"
);
let g = solid("#1,#12", "#210=SHAPE_REPRESENTATION('',(#201),#555);");
assert_eq!(
ContextUnits::of_item(&g, 203),
Err(UnitError::HolderUnreadable {
item: 203,
holder: 210,
reason: Box::new(UnitError::Undefined { from: 210, to: 555 })
})
);
let g = solid(
"#1,#12",
"#210=SHAPE_REPRESENTATION('',(#201),#104);\
#104=(GEOMETRIC_REPRESENTATION_CONTEXT(0)REPRESENTATION_CONTEXT('',''));",
);
assert!(matches!(
ContextUnits::of_item(&g, 203),
Err(UnitError::HolderUnreadable { holder: 210, .. })
));
let g = solid(
"#1,#12",
"#210=(REPRESENTATION('',(#201),#100,$)SHAPE_REPRESENTATION());",
);
assert!(matches!(
ContextUnits::of_item(&g, 203),
Err(UnitError::HolderUnreadable { holder: 210, .. })
));
}
#[test]
fn holders_agree_kind_by_kind() {
let g = solid("#1,#12", "#210=SHAPE_REPRESENTATION('',(#201),#102);");
assert_eq!(
ContextUnits::of_item(&g, 203),
Err(UnitError::ContextsDisagree {
item: 203,
kind: None,
contexts: [100, 102]
})
);
let g = solid("#1,#12", "#210=SHAPE_REPRESENTATION('',(#201),#101);");
let units = ContextUnits::of_item(&g, 203).unwrap();
assert_eq!(units.context, 100);
assert_eq!(units.length().unwrap().instance, 1);
let g = graph(&format!(
"{BASE}#21=UNCERTAINTY_MEASURE_WITH_UNIT(LENGTH_MEASURE(-1.),#1,'closure','');\
#100=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#21))\
GLOBAL_UNIT_ASSIGNED_CONTEXT((#1,#2))REPRESENTATION_CONTEXT('',''));\
#101=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#21))\
GLOBAL_UNIT_ASSIGNED_CONTEXT((#1,#2))REPRESENTATION_CONTEXT('',''));\
#200=ADVANCED_BREP_SHAPE_REPRESENTATION('',(#201),#100);\
#210=SHAPE_REPRESENTATION('',(#201),#101);\
#201=MANIFOLD_SOLID_BREP('',#202);#202=CLOSED_SHELL('',(#203));\
#203=ADVANCED_FACE('',(),#999,.T.);"
));
let units = ContextUnits::of_item(&g, 203).unwrap();
assert_eq!(units.length().unwrap().factor.to_bits(), 1.0f64.to_bits());
assert_eq!(
units.uncertainty(),
Err(UnitError::NotPositive {
instance: 21,
value: -1.0
})
);
let g = solid(
"#1,#12",
"#210=SHAPE_REPRESENTATION('',(#201),#103);\
#103=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#22))\
GLOBAL_UNIT_ASSIGNED_CONTEXT((#1))REPRESENTATION_CONTEXT('',''));\
#22=UNCERTAINTY_MEASURE_WITH_UNIT(LENGTH_MEASURE(0.1),#1,'closure','');",
);
let units = ContextUnits::of_item(&g, 203).unwrap();
assert!(units.length().is_ok());
assert_eq!(
units.uncertainty(),
Err(UnitError::ContextsDisagree {
item: 203,
kind: Some(UnitKind::Length),
contexts: [100, 103]
})
);
assert_eq!(
units.plane_angle(),
Err(UnitError::Missing {
context: 103,
kind: UnitKind::PlaneAngle
})
);
}
#[test]
fn malformed_containers_and_sets_that_are_not_representations() {
let g = solid("#1,#12", "#210=CLOSED_SHELL('',(#203),$);");
assert_eq!(
ContextUnits::of_item(&g, 203),
Err(UnitError::Malformed {
instance: 210,
reason: "a face, shell, solid or surface model with the wrong number of attributes"
})
);
let g = solid("#1,#12", "#210=GEOMETRIC_SET('',(#203));");
assert_eq!(ContextUnits::of_item(&g, 203).map(|u| u.context), Ok(100));
}
const INCH: &str = "#103=(GEOMETRIC_REPRESENTATION_CONTEXT(3)\
GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#16))GLOBAL_UNIT_ASSIGNED_CONTEXT((#6,#12))\
REPRESENTATION_CONTEXT('',''));";
#[test]
fn holders_behind_containers_stepq_does_not_follow_are_not_skipped() {
let not_followed = |item, container, entity: &str| {
Err(UnitError::NotFollowed {
item,
container,
entity: entity.to_owned(),
})
};
let g = solid(
"#1,#12",
&format!(
"{INCH}#312=(CLOSED_SHELL()CONNECTED_FACE_SET((#203))REPRESENTATION_ITEM('')\
TOPOLOGICAL_REPRESENTATION_ITEM());#311=MANIFOLD_SOLID_BREP('',#312);\
#310=ADVANCED_BREP_SHAPE_REPRESENTATION('',(#311),#103);"
),
);
assert_eq!(
ContextUnits::of_item(&g, 203),
not_followed(
203,
312,
"CLOSED_SHELL+CONNECTED_FACE_SET+REPRESENTATION_ITEM+TOPOLOGICAL_REPRESENTATION_ITEM"
)
);
assert_eq!(
ContextUnits::of_item(&g, 312).map(|u| u.length().unwrap().factor.to_bits()),
Ok(25.4f64.to_bits()),
"the complex shell itself is held by the inch solid"
);
let g = graph(&format!(
"{BASE}{INCH}#203=ADVANCED_FACE('',(),#999,.T.);\
#312=(CLOSED_SHELL()CONNECTED_FACE_SET((#203))REPRESENTATION_ITEM('')\
TOPOLOGICAL_REPRESENTATION_ITEM());#311=MANIFOLD_SOLID_BREP('',#312);\
#310=ADVANCED_BREP_SHAPE_REPRESENTATION('',(#311),#103);"
));
assert!(matches!(
ContextUnits::of_item(&g, 203),
Err(UnitError::NotFollowed { container: 312, .. })
));
let g = solid(
"#1,#12",
&format!(
"{INCH}#311=(GEOMETRIC_REPRESENTATION_ITEM()MANIFOLD_SOLID_BREP(#202)\
REPRESENTATION_ITEM('')SOLID_MODEL());\
#310=ADVANCED_BREP_SHAPE_REPRESENTATION('',(#311),#103);"
),
);
for item in [203, 202] {
assert_eq!(
ContextUnits::of_item(&g, item),
not_followed(
item,
311,
"GEOMETRIC_REPRESENTATION_ITEM+MANIFOLD_SOLID_BREP+REPRESENTATION_ITEM+SOLID_MODEL"
),
"#{item}"
);
}
let g = solid(
"#1,#12",
&format!(
"{INCH}#313=ORIENTED_CLOSED_SHELL('',*,#202,.F.);#314=CLOSED_SHELL('',());\
#311=(BREP_WITH_VOIDS((#313))GEOMETRIC_REPRESENTATION_ITEM()\
MANIFOLD_SOLID_BREP(#314)REPRESENTATION_ITEM('')SOLID_MODEL());\
#310=ADVANCED_BREP_SHAPE_REPRESENTATION('',(#311),#103);"
),
);
assert!(matches!(
ContextUnits::of_item(&g, 203),
Err(UnitError::NotFollowed { container: 311, .. })
));
let g = solid("#1,#12", "#310=DRAUGHTING_MODEL('',(#201),#5);");
for item in [203, 201] {
assert_eq!(
ContextUnits::of_item(&g, item),
not_followed(item, 310, "DRAUGHTING_MODEL"),
"#{item}"
);
}
}
#[test]
fn oriented_faces_and_face_subsets_are_followed() {
let disagree = Err(UnitError::ContextsDisagree {
item: 203,
kind: Some(UnitKind::Length),
contexts: [100, 103],
});
let g = solid(
"#1,#12",
&format!(
"{INCH}#313=ORIENTED_FACE('',*,#203,.F.);#314=OPEN_SHELL('',(#313));\
#315=SHELL_BASED_SURFACE_MODEL('',(#314));\
#310=MANIFOLD_SURFACE_SHAPE_REPRESENTATION('',(#315),#103);"
),
);
let units = ContextUnits::of_item(&g, 203).unwrap();
assert_eq!(units.length(), disagree.clone());
assert!(units.plane_angle().is_ok());
assert_eq!(
ContextUnits::of_item(&g, 313).map(|u| u.length().unwrap().instance),
Ok(6),
"the oriented face is in inches"
);
let g = solid(
"#1,#12",
&format!(
"{INCH}#313=CONNECTED_FACE_SUB_SET('',(#203),#202);\
#315=FACE_BASED_SURFACE_MODEL('',(#313));\
#310=MANIFOLD_SURFACE_SHAPE_REPRESENTATION('',(#315),#103);"
),
);
assert_eq!(ContextUnits::of_item(&g, 203).unwrap().length(), disagree);
assert_eq!(
ContextUnits::of_item(&g, 202).map(|u| u.length().unwrap().instance),
Ok(1),
"the parent shell is held only by the millimetre solid"
);
}
#[test]
fn styles_and_layers_hold_nothing() {
let g = solid(
"#1,#12",
"#320=STYLED_ITEM('',(#321),#203);#321=PRESENTATION_STYLE_ASSIGNMENT(());\
#322=OVER_RIDING_STYLED_ITEM('',(#321),#203,#320);\
#323=PRESENTATION_LAYER_ASSIGNMENT('layer','',(#203,#201));\
#324=STYLED_ITEM('',(#321),#201);",
);
for item in [203, 201] {
assert_eq!(
ContextUnits::of_item(&g, item).map(|u| (u.context, u.length().unwrap().instance)),
Ok((100, 1)),
"#{item}"
);
}
}
#[test]
fn repeated_partials_are_refused_not_picked() {
let g = graph(&format!(
"{BASE}#20=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.)SI_UNIT(.CENTI.,.METRE.));\
#21=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.CENTI.,.METRE.)SI_UNIT(.MILLI.,.METRE.));\
#22=(CONVERSION_BASED_UNIT('INCH',#5)CONVERSION_BASED_UNIT('FOOT',#13)\
LENGTH_UNIT()NAMED_UNIT(#4));\
#23=(LENGTH_MEASURE_WITH_UNIT()MEASURE_WITH_UNIT(LENGTH_MEASURE(25.4),#1)\
MEASURE_WITH_UNIT(LENGTH_MEASURE(1.),#1));\
#24=(CONVERSION_BASED_UNIT('INCH',#23)LENGTH_UNIT()NAMED_UNIT(#4));\
#25=(LENGTH_UNIT()NAMED_UNIT(#4)NAMED_UNIT(#11)SI_UNIT(.MILLI.,.METRE.));\
#29=(NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.)SI_UNIT($,.RADIAN.));"
));
let repeated = |instance| {
Err(UnitError::Malformed {
instance,
reason: REPEATED,
})
};
assert_eq!(unit(&g, 20), repeated(20), "SI_UNIT twice");
assert_eq!(unit(&g, 21), repeated(21), "in either order");
assert_eq!(unit(&g, 22), repeated(22), "CONVERSION_BASED_UNIT twice");
assert_eq!(
unit(&g, 24),
repeated(23),
"a factor's MEASURE_WITH_UNIT twice"
);
assert_eq!(unit(&g, 25), repeated(25), "NAMED_UNIT twice");
let g = context(
"#1,#2,#29",
"#16",
"#29=(NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.)SI_UNIT($,.RADIAN.));",
);
let units = ContextUnits::of_context(&g, 100).unwrap();
for kind in UnitKind::ALL {
assert_eq!(
units.unit(kind),
Err(UnitError::Unreadable {
context: 100,
unit: 29,
reason: Box::new(UnitError::Malformed {
instance: 29,
reason: REPEATED
})
}),
"{kind}"
);
}
for partials in [
"GLOBAL_UNIT_ASSIGNED_CONTEXT((#1,#2))GLOBAL_UNIT_ASSIGNED_CONTEXT((#6,#2))",
"GEOMETRIC_REPRESENTATION_CONTEXT(2)GLOBAL_UNIT_ASSIGNED_CONTEXT((#1,#2))",
"GLOBAL_UNIT_ASSIGNED_CONTEXT((#1,#2))GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#16))\
GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT($)",
] {
let g = graph(&format!(
"{BASE}#100=(GEOMETRIC_REPRESENTATION_CONTEXT(3){partials}\
REPRESENTATION_CONTEXT('',''));"
));
assert_eq!(
ContextUnits::of_context(&g, 100),
Err(UnitError::Malformed {
instance: 100,
reason: REPEATED
}),
"{partials}"
);
}
}
#[test]
fn repeated_partials_in_holders_and_uncertainties_are_refused() {
let g = solid(
"#1,#12",
&format!(
"{INCH}#210=(REPRESENTATION('',(#201),#100)REPRESENTATION('',(#201),#103)\
SHAPE_REPRESENTATION());"
),
);
let not_a_representation = UnitError::NotARepresentation {
instance: 210,
entity: "REPRESENTATION+REPRESENTATION+SHAPE_REPRESENTATION".to_owned(),
};
assert_eq!(
representation_context(&g, 210),
Err(not_a_representation.clone())
);
assert_eq!(
ContextUnits::of_item(&g, 203),
Err(UnitError::HolderUnreadable {
item: 203,
holder: 210,
reason: Box::new(not_a_representation)
})
);
let g = context(
"#1,#12",
"#20",
"#20=(LENGTH_MEASURE_WITH_UNIT()MEASURE_WITH_UNIT(LENGTH_MEASURE(0.01),#1)\
UNCERTAINTY_MEASURE_WITH_UNIT('closure','')UNCERTAINTY_MEASURE_WITH_UNIT('x',''));",
);
assert_eq!(
ContextUnits::of_context(&g, 100).unwrap().uncertainty(),
Err(UnitError::Malformed {
instance: 20,
reason: REPEATED
})
);
}
#[test]
fn listed_entries_that_are_no_length_or_angle_unit_refuse_every_kind() {
let g = graph(&format!(
"{BASE}#20=GEOMETRIC_TOLERANCE_WITH_DEFINED_UNIT('','',#5,#999,#5);\
#21=(NAMED_UNIT(*)SI_UNIT(.MILLI.,.METER.));#22=SI_UNIT(*,.MILLI.,.METER.);\
#23=(MASS_UNIT()NAMED_UNIT(*)SI_UNIT(.CENTI.,.METRE.));\
#24=(CONVERSION_BASED_UNIT('INCH',#5)MASS_UNIT()NAMED_UNIT(#4));\
#25=MEASURE_WITH_UNIT(25.4,#1);\
#26=(CONVERSION_BASED_UNIT('X',#25)MASS_UNIT()NAMED_UNIT(*));"
));
for (id, entity) in [
(5, "LENGTH_MEASURE_WITH_UNIT"),
(16, "UNCERTAINTY_MEASURE_WITH_UNIT"),
(20, "GEOMETRIC_TOLERANCE_WITH_DEFINED_UNIT"),
] {
assert_eq!(
unit(&g, id),
Err(UnitError::NotAUnit {
instance: id,
entity: entity.to_owned()
}),
"a measure is no unit"
);
}
for id in [21, 22] {
assert_eq!(
unit(&g, id),
Err(UnitError::Malformed {
instance: id,
reason: "the SI unit name is not an si_unit_name"
}),
"#{id}"
);
}
let extra = "#20=GEOMETRIC_TOLERANCE_WITH_DEFINED_UNIT('','',#5,#999,#5);\
#21=(NAMED_UNIT(*)SI_UNIT(.MILLI.,.METER.));#22=SI_UNIT(*,.MILLI.,.METER.);\
#23=(MASS_UNIT()NAMED_UNIT(*)SI_UNIT(.CENTI.,.METRE.));\
#24=(CONVERSION_BASED_UNIT('INCH',#5)MASS_UNIT()NAMED_UNIT(#4));\
#25=MEASURE_WITH_UNIT(25.4,#1);\
#26=(CONVERSION_BASED_UNIT('X',#25)MASS_UNIT()NAMED_UNIT(*));";
for listed in [5, 16, 20, 21, 22, 23, 24, 26] {
let g = context(&format!("#6,#{listed},#12"), "#16", extra);
let units = ContextUnits::of_context(&g, 100).unwrap();
for kind in UnitKind::ALL {
assert!(
matches!(
units.unit(kind),
Err(UnitError::Unreadable { unit, .. }) if unit == listed
),
"#{listed}, {kind}: {:?}",
units.unit(kind)
);
}
}
let g = context(
"#6,#21,#22,#26,#12",
"#16",
"#21=(MASS_UNIT()NAMED_UNIT(*)SI_UNIT(.KILO.,.GRAM.));#22=SI_UNIT(*,$,.SECOND.);\
#25=MASS_MEASURE_WITH_UNIT(MASS_MEASURE(1000.),#21);\
#26=(CONVERSION_BASED_UNIT('ton',#25)MASS_UNIT()NAMED_UNIT(*));",
);
let units = ContextUnits::of_context(&g, 100).unwrap();
assert_eq!(units.length().unwrap().instance, 6);
assert_eq!(units.plane_angle().unwrap().instance, 12);
}
#[test]
fn of_item_reads_each_holder_once() {
const N: usize = 20_000;
let started = std::time::Instant::now();
let mut data = format!("{BASE}#5000000=ADVANCED_FACE('',(),#999,.T.);");
for i in 0..N {
let (rep, ctx) = (100_000 + 2 * i, 100_001 + 2 * i);
write!(
data,
"#{ctx}=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNIT_ASSIGNED_CONTEXT((#1,#2))\
REPRESENTATION_CONTEXT('',''));#{rep}=SHAPE_REPRESENTATION('',(#5000000),#{ctx});"
)
.unwrap();
}
let mut shells = String::new();
for i in 0..N {
let shell = 200_000 + i;
write!(data, "#{shell}=OPEN_SHELL('',(#5000001));").unwrap();
write!(shells, "{}#{shell}", if i == 0 { "" } else { "," }).unwrap();
}
write!(
data,
"#5000001=ADVANCED_FACE('',(),#999,.T.);\
#5000002=SHELL_BASED_SURFACE_MODEL('',({shells}));\
#5000003=MANIFOLD_SURFACE_SHAPE_REPRESENTATION('',(#5000002),#100001);\
#5000004=SHAPE_REPRESENTATION('',({shells}),#100001);"
)
.unwrap();
let g = graph(&data);
for face in [5_000_000, 5_000_001] {
assert_eq!(
ContextUnits::of_item(&g, face).map(|u| u.length().unwrap().instance),
Ok(1),
"#{face}"
);
}
let elapsed = started.elapsed();
assert!(elapsed < std::time::Duration::from_secs(20), "{elapsed:?}");
}
#[test]
fn messages_name_the_instances() {
assert_eq!(
UnitError::Undefined { from: 5, to: 5 }.to_string(),
"#5 is not defined"
);
assert_eq!(
UnitError::Undefined { from: 7, to: 5 }.to_string(),
"#7 refers to #5, which is not defined"
);
assert_eq!(
UnitError::Ambiguous {
context: 1,
kind: UnitKind::Length,
units: vec![2, 3]
}
.to_string(),
"context #1 assigns length units that differ: #2, #3"
);
let wrapped = crate::Error::from(UnitError::Missing {
context: 4,
kind: UnitKind::SolidAngle,
});
assert_eq!(
wrapped.to_string(),
"context #4 assigns no solid angle unit"
);
}
#[cfg(feature = "serde")]
mod serialized {
use super::*;
use crate::testing::unique;
fn every_error() -> Vec<UnitError> {
let entity = || "X".to_owned();
vec![
UnitError::Undefined { from: 1, to: 2 },
UnitError::NotAUnit {
instance: 1,
entity: entity(),
},
UnitError::Unsupported {
instance: 1,
entity: entity(),
},
UnitError::Malformed {
instance: 1,
reason: "x",
},
UnitError::NotPositive {
instance: 1,
value: -1.0,
},
UnitError::KindMismatch {
instance: 1,
declared: UnitKind::Length,
found: UnitKind::PlaneAngle,
},
UnitError::TooDeep { instance: 1 },
UnitError::NotAContext {
instance: 1,
entity: entity(),
},
UnitError::NotARepresentation {
instance: 1,
entity: entity(),
},
UnitError::Missing {
context: 1,
kind: UnitKind::SolidAngle,
},
UnitError::Ambiguous {
context: 1,
kind: UnitKind::Length,
units: vec![2, 3],
},
UnitError::Unreadable {
context: 1,
unit: 2,
reason: Box::new(UnitError::TooDeep { instance: 2 }),
},
UnitError::NotHeld { item: 1 },
UnitError::NotFollowed {
item: 1,
container: 2,
entity: entity(),
},
UnitError::HolderUnreadable {
item: 1,
holder: 2,
reason: Box::new(UnitError::Undefined { from: 2, to: 3 }),
},
UnitError::ContextsDisagree {
item: 1,
kind: Some(UnitKind::Length),
contexts: [2, 3],
},
UnitError::ContextsDisagree {
item: 1,
kind: None,
contexts: [2, 3],
},
]
}
#[test]
fn errors_are_tagged_by_kind() {
for error in every_error() {
assert!(unique(&error).starts_with("{\"kind\":"), "{error:?}");
}
assert_eq!(
unique(&UnitError::Missing {
context: 1,
kind: UnitKind::SolidAngle,
}),
r#"{"kind":"missing","context":1,"unit_kind":"solid_angle"}"#
);
}
#[test]
fn context_units_serialize_each_kind_as_ok_or_error() {
let g = context("#6,#12", "#16", "");
let units = ContextUnits::of_context(&g, 100).unwrap();
let json = unique(&units);
assert!(
json.contains(
r#""length":{"ok":{"instance":6,"kind":"length","name":"inch","factor":25.4}}"#
),
"{json}"
);
assert!(
json.contains(r#""solid_angle":{"error":{"kind":"missing""#),
"{json}"
);
unique(&units.uncertainty().unwrap()[0]);
}
}
}