openthread/ot/types/device_role.rs
1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::prelude_internal::*;
6
7use num_derive::FromPrimitive;
8
9/// Represents a Thread device role.
10///
11/// Functional equivalent of [`otsys::otDeviceRole`](crate::otsys::otDeviceRole).
12#[derive(Debug, Copy, Clone, Eq, Ord, PartialOrd, PartialEq, FromPrimitive)]
13#[allow(missing_docs)]
14pub enum DeviceRole {
15 /// Functional equivalent of [`otsys::OT_DEVICE_ROLE_DISABLED`](crate::otsys::OT_DEVICE_ROLE_DISABLED).
16 Disabled = OT_DEVICE_ROLE_DISABLED as isize,
17
18 /// Functional equivalent of [`otsys::OT_DEVICE_ROLE_DETACHED`](crate::otsys::OT_DEVICE_ROLE_DETACHED).
19 Detached = OT_DEVICE_ROLE_DETACHED as isize,
20
21 /// Functional equivalent of [`otsys::OT_DEVICE_ROLE_CHILD`](crate::otsys::OT_DEVICE_ROLE_CHILD).
22 Child = OT_DEVICE_ROLE_CHILD as isize,
23
24 /// Functional equivalent of [`otsys::OT_DEVICE_ROLE_ROUTER`](crate::otsys::OT_DEVICE_ROLE_ROUTER).
25 Router = OT_DEVICE_ROLE_ROUTER as isize,
26
27 /// Functional equivalent of [`otsys::OT_DEVICE_ROLE_LEADER`](crate::otsys::OT_DEVICE_ROLE_LEADER).
28 Leader = OT_DEVICE_ROLE_LEADER as isize,
29}
30
31impl DeviceRole {
32 /// Returns true if the role is not disabled nor detached.
33 pub fn is_active(&self) -> bool {
34 #[allow(clippy::match_like_matches_macro)]
35 match self {
36 DeviceRole::Disabled | DeviceRole::Detached => false,
37 _ => true,
38 }
39 }
40}
41
42impl From<otDeviceRole> for DeviceRole {
43 fn from(x: otDeviceRole) -> Self {
44 use num::FromPrimitive;
45 Self::from_u32(x).unwrap_or_else(|| panic!("Unknown otDeviceRole value: {x}"))
46 }
47}
48
49impl From<DeviceRole> for otDeviceRole {
50 fn from(x: DeviceRole) -> Self {
51 x as otDeviceRole
52 }
53}