up_rust/uattributes/upriority.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
14use crate::uattributes::UAttributesError;
15
16#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
17#[repr(C)]
18/// QoS priority classes defined by the uProtocol specification.
19pub enum UPriority {
20 /// Best effort (lowest priority).
21 CS0,
22 /// Priority class CS1.
23 CS1,
24 /// Priority class CS2.
25 CS2,
26 /// Priority class CS3.
27 CS3,
28 /// Priority class CS4 (streaming).
29 CS4,
30 /// Priority class CS5 (RPC default).
31 CS5,
32 /// Priority class CS6 (highest; network control).
33 CS6,
34}
35
36impl UPriority {
37 /// Encodes this priority to a string.
38 ///
39 /// The encoding of priorities to strings is defined in the
40 /// [uProtocol Core API](https://github.com/eclipse-uprotocol/up-core-api/blob/main/uprotocol/uattributes.proto).
41 ///
42 /// # Examples
43 ///
44 /// ```
45 /// use up_rust::UPriority;
46 ///
47 /// assert_eq!(UPriority::CS2.to_priority_code(), "CS2");
48 /// ```
49 #[must_use]
50 pub fn to_priority_code(self) -> &'static str {
51 match self {
52 UPriority::CS0 => "CS0",
53 UPriority::CS1 => "CS1",
54 UPriority::CS2 => "CS2",
55 UPriority::CS3 => "CS3",
56 UPriority::CS4 => "CS4",
57 UPriority::CS5 => "CS5",
58 UPriority::CS6 => "CS6",
59 }
60 }
61
62 /// Gets the priority for a string.
63 ///
64 /// The encoding of priorities to strings is defined in the
65 /// [uProtocol Core API](https://github.com/eclipse-uprotocol/up-core-api/blob/main/uprotocol/uattributes.proto).
66 ///
67 /// # Errors
68 ///
69 /// Returns an error if the given string does not match a supported priority.
70 ///
71 /// # Examples
72 ///
73 /// ```
74 /// use up_rust::UPriority;
75 ///
76 /// let priority = UPriority::try_from_priority_code("CS2").unwrap();
77 /// assert_eq!(priority, UPriority::CS2);
78 ///
79 /// assert!(UPriority::try_from_priority_code("not-supported").is_err());
80 /// ```
81 pub fn try_from_priority_code<T>(code: T) -> Result<Self, UAttributesError>
82 where
83 T: Into<String>,
84 {
85 let prio: String = code.into();
86 match prio.as_str() {
87 "CS0" => Ok(UPriority::CS0),
88 "CS1" => Ok(UPriority::CS1),
89 "CS2" => Ok(UPriority::CS2),
90 "CS3" => Ok(UPriority::CS3),
91 "CS4" => Ok(UPriority::CS4),
92 "CS5" => Ok(UPriority::CS5),
93 "CS6" => Ok(UPriority::CS6),
94 _ => Err(UAttributesError::parsing_error(format!(
95 "unknown priority code [{prio}]"
96 ))),
97 }
98 }
99}
100
101#[cfg(feature = "up-core-api")]
102mod core_types_support {
103 use super::*;
104 use crate::up_core_api::uattributes::UPriority as UPriorityProto;
105
106 impl From<&UPriority> for UPriorityProto {
107 fn from(value: &UPriority) -> Self {
108 match value {
109 UPriority::CS0 => UPriorityProto::UPRIORITY_CS0,
110 UPriority::CS1 => UPriorityProto::UPRIORITY_CS1,
111 UPriority::CS2 => UPriorityProto::UPRIORITY_CS2,
112 UPriority::CS3 => UPriorityProto::UPRIORITY_CS3,
113 UPriority::CS4 => UPriorityProto::UPRIORITY_CS4,
114 UPriority::CS5 => UPriorityProto::UPRIORITY_CS5,
115 UPriority::CS6 => UPriorityProto::UPRIORITY_CS6,
116 }
117 }
118 }
119
120 impl TryFrom<UPriorityProto> for UPriority {
121 type Error = UAttributesError;
122
123 fn try_from(value: UPriorityProto) -> Result<Self, Self::Error> {
124 match value {
125 UPriorityProto::UPRIORITY_CS0 => Ok(UPriority::CS0),
126 UPriorityProto::UPRIORITY_CS1 => Ok(UPriority::CS1),
127 UPriorityProto::UPRIORITY_CS2 => Ok(UPriority::CS2),
128 UPriorityProto::UPRIORITY_CS3 => Ok(UPriority::CS3),
129 UPriorityProto::UPRIORITY_CS4 => Ok(UPriority::CS4),
130 UPriorityProto::UPRIORITY_CS5 => Ok(UPriority::CS5),
131 UPriorityProto::UPRIORITY_CS6 => Ok(UPriority::CS6),
132 _ => Err(UAttributesError::parsing_error(format!(
133 "invalid UPriority value: {}",
134 value as i32
135 ))),
136 }
137 }
138 }
139}