1
1
#![ deny( unsafe_code, rust_2018_idioms, missing_docs) ]
2
2
//! A shared trust model for `gitoxide` crates.
3
3
4
+ use std:: fmt:: { Debug , Display , Formatter } ;
4
5
use std:: marker:: PhantomData ;
5
6
use std:: ops:: Deref ;
6
7
@@ -75,19 +76,22 @@ pub mod trust {
75
76
///
76
77
pub mod permission {
77
78
use crate :: Access ;
79
+ use std:: fmt:: { Debug , Display } ;
78
80
79
81
/// A marker trait to signal tags for permissions.
80
- pub trait Tag { }
82
+ pub trait Tag : Debug { }
81
83
82
84
/// A tag indicating that a permission is applying to the contents of a configuration file.
85
+ #[ derive( Debug ) ]
83
86
pub struct Config ;
84
87
impl Tag for Config { }
85
88
86
89
/// A tag indicating that a permission is applying to the resource itself.
90
+ #[ derive( Debug ) ]
87
91
pub struct Resource ;
88
92
impl Tag for Resource { }
89
93
90
- impl < P > Access < Config , P > {
94
+ impl < P : Debug + Display > Access < Config , P > {
91
95
/// Create a permission for values contained in git configuration files.
92
96
///
93
97
/// This applies permissions to values contained inside of these files.
@@ -99,7 +103,7 @@ pub mod permission {
99
103
}
100
104
}
101
105
102
- impl < P > Access < Resource , P > {
106
+ impl < P : Debug + Display > Access < Resource , P > {
103
107
/// Create a permission a file or directory itself.
104
108
///
105
109
/// This applies permissions to a configuration file itself and whether it can be used at all, or to a directory
@@ -111,6 +115,18 @@ pub mod permission {
111
115
}
112
116
}
113
117
}
118
+
119
+ /// An error to use if an operation cannot proceed due to insufficient permissions.
120
+ ///
121
+ /// It's up to the implementation to decide which permission is required for an operation, and which one
122
+ /// causes errors.
123
+ #[ cfg( feature = "thiserror" ) ]
124
+ #[ derive( Debug , thiserror:: Error ) ]
125
+ #[ error( "Not allowed to handle resource {:?}: permission {}" , . resource, . permission) ]
126
+ pub struct Error < R : Debug , P : Debug + Display > {
127
+ resource : R ,
128
+ permission : P ,
129
+ }
114
130
}
115
131
116
132
/// Allow, deny or forbid using a resource or performing an action.
@@ -125,6 +141,19 @@ pub enum Permission {
125
141
Allow ,
126
142
}
127
143
144
+ impl Display for Permission {
145
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
146
+ Display :: fmt (
147
+ match self {
148
+ Permission :: Allow => "allowed" ,
149
+ Permission :: Deny => "denied" ,
150
+ Permission :: Forbid => "forbidden" ,
151
+ } ,
152
+ f,
153
+ )
154
+ }
155
+ }
156
+
128
157
bitflags:: bitflags! {
129
158
/// Whether something can be read or written.
130
159
#[ cfg_attr( feature = "serde1" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
@@ -136,14 +165,27 @@ bitflags::bitflags! {
136
165
}
137
166
}
138
167
168
+ impl Display for ReadWrite {
169
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
170
+ Debug :: fmt ( self , f)
171
+ }
172
+ }
173
+
139
174
/// A container to define tagged access permissions, rendering the permission read-only.
140
- pub struct Access < T : permission:: Tag , P > {
175
+ #[ derive( Debug ) ]
176
+ pub struct Access < T : permission:: Tag , P : Debug + Display > {
141
177
/// The access permission itself.
142
178
permission : P ,
143
179
_data : PhantomData < T > ,
144
180
}
145
181
146
- impl < T : permission:: Tag , P > Deref for Access < T , P > {
182
+ impl < T : permission:: Tag , P : Debug + Display > Display for Access < T , P > {
183
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
184
+ Display :: fmt ( & self . permission , f)
185
+ }
186
+ }
187
+
188
+ impl < T : permission:: Tag , P : Debug + Display > Deref for Access < T , P > {
147
189
type Target = P ;
148
190
149
191
fn deref ( & self ) -> & Self :: Target {
0 commit comments