Skip to main content

opcua/types/
qualified_name.rs

1// OPCUA for Rust
2// SPDX-License-Identifier: MPL-2.0
3// Copyright (C) 2017-2022 Adam Lock
4
5//! Contains the definition of `QualifiedName`.
6use std::io::{Read, Write};
7
8use crate::types::{encoding::*, string::*};
9
10/// An identifier for a error or condition that is associated with a value or an operation.
11///
12/// A name qualified by a namespace.
13///
14/// For JSON, the namespace_index is saved as "Uri" and MUST be a numeric value or it will not parse. This is
15/// is in accordance with OPC UA spec that says to save the index as a numeric according to rules cut and
16/// pasted from spec below:
17///
18/// Name   The Name component of the QualifiedName.
19///
20/// Uri    The _NamespaceIndexcomponent_ of the QualifiedNameencoded as a JSON number. The Urifield
21///        is omitted if the NamespaceIndex equals 0. For the non-reversible form, the
22///        NamespaceUriassociated with the NamespaceIndexportion of the QualifiedNameis encoded as
23///        JSON string unless the NamespaceIndexis 1 or if NamespaceUriis unknown. In these cases,
24///        the NamespaceIndexis encoded as a JSON number.
25#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
26#[serde(rename_all = "PascalCase")]
27pub struct QualifiedName {
28    /// The namespace index
29    #[serde(rename = "Uri")]
30    pub namespace_index: u16,
31    /// The name.
32    pub name: UAString,
33}
34
35impl<'a> From<&'a str> for QualifiedName {
36    fn from(value: &'a str) -> Self {
37        Self {
38            namespace_index: 0,
39            name: UAString::from(value),
40        }
41    }
42}
43
44impl From<&String> for QualifiedName {
45    fn from(value: &String) -> Self {
46        Self {
47            namespace_index: 0,
48            name: UAString::from(value),
49        }
50    }
51}
52
53impl From<String> for QualifiedName {
54    fn from(value: String) -> Self {
55        Self {
56            namespace_index: 0,
57            name: UAString::from(value),
58        }
59    }
60}
61
62impl BinaryEncoder<QualifiedName> for QualifiedName {
63    fn byte_len(&self) -> usize {
64        let mut size: usize = 0;
65        size += self.namespace_index.byte_len();
66        size += self.name.byte_len();
67        size
68    }
69
70    fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
71        let mut size: usize = 0;
72        size += self.namespace_index.encode(stream)?;
73        size += self.name.encode(stream)?;
74        assert_eq!(size, self.byte_len());
75        Ok(size)
76    }
77
78    fn decode<S: Read>(stream: &mut S, decoding_options: &DecodingOptions) -> EncodingResult<Self> {
79        let namespace_index = u16::decode(stream, decoding_options)?;
80        let name = UAString::decode(stream, decoding_options)?;
81        Ok(QualifiedName {
82            namespace_index,
83            name,
84        })
85    }
86}
87
88impl QualifiedName {
89    pub fn new<T>(namespace_index: u16, name: T) -> QualifiedName
90    where
91        T: Into<UAString>,
92    {
93        QualifiedName {
94            namespace_index,
95            name: name.into(),
96        }
97    }
98
99    pub fn null() -> QualifiedName {
100        QualifiedName {
101            namespace_index: 0,
102            name: UAString::null(),
103        }
104    }
105
106    pub fn is_null(&self) -> bool {
107        self.namespace_index == 0 && self.name.is_null()
108    }
109}