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
extern crate serde_json;
use super::{Github, Result};
use std::collections::HashMap;
use url::form_urlencoded;
use users::User;
pub struct Comments<'a> {
github: &'a Github,
owner: String,
repo: String,
number: u64,
}
impl<'a> Comments<'a> {
#[doc(hidden)]
pub fn new<O, R>(github: &'a Github, owner: O, repo: R, number: u64) -> Comments<'a>
where
O: Into<String>,
R: Into<String>,
{
Comments {
github: github,
owner: owner.into(),
repo: repo.into(),
number: number,
}
}
pub fn create(&self, comment: &CommentOptions) -> Result<Comment> {
let data = serde_json::to_string(&comment)?;
self.github.post::<Comment>(&self.path(), data.as_bytes())
}
pub fn list(&self, options: &CommentListOptions) -> Result<Vec<Comment>> {
let mut uri = vec![self.path()];
if let Some(query) = options.serialize() {
uri.push(query);
}
self.github.get::<Vec<Comment>>(&uri.join("?"))
}
fn path(&self) -> String {
format!(
"/repos/{}/{}/issues/{}/comments",
self.owner,
self.repo,
self.number
)
}
}
#[derive(Debug, Deserialize)]
pub struct Comment {
pub id: u64,
pub url: String,
pub html_url: String,
pub body: String,
pub user: User,
pub created_at: String,
pub updated_at: String,
}
#[derive(Debug, Serialize)]
pub struct CommentOptions {
pub body: String,
}
#[derive(Default)]
pub struct CommentListOptions {
params: HashMap<&'static str, String>,
}
impl CommentListOptions {
pub fn builder() -> CommentListOptionsBuilder {
CommentListOptionsBuilder::new()
}
pub fn serialize(&self) -> Option<String> {
if self.params.is_empty() {
None
} else {
let encoded: String = form_urlencoded::Serializer::new(String::new())
.extend_pairs(&self.params)
.finish();
Some(encoded)
}
}
}
pub struct CommentListOptionsBuilder(CommentListOptions);
impl CommentListOptionsBuilder {
pub fn new() -> CommentListOptionsBuilder {
CommentListOptionsBuilder(CommentListOptions { ..Default::default() })
}
pub fn since<S>(&mut self, since: S) -> &mut CommentListOptionsBuilder
where
S: Into<String>,
{
self.0.params.insert("since", since.into());
self
}
pub fn build(&self) -> CommentListOptions {
CommentListOptions { params: self.0.params.clone() }
}
}