spacegate_kernel/extension/user_group/
combinator.rs1use std::ops::{Deref, DerefMut};
2
3use crate::SgRequest;
4
5use super::UserGroup;
6
7#[derive(Debug)]
8pub struct And<A, B> {
9 pub a: A,
10 pub b: B,
11}
12
13impl<A, B> UserGroup for And<A, B>
14where
15 A: UserGroup,
16 B: UserGroup,
17{
18 fn is_match(&self, req: &SgRequest) -> bool {
19 self.a.is_match(req) && self.b.is_match(req)
20 }
21}
22
23#[derive(Debug)]
24pub struct Not<A> {
25 pub a: A,
26}
27
28impl<A: UserGroup> UserGroup for Not<A> {
29 fn is_match(&self, req: &SgRequest) -> bool {
30 !self.a.is_match(req)
31 }
32}
33
34#[derive(Debug)]
35pub struct Or<A, B> {
36 pub a: A,
37 pub b: B,
38}
39
40impl<A, B> UserGroup for Or<A, B>
41where
42 A: UserGroup,
43 B: UserGroup,
44{
45 fn is_match(&self, req: &SgRequest) -> bool {
46 self.a.is_match(req) || self.b.is_match(req)
47 }
48}
49
50pub struct All {
51 pub groups: Vec<Box<dyn UserGroup>>,
52}
53
54impl std::fmt::Debug for All {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 f.debug_struct("DynamicAll").finish()
57 }
58}
59
60impl Deref for All {
61 type Target = Vec<Box<dyn UserGroup>>;
62
63 fn deref(&self) -> &Self::Target {
64 &self.groups
65 }
66}
67
68impl DerefMut for All {
69 fn deref_mut(&mut self) -> &mut Self::Target {
70 &mut self.groups
71 }
72}
73
74impl UserGroup for All {
75 fn is_match(&self, req: &SgRequest) -> bool {
76 self.groups.iter().all(|g| g.is_match(req))
77 }
78}
79
80impl All {
81 pub fn new(groups: Vec<Box<dyn UserGroup>>) -> Self {
82 All { groups }
83 }
84}
85
86pub struct Any {
87 pub groups: Vec<Box<dyn UserGroup>>,
88}
89
90impl std::fmt::Debug for Any {
91 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92 f.debug_struct("DynamicAny").finish()
93 }
94}
95
96impl Deref for Any {
97 type Target = Vec<Box<dyn UserGroup>>;
98
99 fn deref(&self) -> &Self::Target {
100 &self.groups
101 }
102}
103
104impl DerefMut for Any {
105 fn deref_mut(&mut self) -> &mut Self::Target {
106 &mut self.groups
107 }
108}
109
110impl UserGroup for Any {
111 fn is_match(&self, req: &SgRequest) -> bool {
112 self.groups.iter().any(|g| g.is_match(req))
113 }
114}