Skip to content

Commit fd4e44f

Browse files
Centrilanp
authored andcommitted
cleanup try!(expr) -> expr? + params! macro.
1 parent 2ab4599 commit fd4e44f

File tree

2 files changed

+30
-40
lines changed

2 files changed

+30
-40
lines changed

src/github/client.rs

+28-38
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ pub const DELAY: u64 = 300;
2626

2727
type ParameterMap = BTreeMap<&'static str, String>;
2828

29+
macro_rules! params {
30+
($($key: expr => $val: expr),*) => {{
31+
let mut map = BTreeMap::<_, _>::new();
32+
$(map.insert($key, $val);)*
33+
map
34+
}};
35+
}
36+
2937
header! { (TZ, "Time-Zone") => [String] }
3038
header! { (Accept, "Accept") => [String] }
3139
header! { (RateLimitRemaining, "X-RateLimit-Remaining") => [u32] }
@@ -66,7 +74,7 @@ impl Client {
6674

6775
pub fn org_repos(&self, org: &str) -> DashResult<Vec<String>> {
6876
let url = format!("{}/orgs/{}/repos", BASE_URL, org);
69-
let vals: Vec<serde_json::Value> = try!(self.get_models(&url, None));
77+
let vals: Vec<serde_json::Value> = self.get_models(&url, None)?;
7078

7179
let mut repos = Vec::new();
7280
for v in vals {
@@ -85,40 +93,34 @@ impl Client {
8593
}
8694

8795
pub fn issues_since(&self, repo: &str, start: DateTime<Utc>) -> DashResult<Vec<IssueFromJson>> {
88-
89-
let url = format!("{}/repos/{}/issues", BASE_URL, repo);
90-
let mut params = ParameterMap::new();
91-
92-
params.insert("state", "all".to_string());
93-
params.insert("since", format!("{:?}", start));
94-
params.insert("state", "all".to_string());
95-
params.insert("per_page", format!("{}", PER_PAGE));
96-
params.insert("direction", "asc".to_string());
97-
98-
self.get_models(&url, Some(&params))
96+
self.get_models(&format!("{}/repos/{}/issues", BASE_URL, repo),
97+
Some(&params! {
98+
"state" => "all".to_string(),
99+
"since" => format!("{:?}", start),
100+
"per_page" => format!("{}", PER_PAGE),
101+
"direction" => "asc".to_string()
102+
}))
99103
}
100104

101105
pub fn comments_since(&self,
102106
repo: &str,
103107
start: DateTime<Utc>)
104108
-> DashResult<Vec<CommentFromJson>> {
105-
let url = format!("{}/repos/{}/issues/comments", BASE_URL, repo);
106-
let mut params = ParameterMap::new();
107-
108-
params.insert("sort", "created".to_string());
109-
params.insert("direction", "asc".to_string());
110-
params.insert("since", format!("{:?}", start));
111-
params.insert("per_page", format!("{}", PER_PAGE));
112-
113-
self.get_models(&url, Some(&params))
109+
self.get_models(&format!("{}/repos/{}/issues/comments", BASE_URL, repo),
110+
Some(&params! {
111+
"sort" => "created".to_string(),
112+
"direction" => "asc".to_string(),
113+
"since" => format!("{:?}", start),
114+
"per_page" => format!("{}", PER_PAGE)
115+
}))
114116
}
115117

116118
fn get_models<M: DeserializeOwned>(&self,
117119
start_url: &str,
118120
params: Option<&ParameterMap>)
119121
-> DashResult<Vec<M>> {
120122

121-
let mut res = try!(self.get(start_url, params));
123+
let mut res = self.get(start_url, params)?;
122124
let mut models = self.deserialize::<Vec<M>>(&mut res)?;
123125
while let Some(url) = Self::next_page(&res.headers) {
124126
sleep(Duration::from_millis(DELAY));
@@ -129,9 +131,7 @@ impl Client {
129131
}
130132

131133
pub fn fetch_pull_request(&self, pr_info: &PullRequestUrls) -> DashResult<PullRequestFromJson> {
132-
let url = pr_info.get("url");
133-
134-
if let Some(url) = url {
134+
if let Some(url) = pr_info.get("url") {
135135
let mut res = self.get(url, None)?;
136136
self.deserialize(&mut res)
137137
} else {
@@ -164,11 +164,7 @@ impl Client {
164164

165165
pub fn close_issue(&self, repo: &str, issue_num: i32) -> DashResult<()> {
166166
let url = format!("{}/repos/{}/issues/{}", BASE_URL, repo, issue_num);
167-
168-
let mut obj = BTreeMap::new();
169-
obj.insert("state", "closed");
170-
let payload = serde_json::to_string(&obj)?;
171-
167+
let payload = serde_json::to_string(&params!("state" => "closed"))?;
172168
let mut res = self.patch(&url, &payload)?;
173169

174170
if StatusCode::Ok != res.status {
@@ -213,10 +209,7 @@ impl Client {
213209
-> DashResult<CommentFromJson> {
214210
let url = format!("{}/repos/{}/issues/{}/comments", BASE_URL, repo, issue_num);
215211

216-
let mut obj = BTreeMap::new();
217-
obj.insert("body", text);
218-
219-
let payload = serde_json::to_string(&obj)?;
212+
let payload = serde_json::to_string(&params!("body" => text))?;
220213

221214
// FIXME propagate an error if it's a 404 or other error
222215
self.deserialize(&mut self.post(&url, &payload)?)
@@ -232,10 +225,7 @@ impl Client {
232225
repo,
233226
comment_num);
234227

235-
let mut obj = BTreeMap::new();
236-
obj.insert("body", text);
237-
238-
let payload = serde_json::to_string(&obj)?;
228+
let payload = serde_json::to_string(&params!("body" => text))?;
239229

240230
// FIXME propagate an error if it's a 404 or other error
241231
self.deserialize(&mut self.patch(&url, &payload)?)

src/github/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ pub fn record_successful_update(ingest_start: NaiveDateTime) -> DashResult<()> {
6161

6262
pub fn ingest_since(repo: &str, start: DateTime<Utc>) -> DashResult<()> {
6363
info!("fetching all {} issues and comments since {}", repo, start);
64-
let issues = try!(GH.issues_since(repo, start));
65-
let mut comments = try!(GH.comments_since(repo, start));
64+
let issues = GH.issues_since(repo, start)?;
65+
let mut comments = GH.comments_since(repo, start)?;
6666
// make sure we process the new comments in creation order
6767
comments.sort_by_key(|c| c.created_at);
6868

0 commit comments

Comments
 (0)