opcua_types/
localized_text.rs

1// OPCUA for Rust
2// SPDX-License-Identifier: MPL-2.0
3// Copyright (C) 2017-2024 Adam Lock
4
5//! Contains the definition of `LocalizedText`.
6use std::{
7    fmt,
8    io::{Read, Write},
9};
10
11use crate::{
12    encoding::{BinaryDecodable, BinaryEncodable, EncodingResult},
13    string::*,
14};
15
16#[allow(unused)]
17mod opcua {
18    pub(super) use crate as types;
19}
20/// A human readable text with an optional locale identifier.
21#[derive(PartialEq, Default, Debug, Clone, crate::UaNullable)]
22#[cfg_attr(
23    feature = "json",
24    derive(opcua_macros::JsonEncodable, opcua_macros::JsonDecodable)
25)]
26#[cfg_attr(
27    feature = "xml",
28    derive(crate::XmlEncodable, crate::XmlDecodable, crate::XmlType)
29)]
30pub struct LocalizedText {
31    /// The locale. Omitted from stream if null or empty
32    pub locale: UAString,
33    /// The text in the specified locale. Omitted from stream if null or empty.
34    pub text: UAString,
35}
36
37impl<'a> From<&'a str> for LocalizedText {
38    fn from(value: &'a str) -> Self {
39        Self {
40            locale: UAString::null(),
41            text: UAString::from(value),
42        }
43    }
44}
45
46impl From<&String> for LocalizedText {
47    fn from(value: &String) -> Self {
48        Self {
49            locale: UAString::null(),
50            text: UAString::from(value),
51        }
52    }
53}
54
55impl From<String> for LocalizedText {
56    fn from(value: String) -> Self {
57        Self {
58            locale: UAString::null(),
59            text: UAString::from(value),
60        }
61    }
62}
63
64impl fmt::Display for LocalizedText {
65    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66        write!(f, "{}", self.text)
67    }
68}
69
70impl BinaryEncodable for LocalizedText {
71    fn byte_len(&self, ctx: &opcua::types::Context<'_>) -> usize {
72        let mut size = 1;
73        if !self.locale.is_empty() {
74            size += self.locale.byte_len(ctx);
75        }
76        if !self.text.is_empty() {
77            size += self.text.byte_len(ctx);
78        }
79        size
80    }
81
82    fn encode<S: Write + ?Sized>(
83        &self,
84        stream: &mut S,
85        ctx: &crate::Context<'_>,
86    ) -> EncodingResult<()> {
87        // A bit mask that indicates which fields are present in the stream.
88        // The mask has the following bits:
89        // 0x01    Locale
90        // 0x02    Text
91        let mut encoding_mask: u8 = 0;
92        if !self.locale.is_empty() {
93            encoding_mask |= 0x1;
94        }
95        if !self.text.is_empty() {
96            encoding_mask |= 0x2;
97        }
98        encoding_mask.encode(stream, ctx)?;
99        if !self.locale.is_empty() {
100            self.locale.encode(stream, ctx)?;
101        }
102        if !self.text.is_empty() {
103            self.text.encode(stream, ctx)?;
104        }
105        Ok(())
106    }
107}
108
109impl BinaryDecodable for LocalizedText {
110    fn decode<S: Read + ?Sized>(stream: &mut S, ctx: &crate::Context<'_>) -> EncodingResult<Self> {
111        let encoding_mask = u8::decode(stream, ctx)?;
112        let locale = if encoding_mask & 0x1 != 0 {
113            UAString::decode(stream, ctx)?
114        } else {
115            UAString::null()
116        };
117        let text = if encoding_mask & 0x2 != 0 {
118            UAString::decode(stream, ctx)?
119        } else {
120            UAString::null()
121        };
122        Ok(LocalizedText { locale, text })
123    }
124}
125
126impl LocalizedText {
127    /// Create a new LocalizedText from the specified locale and text.
128    pub fn new(locale: &str, text: &str) -> LocalizedText {
129        LocalizedText {
130            locale: UAString::from(locale),
131            text: UAString::from(text),
132        }
133    }
134
135    /// Create a null LocalizedText.
136    pub fn null() -> LocalizedText {
137        LocalizedText {
138            locale: UAString::null(),
139            text: UAString::null(),
140        }
141    }
142}