Skip to main content

made_core/value_objects/ceremony/
ceremony_role.rs

1use std::collections::BTreeSet;
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::DomainError;
6
7use super::{RoleAction, RoleId};
8
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct CeremonyRole {
11    id: RoleId,
12    allowed_actions: BTreeSet<RoleAction>,
13}
14
15impl CeremonyRole {
16    pub fn new(
17        id: RoleId,
18        allowed_actions: impl IntoIterator<Item = RoleAction>,
19    ) -> Result<Self, DomainError> {
20        let allowed_actions: BTreeSet<RoleAction> = allowed_actions.into_iter().collect();
21        if allowed_actions.is_empty() {
22            return Err(DomainError::EmptyCollection {
23                field: "ceremony_role.allowed_actions",
24            });
25        }
26        Ok(Self {
27            id,
28            allowed_actions,
29        })
30    }
31
32    #[must_use]
33    pub fn id(&self) -> &RoleId {
34        &self.id
35    }
36
37    #[must_use]
38    pub fn allowed_actions(&self) -> &BTreeSet<RoleAction> {
39        &self.allowed_actions
40    }
41
42    #[must_use]
43    pub fn allows(&self, action: &RoleAction) -> bool {
44        self.allowed_actions.contains(action)
45    }
46}