@@ -79,19 +79,19 @@ pub mod permission {
79
79
use std:: fmt:: { Debug , Display } ;
80
80
81
81
/// A marker trait to signal tags for permissions.
82
- pub trait Tag : Debug { }
82
+ pub trait Tag : Debug + Clone { }
83
83
84
84
/// A tag indicating that a permission is applying to the contents of a configuration file.
85
- #[ derive( Debug ) ]
85
+ #[ derive( Debug , Clone ) ]
86
86
pub struct Config ;
87
87
impl Tag for Config { }
88
88
89
89
/// A tag indicating that a permission is applying to the resource itself.
90
- #[ derive( Debug ) ]
90
+ #[ derive( Debug , Clone ) ]
91
91
pub struct Resource ;
92
92
impl Tag for Resource { }
93
93
94
- impl < P : Debug + Display > Access < Config , P > {
94
+ impl < P : Debug + Display + Clone > Access < Config , P > {
95
95
/// Create a permission for values contained in git configuration files.
96
96
///
97
97
/// This applies permissions to values contained inside of these files.
@@ -103,7 +103,7 @@ pub mod permission {
103
103
}
104
104
}
105
105
106
- impl < P : Debug + Display > Access < Resource , P > {
106
+ impl < P : Debug + Display + Clone > Access < Resource , P > {
107
107
/// Create a permission a file or directory itself.
108
108
///
109
109
/// This applies permissions to a configuration file itself and whether it can be used at all, or to a directory
@@ -124,8 +124,10 @@ pub mod permission {
124
124
#[ derive( Debug , thiserror:: Error ) ]
125
125
#[ error( "Not allowed to handle resource {:?}: permission {}" , . resource, . permission) ]
126
126
pub struct Error < R : Debug , P : Debug + Display > {
127
- resource : R ,
128
- permission : P ,
127
+ /// The resource which cannot be used.
128
+ pub resource : R ,
129
+ /// The permission causing it to be disallowed.
130
+ pub permission : P ,
129
131
}
130
132
}
131
133
@@ -141,6 +143,22 @@ pub enum Permission {
141
143
Allow ,
142
144
}
143
145
146
+ impl Permission {
147
+ /// Check this permissions and produce a reply to indicate if the `resource` can be used and in which way.
148
+ ///
149
+ /// Only if this permission is set to `Allow` will the resource be usable.
150
+ pub fn check < R : Debug > ( & self , resource : R ) -> Result < Option < R > , permission:: Error < R , Self > > {
151
+ match self {
152
+ Permission :: Allow => Ok ( Some ( resource) ) ,
153
+ Permission :: Deny => Ok ( None ) ,
154
+ Permission :: Forbid => Err ( permission:: Error {
155
+ resource,
156
+ permission : self . clone ( ) ,
157
+ } ) ,
158
+ }
159
+ }
160
+ }
161
+
144
162
impl Display for Permission {
145
163
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
146
164
Display :: fmt (
@@ -172,20 +190,20 @@ impl Display for ReadWrite {
172
190
}
173
191
174
192
/// A container to define tagged access permissions, rendering the permission read-only.
175
- #[ derive( Debug ) ]
176
- pub struct Access < T : permission:: Tag , P : Debug + Display > {
193
+ #[ derive( Debug , Clone ) ]
194
+ pub struct Access < T : permission:: Tag , P : Debug + Display + Clone > {
177
195
/// The access permission itself.
178
196
permission : P ,
179
197
_data : PhantomData < T > ,
180
198
}
181
199
182
- impl < T : permission:: Tag , P : Debug + Display > Display for Access < T , P > {
200
+ impl < T : permission:: Tag , P : Debug + Display + Clone > Display for Access < T , P > {
183
201
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
184
202
Display :: fmt ( & self . permission , f)
185
203
}
186
204
}
187
205
188
- impl < T : permission:: Tag , P : Debug + Display > Deref for Access < T , P > {
206
+ impl < T : permission:: Tag , P : Debug + Display + Clone > Deref for Access < T , P > {
189
207
type Target = P ;
190
208
191
209
fn deref ( & self ) -> & Self :: Target {
0 commit comments