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 protobuf::EnumFull;
15
16use crate::uattributes::UAttributesError;
17pub use crate::up_core_api::uattributes::UPriority;
18use crate::up_core_api::uoptions::exts::ce_name;
19
20impl UPriority {
21    /// Encodes this priority to a string.
22    ///
23    /// The encoding of priorities to strings is defined in the
24    /// [uProtocol Core API](https://github.com/eclipse-uprotocol/up-core-api/blob/main/uprotocol/uattributes.proto).
25    ///
26    /// # Examples
27    ///
28    /// ```
29    /// use up_rust::UPriority;
30    ///
31    /// assert_eq!(UPriority::UPRIORITY_CS2.to_priority_code(), "CS2");
32    /// ```
33    pub fn to_priority_code(self) -> String {
34        let desc = self.descriptor();
35        let desc_proto = desc.proto();
36        ce_name
37            .get(desc_proto.options.get_or_default())
38            .unwrap_or_default()
39    }
40
41    /// Gets the priority for a string.
42    ///
43    /// The encoding of priorities to strings is defined in the
44    /// [uProtocol Core API](https://github.com/eclipse-uprotocol/up-core-api/blob/main/uprotocol/uattributes.proto).
45    ///
46    /// # Errors
47    ///
48    /// Returns an error if the given string does not match a supported priority.
49    ///
50    /// # Examples
51    ///
52    /// ```
53    /// use up_rust::UPriority;
54    ///
55    /// let priority = UPriority::try_from_priority_code("CS2").unwrap();
56    /// assert_eq!(priority, UPriority::UPRIORITY_CS2);
57    ///
58    /// assert!(UPriority::try_from_priority_code("not-supported").is_err());
59    /// ```
60    pub fn try_from_priority_code<T>(code: T) -> Result<Self, UAttributesError>
61    where
62        T: Into<String>,
63    {
64        let prio: String = code.into();
65        Self::enum_descriptor()
66            .values()
67            .find_map(|desc| {
68                let proto_desc = desc.proto();
69
70                ce_name
71                    .get(proto_desc.options.get_or_default())
72                    .and_then(|prio_option_value| {
73                        if prio_option_value.eq(prio.as_str()) {
74                            desc.cast::<Self>()
75                        } else {
76                            None
77                        }
78                    })
79            })
80            .ok_or_else(|| UAttributesError::parsing_error(format!("unknown priority [{prio}]")))
81    }
82}