#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ElementRef {
pub document: Option<String>,
pub id: String,
}
impl ElementRef {
#[must_use]
pub fn local(id: impl Into<String>) -> Self {
Self {
document: None,
id: id.into(),
}
}
#[must_use]
pub fn in_document(document: impl Into<String>, id: impl Into<String>) -> Self {
Self {
document: Some(document.into()),
id: id.into(),
}
}
#[must_use]
pub fn is_cross_document(&self) -> bool {
self.document.is_some()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn element_refs_distinguish_scope() {
let local = ElementRef::local("a");
assert_eq!(local.document, None);
assert!(!local.is_cross_document());
let remote = ElementRef::in_document("d.ifc", "a");
assert_eq!(remote.document.as_deref(), Some("d.ifc"));
assert!(remote.is_cross_document());
}
#[test]
fn same_id_in_different_documents_is_not_the_same_element() {
assert_ne!(
ElementRef::in_document("a.ifc", "x"),
ElementRef::in_document("b.ifc", "x")
);
assert_ne!(
ElementRef::local("x"),
ElementRef::in_document("a.ifc", "x")
);
}
}