Skip to main content

p2panda_auth/group/
member.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3#[cfg(any(test, feature = "serde"))]
4use serde::{Deserialize, Serialize};
5
6/// A group member which can be a single individual or another group.
7///
8/// The `Group` variant can be used to express nested group relations. In both cases, the member
9/// identifier is the same generic ID.
10#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
11#[cfg_attr(any(test, feature = "serde"), derive(Deserialize, Serialize))]
12pub enum GroupMember<ID> {
13    Individual(ID),
14    Group(ID),
15}
16
17impl<ID> GroupMember<ID>
18where
19    ID: Copy,
20{
21    /// Return the ID of a group member.
22    pub fn id(&self) -> ID {
23        match self {
24            GroupMember::Individual(id) => *id,
25            GroupMember::Group(id) => *id,
26        }
27    }
28
29    /// Return true if this group member is itself a group.
30    pub fn is_group(&self) -> bool {
31        match self {
32            GroupMember::Individual(_) => false,
33            GroupMember::Group(_) => true,
34        }
35    }
36
37    /// Return true if this group member is an individual.
38    pub fn is_individual(&self) -> bool {
39        !self.is_group()
40    }
41}