Skip to main content

bacnet_types/constructed/
name_value.rs

1//! `BACnetNameValue` (Clause 21): one tag of an object's Tags array (#1553).
2
3#[cfg(not(feature = "std"))]
4use alloc::string::String;
5
6use crate::primitives::PropertyValue;
7
8/// `BACnetNameValue` (Clause 21): a tag's name and, for a value tag, its
9/// value. A semantic tag has a name alone (Annex Y.1.4).
10///
11/// On the wire it is a SEQUENCE with no frame of its own: the name as a
12/// CharacterString under primitive context tag `[0]`, then the value, when
13/// there is one, application-tagged as its own datatype. The `bacnet-encoding`
14/// crate owns the codec. The 2024-04-29 errata, item 37, restricts the
15/// optional value to one primitive; Date and Time are allowed separately.
16///
17/// ```
18/// use bacnet_types::constructed::BACnetNameValue;
19/// use bacnet_types::primitives::PropertyValue;
20///
21/// let semantic = BACnetNameValue::semantic("exhaust");
22/// let valued = BACnetNameValue::valued("floor", PropertyValue::Unsigned(3));
23/// assert!(semantic.value.is_none() && valued.value.is_some());
24/// ```
25#[derive(Debug, Clone, PartialEq)]
26pub struct BACnetNameValue {
27    /// Context tag 0: the tag's name.
28    pub name: String,
29    /// One primitive value; `None` for a semantic tag. The codec and object
30    /// profile validator reject `List` and `ApplicationData` values.
31    pub value: Option<PropertyValue>,
32}
33
34impl BACnetNameValue {
35    /// A semantic tag: a name with no value.
36    pub fn semantic(name: impl Into<String>) -> Self {
37        Self {
38            name: name.into(),
39            value: None,
40        }
41    }
42
43    /// A value tag: a name and its value.
44    pub fn valued(name: impl Into<String>, value: PropertyValue) -> Self {
45        Self {
46            name: name.into(),
47            value: Some(value),
48        }
49    }
50}