up_rust/
uattributes.rs

1/********************************************************************************
2 * Copyright (c) 2023 Contributors to the Eclipse Foundation
3 *
4 * See the NOTICE file(s) distributed with this work for additional
5 * information regarding copyright ownership.
6 *
7 * This program and the accompanying materials are made available under the
8 * terms of the Apache License Version 2.0 which is available at
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * SPDX-License-Identifier: Apache-2.0
12 ********************************************************************************/
13
14mod uattributesvalidator;
15mod upayloadformat;
16mod upriority;
17
18pub use uattributesvalidator::*;
19pub use upriority::*;
20
21pub use crate::up_core_api::uattributes::*;
22
23#[derive(Debug)]
24pub enum UAttributesError {
25    ValidationError(String),
26    ParsingError(String),
27}
28
29impl UAttributesError {
30    pub fn validation_error<T>(message: T) -> UAttributesError
31    where
32        T: Into<String>,
33    {
34        Self::ValidationError(message.into())
35    }
36
37    pub fn parsing_error<T>(message: T) -> UAttributesError
38    where
39        T: Into<String>,
40    {
41        Self::ParsingError(message.into())
42    }
43}
44
45impl std::fmt::Display for UAttributesError {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        match self {
48            Self::ValidationError(e) => f.write_fmt(format_args!("Validation failure: {}", e)),
49            Self::ParsingError(e) => f.write_fmt(format_args!("Parsing error: {}", e)),
50        }
51    }
52}
53
54impl std::error::Error for UAttributesError {}
55
56impl UAttributes {
57    /// Checks if these are the attributes for a Publish message.
58    ///
59    /// # Examples
60    ///
61    /// ```rust
62    /// use up_rust::{UAttributes, UMessageType};
63    ///
64    /// let attribs = UAttributes {
65    ///   type_: UMessageType::UMESSAGE_TYPE_PUBLISH.into(),
66    ///   ..Default::default()
67    /// };
68    /// assert!(attribs.is_publish());
69    /// ```
70    pub fn is_publish(&self) -> bool {
71        self.type_.enum_value() == Ok(UMessageType::UMESSAGE_TYPE_PUBLISH)
72    }
73
74    /// Checks if these are the attributes for an RPC Request message.
75    ///
76    /// # Examples
77    ///
78    /// ```rust
79    /// use up_rust::{UAttributes, UMessageType};
80    ///
81    /// let attribs = UAttributes {
82    ///   type_: UMessageType::UMESSAGE_TYPE_REQUEST.into(),
83    ///   ..Default::default()
84    /// };
85    /// assert!(attribs.is_request());
86    /// ```
87    pub fn is_request(&self) -> bool {
88        self.type_.enum_value() == Ok(UMessageType::UMESSAGE_TYPE_REQUEST)
89    }
90
91    /// Checks if these are the attributes for an RPC Response message.
92    ///
93    /// # Examples
94    ///
95    /// ```rust
96    /// use up_rust::{UAttributes, UMessageType};
97    ///
98    /// let attribs = UAttributes {
99    ///   type_: UMessageType::UMESSAGE_TYPE_RESPONSE.into(),
100    ///   ..Default::default()
101    /// };
102    /// assert!(attribs.is_response());
103    /// ```
104    pub fn is_response(&self) -> bool {
105        self.type_.enum_value() == Ok(UMessageType::UMESSAGE_TYPE_RESPONSE)
106    }
107
108    /// Checks if these are the attributes for a Notification message.
109    ///
110    /// # Examples
111    ///
112    /// ```rust
113    /// use up_rust::{UAttributes, UMessageType};
114    ///
115    /// let attribs = UAttributes {
116    ///   type_: UMessageType::UMESSAGE_TYPE_NOTIFICATION.into(),
117    ///   ..Default::default()
118    /// };
119    /// assert!(attribs.is_notification());
120    /// ```
121    pub fn is_notification(&self) -> bool {
122        self.type_.enum_value() == Ok(UMessageType::UMESSAGE_TYPE_NOTIFICATION)
123    }
124}