Skip to main content

aranya_client/client/
label.rs

1use std::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_vec_into_iter_wrapper, ApiConv as _, ApiId},
10};
11
12custom_id! {
13    /// An AFC label ID.
14    pub struct LabelId;
15}
16impl ApiId<api::LabelId> for LabelId {}
17
18/// A label.
19#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
20#[non_exhaustive]
21pub struct Label {
22    /// Uniquely identifies the label.
23    pub id: LabelId,
24    /// The human-readable label name.
25    pub name: Text,
26    /// The device that created the label.
27    pub author_id: DeviceId,
28}
29
30impl Label {
31    pub(crate) fn from_api(v: api::Label) -> Self {
32        Self {
33            id: LabelId::from_api(v.id),
34            name: v.name,
35            author_id: DeviceId::from_api(v.author_id),
36        }
37    }
38}
39
40/// List of labels.
41#[derive(Clone, Debug)]
42pub struct Labels {
43    pub(super) labels: Box<[Label]>,
44}
45
46impl Labels {
47    /// Returns an iterator over the labels.
48    pub fn iter(&self) -> impl Iterator<Item = &Label> {
49        self.labels.iter()
50    }
51
52    #[doc(hidden)]
53    pub fn __data(&self) -> &[Label] {
54        &self.labels
55    }
56
57    #[doc(hidden)]
58    pub fn __into_data(self) -> Box<[Label]> {
59        self.labels
60    }
61}
62
63impl IntoIterator for Labels {
64    type Item = Label;
65    type IntoIter = IntoIterLabels;
66
67    fn into_iter(self) -> Self::IntoIter {
68        IntoIterLabels(self.labels.into_vec().into_iter())
69    }
70}
71
72/// An owning iterator over [`Label`]s.
73#[derive(Clone, Debug)]
74pub struct IntoIterLabels(vec::IntoIter<Label>);
75
76impl_vec_into_iter_wrapper!(IntoIterLabels for Label);