Skip to content

Commit 4c908cc

Browse files
committed
Replace try with ?
1 parent f4b7d3a commit 4c908cc

File tree

8 files changed

+358
-363
lines changed

8 files changed

+358
-363
lines changed

src/category.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ pub struct EncodableCategoryWithSubcategories {
4545
impl Category {
4646
pub fn find_by_category(conn: &GenericConnection, name: &str)
4747
-> CargoResult<Category> {
48-
let stmt = try!(conn.prepare("SELECT * FROM categories \
49-
WHERE category = $1"));
50-
let rows = try!(stmt.query(&[&name]));
48+
let stmt = conn.prepare("SELECT * FROM categories \
49+
WHERE category = $1")?;
50+
let rows = stmt.query(&[&name])?;
5151
rows.iter().next()
5252
.chain_error(|| NotFound)
5353
.map(|row| Model::from_row(&row))
5454
}
5555

5656
pub fn find_by_slug(conn: &GenericConnection, slug: &str)
5757
-> CargoResult<Category> {
58-
let stmt = try!(conn.prepare("SELECT * FROM categories \
59-
WHERE slug = LOWER($1)"));
60-
let rows = try!(stmt.query(&[&slug]));
58+
let stmt = conn.prepare("SELECT * FROM categories \
59+
WHERE slug = LOWER($1)")?;
60+
let rows = stmt.query(&[&slug])?;
6161
rows.iter().next()
6262
.chain_error(|| NotFound)
6363
.map(|row| Model::from_row(&row))
@@ -80,7 +80,7 @@ impl Category {
8080
pub fn update_crate(conn: &GenericConnection,
8181
krate: &Crate,
8282
categories: &[String]) -> CargoResult<Vec<String>> {
83-
let old_categories = try!(krate.categories(conn));
83+
let old_categories = krate.categories(conn)?;
8484
let old_categories_ids: HashSet<_> = old_categories.iter().map(|cat| {
8585
cat.id
8686
}).collect();
@@ -112,21 +112,21 @@ impl Category {
112112
.collect();
113113

114114
if !to_rm.is_empty() {
115-
try!(conn.execute("DELETE FROM crates_categories \
115+
conn.execute("DELETE FROM crates_categories \
116116
WHERE category_id = ANY($1) \
117117
AND crate_id = $2",
118-
&[&to_rm, &krate.id]));
118+
&[&to_rm, &krate.id])?;
119119
}
120120

121121
if !to_add.is_empty() {
122122
let insert: Vec<_> = to_add.into_iter().map(|id| {
123123
format!("({}, {})", krate.id, id)
124124
}).collect();
125125
let insert = insert.join(", ");
126-
try!(conn.execute(&format!("INSERT INTO crates_categories \
126+
conn.execute(&format!("INSERT INTO crates_categories \
127127
(crate_id, category_id) VALUES {}",
128-
insert),
129-
&[]));
128+
insert),
129+
&[])?;
130130
}
131131

132132
Ok(invalid_categories)
@@ -139,8 +139,8 @@ impl Category {
139139
WHERE category NOT LIKE '%::%'",
140140
Model::table_name(None::<Self>
141141
));
142-
let stmt = try!(conn.prepare(&sql));
143-
let rows = try!(stmt.query(&[]));
142+
let stmt = conn.prepare(&sql)?;
143+
let rows = stmt.query(&[])?;
144144
Ok(rows.iter().next().unwrap().get("count"))
145145
}
146146

@@ -156,7 +156,7 @@ impl Category {
156156

157157
// Collect all the top-level categories and sum up the crates_cnt of
158158
// the crates in all subcategories
159-
let stmt = try!(conn.prepare(&format!(
159+
let stmt = conn.prepare(&format!(
160160
"SELECT c.id, c.category, c.slug, c.description, c.created_at, \
161161
COALESCE (( \
162162
SELECT sum(c2.crates_cnt)::int \
@@ -167,10 +167,10 @@ impl Category {
167167
FROM categories as c \
168168
WHERE c.category NOT LIKE '%::%' {} \
169169
LIMIT $1 OFFSET $2",
170-
sort_sql
171-
)));
170+
sort_sql
171+
))?;
172172

173-
let categories: Vec<_> = try!(stmt.query(&[&limit, &offset]))
173+
let categories: Vec<_> = stmt.query(&[&limit, &offset])?
174174
.iter()
175175
.map(|row| Model::from_row(&row))
176176
.collect();
@@ -180,7 +180,7 @@ impl Category {
180180

181181
pub fn subcategories(&self, conn: &GenericConnection)
182182
-> CargoResult<Vec<Category>> {
183-
let stmt = try!(conn.prepare("\
183+
let stmt = conn.prepare("\
184184
SELECT c.id, c.category, c.slug, c.description, c.created_at, \
185185
COALESCE (( \
186186
SELECT sum(c2.crates_cnt)::int \
@@ -190,9 +190,9 @@ impl Category {
190190
), 0) as crates_cnt \
191191
FROM categories as c \
192192
WHERE c.category ILIKE $1 || '::%' \
193-
AND c.category NOT ILIKE $1 || '::%::%'"));
193+
AND c.category NOT ILIKE $1 || '::%::%'")?;
194194

195-
let rows = try!(stmt.query(&[&self.category]));
195+
let rows = stmt.query(&[&self.category])?;
196196
Ok(rows.iter().map(|r| Model::from_row(&r)).collect())
197197
}
198198
}
@@ -213,16 +213,16 @@ impl Model for Category {
213213

214214
/// Handles the `GET /categories` route.
215215
pub fn index(req: &mut Request) -> CargoResult<Response> {
216-
let conn = try!(req.tx());
217-
let (offset, limit) = try!(req.pagination(10, 100));
216+
let conn = req.tx()?;
217+
let (offset, limit) = req.pagination(10, 100)?;
218218
let query = req.query();
219219
let sort = query.get("sort").map_or("alpha", String::as_str);
220220

221-
let categories = try!(Category::toplevel(conn, sort, limit, offset));
221+
let categories = Category::toplevel(conn, sort, limit, offset)?;
222222
let categories = categories.into_iter().map(Category::encodable).collect();
223223

224224
// Query for the total count of categories
225-
let total = try!(Category::count_toplevel(conn));
225+
let total = Category::count_toplevel(conn)?;
226226

227227
#[derive(RustcEncodable)]
228228
struct R { categories: Vec<EncodableCategory>, meta: Meta }
@@ -238,9 +238,9 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
238238
/// Handles the `GET /categories/:category_id` route.
239239
pub fn show(req: &mut Request) -> CargoResult<Response> {
240240
let slug = &req.params()["category_id"];
241-
let conn = try!(req.tx());
242-
let cat = try!(Category::find_by_slug(&*conn, &slug));
243-
let subcats = try!(cat.subcategories(&*conn)).into_iter().map(|s| {
241+
let conn = req.tx()?;
242+
let cat = Category::find_by_slug(&*conn, &slug)?;
243+
let subcats = cat.subcategories(&*conn)?.into_iter().map(|s| {
244244
s.encodable()
245245
}).collect();
246246
let cat = cat.encodable();
@@ -261,10 +261,10 @@ pub fn show(req: &mut Request) -> CargoResult<Response> {
261261

262262
/// Handles the `GET /category_slugs` route.
263263
pub fn slugs(req: &mut Request) -> CargoResult<Response> {
264-
let conn = try!(req.tx());
265-
let stmt = try!(conn.prepare("SELECT slug FROM categories \
266-
ORDER BY slug"));
267-
let rows = try!(stmt.query(&[]));
264+
let conn = req.tx()?;
265+
let stmt = conn.prepare("SELECT slug FROM categories \
266+
ORDER BY slug")?;
267+
let rows = stmt.query(&[])?;
268268

269269
#[derive(RustcEncodable)]
270270
struct Slug { id: String, slug: String }

src/dependency.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ impl Dependency {
5151
-> CargoResult<Dependency> {
5252
let req = req.to_string();
5353
let features = features.join(",");
54-
let stmt = try!(conn.prepare("INSERT INTO dependencies
54+
let stmt = conn.prepare("INSERT INTO dependencies
5555
(version_id, crate_id, req, optional,
5656
default_features, features, target, kind)
5757
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
58-
RETURNING *"));
59-
let rows = try!(stmt.query(&[&version_id, &crate_id, &req,
60-
&optional, &default_features,
61-
&features, target, &(kind as i32)]));
58+
RETURNING *")?;
59+
let rows = stmt.query(&[&version_id, &crate_id, &req,
60+
&optional, &default_features,
61+
&features, target, &(kind as i32)])?;
6262
Ok(Model::from_row(&rows.iter().next().unwrap()))
6363
}
6464

src/keyword.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,27 @@ pub struct EncodableKeyword {
3131
impl Keyword {
3232
pub fn find_by_keyword(conn: &GenericConnection, name: &str)
3333
-> CargoResult<Option<Keyword>> {
34-
let stmt = try!(conn.prepare("SELECT * FROM keywords \
35-
WHERE keyword = LOWER($1)"));
36-
let rows = try!(stmt.query(&[&name]));
34+
let stmt = conn.prepare("SELECT * FROM keywords \
35+
WHERE keyword = LOWER($1)")?;
36+
let rows = stmt.query(&[&name])?;
3737
Ok(rows.iter().next().map(|r| Model::from_row(&r)))
3838
}
3939

4040
pub fn find_or_insert(conn: &GenericConnection, name: &str)
4141
-> CargoResult<Keyword> {
4242
// TODO: racy (the select then insert is not atomic)
43-
let stmt = try!(conn.prepare("SELECT * FROM keywords
44-
WHERE keyword = LOWER($1)"));
45-
for row in try!(stmt.query(&[&name])).iter() {
43+
let stmt = conn.prepare("SELECT * FROM keywords
44+
WHERE keyword = LOWER($1)")?;
45+
for row in stmt.query(&[&name])?.iter() {
4646
return Ok(Model::from_row(&row))
4747
}
4848

49-
let stmt = try!(conn.prepare("INSERT INTO keywords (keyword) VALUES (LOWER($1))
50-
RETURNING *"));
51-
let rows = try!(stmt.query(&[&name]));
52-
Ok(Model::from_row(&try!(rows.iter().next().chain_error(|| {
49+
let stmt = conn.prepare("INSERT INTO keywords (keyword) VALUES (LOWER($1))
50+
RETURNING *")?;
51+
let rows = stmt.query(&[&name])?;
52+
Ok(Model::from_row(&rows.iter().next().chain_error(|| {
5353
internal("no version returned")
54-
}))))
54+
})?))
5555
}
5656

5757
pub fn all(conn: &GenericConnection, sort: &str, limit: i64, offset: i64)
@@ -62,11 +62,11 @@ impl Keyword {
6262
_ => "ORDER BY keyword ASC",
6363
};
6464

65-
let stmt = try!(conn.prepare(&format!("SELECT * FROM keywords {}
65+
let stmt = conn.prepare(&format!("SELECT * FROM keywords {}
6666
LIMIT $1 OFFSET $2",
67-
sort_sql)));
67+
sort_sql))?;
6868

69-
let keywords: Vec<_> = try!(stmt.query(&[&limit, &offset]))
69+
let keywords: Vec<_> = stmt.query(&[&limit, &offset])?
7070
.iter()
7171
.map(|row| Model::from_row(&row))
7272
.collect();
@@ -94,14 +94,14 @@ impl Keyword {
9494
pub fn update_crate(conn: &GenericConnection,
9595
krate: &Crate,
9696
keywords: &[String]) -> CargoResult<()> {
97-
let old_kws = try!(krate.keywords(conn));
97+
let old_kws = krate.keywords(conn)?;
9898
let old_kws = old_kws.iter().map(|kw| {
9999
(&kw.keyword[..], kw)
100100
}).collect::<HashMap<_, _>>();
101-
let new_kws = try!(keywords.iter().map(|k| {
102-
let kw = try!(Keyword::find_or_insert(conn, &k));
101+
let new_kws = keywords.iter().map(|k| {
102+
let kw = Keyword::find_or_insert(conn, &k)?;
103103
Ok((&k[..], kw))
104-
}).collect::<CargoResult<HashMap<_, _>>>());
104+
}).collect::<CargoResult<HashMap<_, _>>>()?;
105105

106106
let to_rm = old_kws.iter().filter(|&(kw, _)| {
107107
!new_kws.contains_key(kw)
@@ -111,10 +111,10 @@ impl Keyword {
111111
}).map(|(_, v)| v.id).collect::<Vec<_>>();
112112

113113
if to_rm.len() > 0 {
114-
try!(conn.execute("DELETE FROM crates_keywords
114+
conn.execute("DELETE FROM crates_keywords
115115
WHERE keyword_id = ANY($1)
116116
AND crate_id = $2",
117-
&[&to_rm, &krate.id]));
117+
&[&to_rm, &krate.id])?;
118118
}
119119

120120
if to_add.len() > 0 {
@@ -123,10 +123,10 @@ impl Keyword {
123123
let id: i32 = *id;
124124
format!("({}, {})", crate_id, id)
125125
}).collect::<Vec<_>>().join(", ");
126-
try!(conn.execute(&format!("INSERT INTO crates_keywords
126+
conn.execute(&format!("INSERT INTO crates_keywords
127127
(crate_id, keyword_id) VALUES {}",
128-
insert),
129-
&[]));
128+
insert),
129+
&[])?;
130130
}
131131

132132
Ok(())
@@ -147,16 +147,16 @@ impl Model for Keyword {
147147

148148
/// Handles the `GET /keywords` route.
149149
pub fn index(req: &mut Request) -> CargoResult<Response> {
150-
let conn = try!(req.tx());
151-
let (offset, limit) = try!(req.pagination(10, 100));
150+
let conn = req.tx()?;
151+
let (offset, limit) = req.pagination(10, 100)?;
152152
let query = req.query();
153153
let sort = query.get("sort").map(|s| &s[..]).unwrap_or("alpha");
154154

155-
let keywords = try!(Keyword::all(conn, sort, limit, offset));
155+
let keywords = Keyword::all(conn, sort, limit, offset)?;
156156
let keywords = keywords.into_iter().map(Keyword::encodable).collect();
157157

158158
// Query for the total count of keywords
159-
let total = try!(Keyword::count(conn));
159+
let total = Keyword::count(conn)?;
160160

161161
#[derive(RustcEncodable)]
162162
struct R { keywords: Vec<EncodableKeyword>, meta: Meta }
@@ -172,9 +172,9 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
172172
/// Handles the `GET /keywords/:keyword_id` route.
173173
pub fn show(req: &mut Request) -> CargoResult<Response> {
174174
let name = &req.params()["keyword_id"];
175-
let conn = try!(req.tx());
176-
let kw = try!(Keyword::find_by_keyword(&*conn, &name));
177-
let kw = try!(kw.chain_error(|| NotFound));
175+
let conn = req.tx()?;
176+
let kw = Keyword::find_by_keyword(&*conn, &name)?;
177+
let kw = kw.chain_error(|| NotFound)?;
178178

179179
#[derive(RustcEncodable)]
180180
struct R { keyword: EncodableKeyword }

0 commit comments

Comments
 (0)