@@ -51,6 +51,108 @@ pub struct Password {
51
51
pub password : String ,
52
52
}
53
53
54
+ #[ derive( Clone , Debug ) ]
55
+ /// Data required to create a gist in DB
56
+ /// creation date defaults to time at which creation method is called
57
+ pub struct CreateGist {
58
+ /// owner of the gist
59
+ pub owner : String ,
60
+ /// description of the gist
61
+ pub description : Option < String > ,
62
+ /// public ID of the gist
63
+ pub public_id : String ,
64
+ /// gist privacy
65
+ pub privacy : GistPrivacy ,
66
+ }
67
+
68
+ /// Gist privacy
69
+ #[ derive( Clone , PartialEq , Debug ) ]
70
+ pub enum GistPrivacy {
71
+ /// Everyone can see the gist, will be displayed on /explore and
72
+ /// search engines might index it too
73
+ Public ,
74
+ /// Everyone with the link can see it, won't be listed on /explore and
75
+ /// search engines won't index them
76
+ Unlisted ,
77
+ /// Only the owner can see gist
78
+ Private ,
79
+ }
80
+
81
+ impl GistPrivacy {
82
+ /// Convert [GistPrivacy] to [str]
83
+ pub const fn to_str ( & self ) -> & ' static str {
84
+ match self {
85
+ GistPrivacy :: Private => "private" ,
86
+ GistPrivacy :: Unlisted => "unlisted" ,
87
+ GistPrivacy :: Public => "public" ,
88
+ }
89
+ }
90
+
91
+ /// Convert [str] to [GistPrivacy]
92
+ pub fn from_str ( s : & str ) -> DBResult < Self > {
93
+ const PRIVATE : & str = GistPrivacy :: Private . to_str ( ) ;
94
+ const PUBLIC : & str = GistPrivacy :: Public . to_str ( ) ;
95
+ const UNLISTED : & str = GistPrivacy :: Unlisted . to_str ( ) ;
96
+ let s = s. trim ( ) ;
97
+ match s {
98
+ PRIVATE => Ok ( Self :: Private ) ,
99
+ PUBLIC => Ok ( Self :: Public ) ,
100
+ UNLISTED => Ok ( Self :: Unlisted ) ,
101
+ _ => Err ( DBError :: UnknownPrivacySpecifier ( s. to_owned ( ) ) ) ,
102
+ }
103
+ }
104
+ }
105
+
106
+ impl From < GistPrivacy > for String {
107
+ fn from ( gp : GistPrivacy ) -> String {
108
+ gp. to_str ( ) . into ( )
109
+ }
110
+ }
111
+
112
+ #[ derive( Clone , Debug ) ]
113
+ /// Represents a gist
114
+ pub struct Gist {
115
+ /// owner of the gist
116
+ pub owner : String ,
117
+ /// description of the gist
118
+ pub description : Option < String > ,
119
+ /// public ID of the gist
120
+ pub public_id : String ,
121
+ /// gist creation time
122
+ pub created : i64 ,
123
+ /// gist updated time
124
+ pub updated : i64 ,
125
+ /// gist privacy
126
+ pub privacy : GistPrivacy ,
127
+ }
128
+
129
+ #[ derive( Clone , Debug ) ]
130
+ /// Represents a comment on a Gist
131
+ pub struct GistComment {
132
+ /// Unique identifier, possible database assigned, auto-incremented ID
133
+ pub id : i64 ,
134
+ /// owner of the comment
135
+ pub owner : String ,
136
+ /// public ID of the gist on which this comment was made
137
+ pub gist_public_id : String ,
138
+ /// comment text
139
+ pub comment : String ,
140
+ /// comment creation time
141
+ pub created : i64 ,
142
+ }
143
+
144
+ #[ derive( Clone , Debug ) ]
145
+ /// Data required to create a comment on a Gist
146
+ /// creation date defaults to time at which creation method is called
147
+ pub struct CreateGistComment {
148
+ /// owner of the comment
149
+ pub owner : String ,
150
+ /// public ID of the gist on which this comment was made
151
+ pub gist_public_id : String ,
152
+ /// comment text
153
+ pub comment : String ,
154
+ }
155
+
54
156
/// payload to register a user with username _and_ email
55
157
pub struct EmailRegisterPayload < ' a > {
56
158
/// username of new user
@@ -118,9 +220,33 @@ pub trait GistDatabase: std::marker::Send + std::marker::Sync + CloneGistDatabas
118
220
async fn email_register ( & self , payload : & EmailRegisterPayload ) -> DBResult < ( ) > ;
119
221
/// register with username
120
222
async fn username_register ( & self , payload : & UsernameRegisterPayload ) -> DBResult < ( ) > ;
121
-
122
223
/// ping DB
123
224
async fn ping ( & self ) -> bool ;
225
+
226
+ /// Check if a Gist with the given ID exists
227
+ async fn gist_exists ( & self , public_id : & str ) -> DBResult < bool > ;
228
+ /// Create new gists
229
+ async fn new_gist ( & self , gist : & CreateGist ) -> DBResult < ( ) > ;
230
+ /// Retrieve gist from database
231
+ async fn get_gist ( & self , public_id : & str ) -> DBResult < Gist > ;
232
+
233
+ /// Retrieve gists belonging to user
234
+ async fn get_user_gists ( & self , owner : & str ) -> DBResult < Vec < Gist > > ;
235
+
236
+ /// Delete gist
237
+ async fn delete_gist ( & self , owner : & str , public_id : & str ) -> DBResult < ( ) > ;
238
+
239
+ /// Create new comment
240
+ async fn new_comment ( & self , comment : & CreateGistComment ) -> DBResult < ( ) > ;
241
+ /// Get comments on a gist
242
+ async fn get_comments_on_gist ( & self , public_id : & str ) -> DBResult < Vec < GistComment > > ;
243
+ /// Get a specific comment using its database assigned ID
244
+ async fn get_comment_by_id ( & self , id : i64 ) -> DBResult < GistComment > ;
245
+ /// Delete comment
246
+ async fn delete_comment ( & self , owner : & str , id : i64 ) -> DBResult < ( ) > ;
247
+
248
+ /// check if privacy mode exists
249
+ async fn privacy_exists ( & self , privacy : & GistPrivacy ) -> DBResult < bool > ;
124
250
}
125
251
126
252
#[ async_trait]
@@ -177,6 +303,47 @@ impl GistDatabase for Box<dyn GistDatabase> {
177
303
async fn ping ( & self ) -> bool {
178
304
( * * self ) . ping ( ) . await
179
305
}
306
+
307
+ async fn gist_exists ( & self , public_id : & str ) -> DBResult < bool > {
308
+ ( * * self ) . gist_exists ( public_id) . await
309
+ }
310
+
311
+ async fn new_gist ( & self , gist : & CreateGist ) -> DBResult < ( ) > {
312
+ ( * * self ) . new_gist ( gist) . await
313
+ }
314
+
315
+ async fn get_gist ( & self , public_id : & str ) -> DBResult < Gist > {
316
+ ( * * self ) . get_gist ( public_id) . await
317
+ }
318
+
319
+ async fn get_user_gists ( & self , owner : & str ) -> DBResult < Vec < Gist > > {
320
+ ( * * self ) . get_user_gists ( owner) . await
321
+ }
322
+
323
+
324
+ async fn delete_gist ( & self , owner : & str , public_id : & str ) -> DBResult < ( ) > {
325
+ ( * * self ) . delete_gist ( owner, public_id) . await
326
+ }
327
+
328
+ async fn new_comment ( & self , comment : & CreateGistComment ) -> DBResult < ( ) > {
329
+ ( * * self ) . new_comment ( comment) . await
330
+ }
331
+
332
+ async fn get_comments_on_gist ( & self , public_id : & str ) -> DBResult < Vec < GistComment > > {
333
+ ( * * self ) . get_comments_on_gist ( public_id) . await
334
+ }
335
+
336
+ async fn get_comment_by_id ( & self , id : i64 ) -> DBResult < GistComment > {
337
+ ( * * self ) . get_comment_by_id ( id) . await
338
+ }
339
+
340
+ async fn delete_comment ( & self , owner : & str , id : i64 ) -> DBResult < ( ) > {
341
+ ( * * self ) . delete_comment ( owner, id) . await
342
+ }
343
+
344
+ async fn privacy_exists ( & self , privacy : & GistPrivacy ) -> DBResult < bool > {
345
+ ( * * self ) . privacy_exists ( privacy) . await
346
+ }
180
347
}
181
348
182
349
/// Trait to clone GistDatabase
0 commit comments