opcua/types/
qualified_name.rs1use std::io::{Read, Write};
7
8use crate::types::{encoding::*, string::*};
9
10#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
26#[serde(rename_all = "PascalCase")]
27pub struct QualifiedName {
28 #[serde(rename = "Uri")]
30 pub namespace_index: u16,
31 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}