Skip to main content

aranya_client/client/
role.rs

1use std::{slice, vec};
2
3use aranya_daemon_api as api;
4use aranya_id::custom_id;
5use aranya_policy_text::Text;
6
7use crate::{
8    client::DeviceId,
9    util::{impl_slice_iter_wrapper, impl_vec_into_iter_wrapper, ApiConv as _, ApiId},
10};
11
12custom_id! {
13    /// Uniquely identifies a role.
14    pub struct RoleId;
15}
16impl ApiId<api::RoleId> for RoleId {}
17
18/// A role.
19#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
20#[non_exhaustive]
21pub struct Role {
22    /// Uniquely identifies the role.
23    pub id: RoleId,
24    /// The humman-readable name of the role.
25    pub name: Text,
26    /// The unique ID of the author of the role.
27    pub author_id: DeviceId,
28    /// Is this a default role?
29    pub default: bool,
30}
31
32impl Role {
33    pub(crate) fn from_api(v: api::Role) -> Self {
34        Self {
35            id: RoleId::from_api(v.id),
36            name: v.name,
37            author_id: DeviceId::from_api(v.author_id),
38            default: v.default,
39        }
40    }
41}
42
43/// A set of [`Role`]s.
44#[derive(Clone, Debug)]
45pub struct Roles {
46    pub(super) roles: Box<[Role]>,
47}
48
49impl Roles {
50    /// Returns an iterator over the roles.
51    pub fn iter(&self) -> IterRoles<'_> {
52        IterRoles(self.roles.iter())
53    }
54
55    #[doc(hidden)]
56    pub fn __into_data(self) -> Box<[Role]> {
57        self.roles
58    }
59}
60
61impl IntoIterator for Roles {
62    type Item = Role;
63    type IntoIter = IntoIterRoles;
64
65    fn into_iter(self) -> Self::IntoIter {
66        IntoIterRoles(self.roles.into_vec().into_iter())
67    }
68}
69
70/// An iterator over [`Role`]s.
71#[derive(Clone, Debug)]
72pub struct IterRoles<'a>(slice::Iter<'a, Role>);
73
74impl_slice_iter_wrapper!(IterRoles<'a> for Role);
75
76/// An owning iterator over [`Role`]s.
77#[derive(Clone, Debug)]
78pub struct IntoIterRoles(vec::IntoIter<Role>);
79
80impl_vec_into_iter_wrapper!(IntoIterRoles for Role);