use std::{slice, vec};
use aranya_daemon_api as api;
use aranya_id::custom_id;
use aranya_policy_text::Text;
use crate::{
client::DeviceId,
util::{impl_slice_iter_wrapper, impl_vec_into_iter_wrapper, ApiConv as _, ApiId},
};
custom_id! {
pub struct RoleId;
}
impl ApiId<api::RoleId> for RoleId {}
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[non_exhaustive]
pub struct Role {
pub id: RoleId,
pub name: Text,
pub author_id: DeviceId,
pub default: bool,
}
impl Role {
pub(crate) fn from_api(v: api::Role) -> Self {
Self {
id: RoleId::from_api(v.id),
name: v.name,
author_id: DeviceId::from_api(v.author_id),
default: v.default,
}
}
}
#[derive(Clone, Debug)]
pub struct Roles {
pub(super) roles: Box<[Role]>,
}
impl Roles {
pub fn iter(&self) -> IterRoles<'_> {
IterRoles(self.roles.iter())
}
#[doc(hidden)]
pub fn __into_data(self) -> Box<[Role]> {
self.roles
}
}
impl IntoIterator for Roles {
type Item = Role;
type IntoIter = IntoIterRoles;
fn into_iter(self) -> Self::IntoIter {
IntoIterRoles(self.roles.into_vec().into_iter())
}
}
#[derive(Clone, Debug)]
pub struct IterRoles<'a>(slice::Iter<'a, Role>);
impl_slice_iter_wrapper!(IterRoles<'a> for Role);
#[derive(Clone, Debug)]
pub struct IntoIterRoles(vec::IntoIter<Role>);
impl_vec_into_iter_wrapper!(IntoIterRoles for Role);