Skip to main content

coil_auth/types/
subject.rs

1use super::*;
2
3#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4pub enum DefaultSubject {
5    Entity(Entity),
6    Userset { object: Entity, relation: Relation },
7}
8
9impl DefaultSubject {
10    pub fn entity(entity: Entity) -> Self {
11        Self::Entity(entity)
12    }
13
14    pub fn userset(object: Entity, relation: Relation) -> Self {
15        Self::Userset { object, relation }
16    }
17
18    pub fn to_subject(&self) -> Subject {
19        match self {
20            Self::Entity(entity) => Subject::Entity(entity.to_object()),
21            Self::Userset { object, relation } => Subject::Userset {
22                object: object.to_object(),
23                relation: relation.to_string(),
24            },
25        }
26    }
27
28    pub fn from_subject(subject: &Subject) -> Option<Self> {
29        match subject {
30            Subject::Entity(object) => Some(Self::Entity(Entity::from_object(object)?)),
31            Subject::Userset { object, relation } => Some(Self::Userset {
32                object: Entity::from_object(object)?,
33                relation: Relation::from_str(relation)?,
34            }),
35        }
36    }
37}
38
39impl From<&DefaultSubject> for Subject {
40    fn from(value: &DefaultSubject) -> Self {
41        value.to_subject()
42    }
43}
44
45impl From<DefaultSubject> for Subject {
46    fn from(value: DefaultSubject) -> Self {
47        value.to_subject()
48    }
49}