-
-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathgists.rs
305 lines (272 loc) · 8.76 KB
/
gists.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//! The gist API
mod list_commits;
use serde::Serialize;
use std::collections::BTreeMap;
pub use self::list_commits::ListCommitsBuilder;
use crate::{
models::gists::{Gist, GistRevision},
Octocrab, Result,
};
/// Handler for GitHub's gist API.
///
/// Created with [`Octocrab::gists`].
pub struct GistsHandler<'octo> {
crab: &'octo Octocrab,
}
impl<'octo> GistsHandler<'octo> {
pub(crate) fn new(crab: &'octo Octocrab) -> Self {
Self { crab }
}
/// Create a new gist.
///
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// let gitignore = octocrab::instance()
/// .gists()
/// .create()
/// .file("hello_world.rs", "fn main() {\n println!(\"Hello World!\");\n}")
/// // Optional Parameters
/// .description("Hello World in Rust")
/// .public(false)
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn create(&self) -> CreateGistBuilder<'octo> {
CreateGistBuilder::new(self.crab)
}
/// Update an existing gist.
///
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// let gitignore = octocrab::instance()
/// .gists()
/// .update("aa5a315d61ae9438b18d")
/// // Optional Parameters
/// .description("Updated!")
/// .file("hello_world.rs")
/// .rename_to("fibonacci.rs")
/// .with_content("fn main() {\n println!(\"I should be a Fibonacci!\");\n}")
/// .file("delete_me.rs")
/// .delete()
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn update(&self, id: impl AsRef<str>) -> UpdateGistBuilder<'octo> {
UpdateGistBuilder::new(self.crab, format!("/gists/{id}", id = id.as_ref()))
}
/// Get a single gist.
///
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// let gist = octocrab::instance().gists().get("00000000000000000000000000000000").await?;
/// # Ok(())
/// # }
/// ```
pub async fn get(&self, id: impl AsRef<str>) -> Result<Gist> {
let id = id.as_ref();
self.crab.get(format!("/gists/{id}"), None::<&()>).await
}
/// Get a single gist revision.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// let revision = octocrab::instance()
/// .gists()
/// .get_revision("00000000000000000000000000000000", "1111111111111111111111111111111111111111")
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn get_revision(
&self,
id: impl AsRef<str>,
sha1: impl AsRef<str>,
) -> Result<GistRevision> {
let id = id.as_ref();
let sha1 = sha1.as_ref();
self.crab
.get(format!("/gists/{id}/{sha1}"), None::<&()>)
.await
}
/// List commits for the specified gist.
///
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// use octocrab::params;
///
/// // Get the least active repos belonging to `owner`.
/// let page = octocrab::instance()
/// .gists()
/// .list_commits("00000000000000000000000000000000")
/// // Optional Parameters
/// .per_page(25)
/// .page(5u32)
/// // Send the request.
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn list_commits(&self, gist_id: impl Into<String>) -> list_commits::ListCommitsBuilder {
list_commits::ListCommitsBuilder::new(self, gist_id.into())
}
}
#[derive(Debug)]
pub struct CreateGistBuilder<'octo> {
crab: &'octo Octocrab,
data: CreateGist,
}
impl<'octo> CreateGistBuilder<'octo> {
pub(crate) fn new(crab: &'octo Octocrab) -> Self {
Self {
crab,
data: Default::default(),
}
}
/// Set a description for the gist to be created.
pub fn description(mut self, description: impl Into<String>) -> Self {
self.data.description = Some(description.into());
self
}
/// Set the `public` flag of the gist to be created.
pub fn public(mut self, public: bool) -> Self {
self.data.public = Some(public);
self
}
/// Add a file to the gist with `filename` and `content`.
pub fn file(mut self, filename: impl Into<String>, content: impl Into<String>) -> Self {
let file = CreateGistFile {
filename: Default::default(),
content: content.into(),
};
self.data.files.insert(filename.into(), file);
self
}
/// Send the `CreateGist` request to Github for execution.
pub async fn send(self) -> Result<Gist> {
self.crab.post("/gists", Some(&self.data)).await
}
}
#[derive(Debug, Default, Serialize)]
struct CreateGist {
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
public: Option<bool>,
files: BTreeMap<String, CreateGistFile>,
}
#[derive(Debug, Serialize)]
struct CreateGistFile {
#[serde(skip_serializing_if = "Option::is_none")]
filename: Option<String>,
content: String,
}
#[derive(Debug)]
pub struct UpdateGistBuilder<'octo> {
crab: &'octo Octocrab,
gist_path: String,
data: UpdateGist,
}
impl<'octo> UpdateGistBuilder<'octo> {
fn new(crab: &'octo Octocrab, gist_path: String) -> Self {
Self {
crab,
gist_path,
data: Default::default(),
}
}
/// Update the description of the the gist with the content provided by `description`.
pub fn description(mut self, description: impl Into<String>) -> Self {
self.data.description = Some(description.into());
self
}
/// Update the file with the `filename`.
///
/// The update operation is chosen in further calls to the returned builder.
pub fn file(self, filename: impl Into<String>) -> UpdateGistFileBuilder<'octo> {
UpdateGistFileBuilder::new(self, filename)
}
/// Send the `UpdateGist` command to Github for execution.
pub async fn send(self) -> Result<Gist> {
self.crab.patch(self.gist_path, Some(&self.data)).await
}
}
#[derive(Debug, Default, Serialize)]
struct UpdateGist {
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
files: Option<BTreeMap<String, Option<UpdateGistFile>>>,
}
#[derive(Debug, Default, Serialize)]
pub struct UpdateGistFile {
#[serde(skip_serializing_if = "Option::is_none")]
filename: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
content: Option<String>,
}
pub struct UpdateGistFileBuilder<'octo> {
builder: UpdateGistBuilder<'octo>,
filename: String,
file: Option<UpdateGistFile>,
ready: bool,
}
impl<'octo> UpdateGistFileBuilder<'octo> {
fn new(builder: UpdateGistBuilder<'octo>, filename: impl Into<String>) -> Self {
Self {
builder,
filename: filename.into(),
file: None,
ready: false,
}
}
fn build(mut self) -> UpdateGistBuilder<'octo> {
if self.ready {
self.builder
.data
.files
.get_or_insert_with(BTreeMap::new)
.insert(self.filename, self.file);
}
self.builder
}
/// Delete the file from the gist.
pub fn delete(mut self) -> UpdateGistBuilder<'octo> {
self.ready = true;
self.file = None;
self.build()
}
/// Rename the file to `filename`.
pub fn rename_to(mut self, filename: impl Into<String>) -> Self {
self.ready = true;
self.file.get_or_insert_with(Default::default).filename = Some(filename.into());
self
}
/// Update the content of the file and overwrite it with `content`.
pub fn with_content(mut self, content: impl Into<String>) -> Self {
self.ready = true;
self.file.get_or_insert_with(Default::default).content = Some(content.into());
self
}
/// Overwrite the Description of the gist with `description`.
///
/// This will finalize the update operation and will continue to operate on the gist itself.
pub fn description(self, description: impl Into<String>) -> UpdateGistBuilder<'octo> {
self.build().description(description)
}
/// Update the next file identified by `filename`.
///
/// This will finalize the update operation and will continue to operate on the gist itself.
pub fn file(self, filename: impl Into<String>) -> UpdateGistFileBuilder<'octo> {
self.build().file(filename)
}
/// Send the `UpdateGist` command to Github for execution.
///
/// This will finalize the update operation before sending.
pub async fn send(self) -> Result<Gist> {
self.build().send().await
}
}