Skip to main content

egml_core/model/base/
reference.rs

1use crate::model::base::{
2    AssociationAttributes, HasAssociationAttributes, HasAssociationAttributesMut,
3    HasOwnershipAttributes, HasOwnershipAttributesMut, OwnershipAttributes,
4};
5use crate::model::xlink::HRef;
6
7/// A by-reference-only property, corresponding to `gml:ReferenceType`.
8///
9/// Unlike property wrappers such as `AbstractSurfaceProperty`, a `Reference` never carries
10/// inline content — the type's content model is empty (`<sequence/>`); it only ever points at
11/// another object via [`xlink:href`](AssociationAttributes::href).
12///
13/// Corresponds to `gml:ReferenceType` in [OGC 07-036 §7.2.3.7](https://docs.ogc.org/is/07-036/07-036.pdf).
14#[derive(Debug, Clone, PartialEq, Default)]
15pub struct Reference {
16    pub association: AssociationAttributes,
17    pub ownership: OwnershipAttributes,
18}
19
20impl Reference {
21    /// Creates a `Reference` pointing at `href`.
22    pub fn new(href: HRef) -> Self {
23        Self {
24            association: AssociationAttributes::new_href(href),
25            ownership: OwnershipAttributes::default(),
26        }
27    }
28}
29
30impl HasAssociationAttributes for Reference {
31    fn association(&self) -> &AssociationAttributes {
32        &self.association
33    }
34}
35
36impl HasAssociationAttributesMut for Reference {
37    fn association_mut(&mut self) -> &mut AssociationAttributes {
38        &mut self.association
39    }
40}
41
42impl HasOwnershipAttributes for Reference {
43    fn ownership(&self) -> &OwnershipAttributes {
44        &self.ownership
45    }
46}
47
48impl HasOwnershipAttributesMut for Reference {
49    fn ownership_mut(&mut self) -> &mut OwnershipAttributes {
50        &mut self.ownership
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::Reference;
57    use crate::model::base::{
58        HasAssociationAttributes, HasAssociationAttributesMut, HasOwnershipAttributes,
59    };
60    use crate::model::xlink::HRef;
61
62    #[test]
63    fn new_sets_href() {
64        let reference = Reference::new(HRef::from_local("some-id"));
65        assert_eq!(reference.href(), Some(&HRef::from_local("some-id")));
66        assert!(!reference.owns());
67    }
68
69    #[test]
70    fn set_role_updates_association() {
71        let mut reference = Reference::new(HRef::from_local("some-id"));
72        reference.set_role("http://example.com/role");
73        assert_eq!(reference.role(), Some("http://example.com/role"));
74    }
75}