Skip to main content

egml_core/model/basic/
code.rs

1/// A coded value optionally qualified by a code-space URI.
2///
3/// Corresponds to `gml:CodeType` in ISO 19136.  The optional `code_space`
4/// attribute is a URI that identifies the code list or dictionary in which
5/// `value` is defined.
6///
7/// # Examples
8///
9/// ```rust
10/// use egml_core::model::basic::Code;
11///
12///
13/// let code = Code::with_code_space("https://example.org/codes", "WallSurface");
14/// assert_eq!(code.value(), "WallSurface");
15/// ```
16#[derive(Debug, Clone, PartialEq, PartialOrd)]
17pub struct Code {
18    /// Optional URI identifying the code list or dictionary.
19    code_space: Option<String>,
20    /// The code value string.
21    value: String,
22}
23
24impl Code {
25    /// Creates a new `Code` without a code space.
26    pub fn new(value: impl Into<String>) -> Self {
27        Self {
28            code_space: None,
29            value: value.into(),
30        }
31    }
32
33    /// Creates a new `Code` with a code space.
34    pub fn with_code_space(code_space: impl Into<String>, value: impl Into<String>) -> Self {
35        Self {
36            code_space: Some(code_space.into()),
37            value: value.into(),
38        }
39    }
40
41    pub fn from_parts(code_space: Option<impl Into<String>>, value: impl Into<String>) -> Self {
42        Self {
43            code_space: code_space.map(Into::into),
44            value: value.into(),
45        }
46    }
47
48    /// Returns the code value.
49    pub fn value(&self) -> &str {
50        &self.value
51    }
52
53    /// Returns the optional code-space URI.
54    pub fn code_space(&self) -> Option<&str> {
55        self.code_space.as_deref()
56    }
57}