kerml/root/
element.rs

1// This is free and unencumbered software released into the public domain.
2
3use super::{Namespace, QualifiedName, Relationship};
4use crate::prelude::{fmt, vec, String, Vec};
5
6pub trait Element {
7    /// The unique element ID, if any.
8    fn id(&self) -> Option<&str> {
9        None
10    }
11
12    /// Various alternative identifiers for this [`Element`].
13    fn alias_ids(&self) -> Vec<&str> {
14        Vec::new()
15    }
16
17    /// The name of the element.
18    fn name(&self) -> Option<&str> {
19        None
20    }
21
22    /// The short name of the element, if any.
23    fn short_name(&self) -> Option<&str> {
24        None
25    }
26
27    /// Whether this [`Element`] is contained in the ownership tree of
28    /// a library model.
29    fn is_library_element(&self) -> bool {
30        false
31    }
32
33    /// The owner of this [`Element`], if any.
34    fn owner(&self) -> Option<&dyn Element> {
35        None
36    }
37
38    /// The [`Namespace`] that owns this [`Element`], if any.
39    fn owning_namespace(&self) -> Option<&dyn Namespace> {
40        None
41    }
42
43    /// The [`Relationship`] for which this [`Element`] is an
44    /// `owned_related_element`, if any.
45    fn owning_relationship(&self) -> Option<&dyn Relationship> {
46        None
47    }
48
49    fn qualified_name(&self) -> Option<QualifiedName> {
50        let owning_namespace = self.owning_namespace()?;
51        let escaped_name = String::from(self.name()?);
52        if owning_namespace.owner().is_none() {
53            Some(QualifiedName::from(escaped_name))
54        } else {
55            let mut names = owning_namespace.qualified_name()?.to_vec();
56            names.push(escaped_name);
57            Some(QualifiedName::from(names))
58        }
59    }
60}