Skip to content

Commit 3e96bc4

Browse files
committed
Merge branch 'error_chain'
2 parents ea33e61 + 399b171 commit 3e96bc4

File tree

15 files changed

+171
-258
lines changed

15 files changed

+171
-258
lines changed

Cargo.lock

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ params = "0.2.2"
3131
libc = "0.2"
3232
hoedown = "5.0"
3333
badge = { version = "0", path = "src/web/badge" }
34+
error-chain = "0.5"
3435

3536
[dependencies.cargo]
3637
git = "https://github.com/rust-lang/cargo.git"

src/db/add_package.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
use {DocBuilderError, ChrootBuilderResult};
2+
use ChrootBuilderResult;
33
use utils::source_path;
44
use regex::Regex;
55

@@ -16,6 +16,8 @@ use semver;
1616
use postgres::Connection;
1717
use time;
1818

19+
use errors::*;
20+
1921

2022

2123
/// Adds a package into database.
@@ -26,7 +28,7 @@ pub fn add_package_into_database(conn: &Connection,
2628
res: &ChrootBuilderResult,
2729
files: Option<Json>,
2830
doc_targets: Vec<String>)
29-
-> Result<i32, DocBuilderError> {
31+
-> Result<i32> {
3032
debug!("Adding package into database");
3133
let crate_id = try!(initialize_package_in_database(&conn, &pkg));
3234
let dependencies = convert_dependencies(&pkg);
@@ -154,7 +156,7 @@ pub fn add_package_into_database(conn: &Connection,
154156
pub fn add_build_into_database(conn: &Connection,
155157
release_id: &i32,
156158
res: &ChrootBuilderResult)
157-
-> Result<i32, DocBuilderError> {
159+
-> Result<i32> {
158160
debug!("Adding build into database");
159161
let mut rows = try!(conn.query("SELECT id FROM builds WHERE rid = $1 AND rustc_version = $2",
160162
&[release_id, &res.rustc_version]));
@@ -184,7 +186,7 @@ pub fn add_build_into_database(conn: &Connection,
184186

185187
fn initialize_package_in_database(conn: &Connection,
186188
pkg: &Package)
187-
-> Result<i32, DocBuilderError> {
189+
-> Result<i32> {
188190
let mut rows = try!(conn.query("SELECT id FROM crates WHERE name = $1",
189191
&[&pkg.manifest().name()]));
190192
// insert crate into database if it is not exists
@@ -210,10 +212,10 @@ fn convert_dependencies(pkg: &Package) -> Vec<(String, String)> {
210212

211213

212214
/// Reads readme if there is any read defined in Cargo.toml of a Package
213-
fn get_readme(pkg: &Package) -> Result<Option<String>, DocBuilderError> {
215+
fn get_readme(pkg: &Package) -> Result<Option<String>> {
214216
if pkg.manifest().metadata().readme.is_some() {
215217
let readme_path = PathBuf::from(try!(source_path(&pkg)
216-
.ok_or(DocBuilderError::FileNotFound)))
218+
.ok_or("File not found")))
217219
.join(pkg.manifest().metadata().readme.clone().unwrap());
218220
let mut reader = try!(fs::File::open(readme_path).map(|f| BufReader::new(f)));
219221
let mut readme = String::new();
@@ -225,19 +227,19 @@ fn get_readme(pkg: &Package) -> Result<Option<String>, DocBuilderError> {
225227
}
226228

227229

228-
fn get_rustdoc(pkg: &Package) -> Result<Option<String>, DocBuilderError> {
230+
fn get_rustdoc(pkg: &Package) -> Result<Option<String>> {
229231
if pkg.manifest().targets()[0].src_path().is_absolute() {
230232
read_rust_doc(pkg.manifest().targets()[0].src_path())
231233
} else {
232-
let mut path = PathBuf::from(try!(source_path(&pkg).ok_or(DocBuilderError::FileNotFound)));
234+
let mut path = PathBuf::from(try!(source_path(&pkg).ok_or("File not found")));
233235
path.push(pkg.manifest().targets()[0].src_path());
234236
read_rust_doc(path.as_path())
235237
}
236238
}
237239

238240

239241
/// Reads rustdoc from library
240-
fn read_rust_doc(file_path: &Path) -> Result<Option<String>, DocBuilderError> {
242+
fn read_rust_doc(file_path: &Path) -> Result<Option<String>> {
241243
let reader = try!(fs::File::open(file_path).map(|f| BufReader::new(f)));
242244
let mut rustdoc = String::new();
243245

@@ -263,7 +265,7 @@ fn read_rust_doc(file_path: &Path) -> Result<Option<String>, DocBuilderError> {
263265
/// Get release_time, yanked and downloads from crates.io
264266
fn get_release_time_yanked_downloads
265267
(pkg: &Package)
266-
-> Result<(Option<time::Timespec>, Option<bool>, Option<i32>), DocBuilderError> {
268+
-> Result<(Option<time::Timespec>, Option<bool>, Option<i32>)> {
267269
let url = format!("https://crates.io/api/v1/crates/{}/versions",
268270
pkg.manifest().name());
269271
// FIXME: There is probably better way to do this
@@ -276,30 +278,30 @@ fn get_release_time_yanked_downloads
276278
let versions = try!(json.as_object()
277279
.and_then(|o| o.get("versions"))
278280
.and_then(|v| v.as_array())
279-
.ok_or(DocBuilderError::JsonNotObject));
281+
.ok_or("Not a JSON object"));
280282

281283
let (mut release_time, mut yanked, mut downloads) = (None, None, None);
282284

283285
for version in versions {
284-
let version = try!(version.as_object().ok_or(DocBuilderError::JsonNotObject));
286+
let version = try!(version.as_object().ok_or("Not a JSON object"));
285287
let version_num = try!(version.get("num")
286288
.and_then(|v| v.as_string())
287-
.ok_or(DocBuilderError::JsonNotObject));
289+
.ok_or("Not a JSON object"));
288290

289291
if &semver::Version::parse(version_num).unwrap() == pkg.manifest().version() {
290292
let release_time_raw = try!(version.get("created_at")
291293
.and_then(|c| c.as_string())
292-
.ok_or(DocBuilderError::JsonNotObject));
294+
.ok_or("Not a JSON object"));
293295
release_time = Some(time::strptime(release_time_raw, "%Y-%m-%dT%H:%M:%S")
294296
.unwrap()
295297
.to_timespec());
296298

297299
yanked = Some(try!(version.get("yanked")
298300
.and_then(|c| c.as_boolean())
299-
.ok_or(DocBuilderError::JsonNotObject)));
301+
.ok_or("Not a JSON object")));
300302

301303
downloads = Some(try!(version.get("downloads").and_then(|c| c.as_i64())
302-
.ok_or(DocBuilderError::JsonNotObject)) as i32);
304+
.ok_or("Not a JSON object")) as i32);
303305

304306
break;
305307
}
@@ -313,7 +315,7 @@ fn get_release_time_yanked_downloads
313315
fn add_keywords_into_database(conn: &Connection,
314316
pkg: &Package,
315317
release_id: &i32)
316-
-> Result<(), DocBuilderError> {
318+
-> Result<()> {
317319
for keyword in &pkg.manifest().metadata().keywords {
318320
let slug = slugify(&keyword);
319321
let keyword_id: i32 = {
@@ -341,7 +343,7 @@ fn add_keywords_into_database(conn: &Connection,
341343
fn add_authors_into_database(conn: &Connection,
342344
pkg: &Package,
343345
release_id: &i32)
344-
-> Result<(), DocBuilderError> {
346+
-> Result<()> {
345347

346348
let author_capture_re = Regex::new("^([^><]+)<*(.*?)>*$").unwrap();
347349
for author in &pkg.manifest().metadata().authors {
@@ -378,7 +380,7 @@ fn add_authors_into_database(conn: &Connection,
378380
fn add_owners_into_database(conn: &Connection,
379381
pkg: &Package,
380382
crate_id: &i32)
381-
-> Result<(), DocBuilderError> {
383+
-> Result<()> {
382384
// owners available in: https://crates.io/api/v1/crates/rand/owners
383385
let owners_url = format!("https://crates.io/api/v1/crates/{}/owners",
384386
&pkg.manifest().name());

src/db/file.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
77

88
use std::path::Path;
9-
use DocBuilderError;
109
use postgres::Connection;
1110
use rustc_serialize::json::{Json, ToJson};
1211
use std::fs::File;
1312
use std::io::Read;
13+
use errors::*;
1414

1515

1616

@@ -25,7 +25,7 @@ fn file_path(prefix: &str, name: &str) -> String {
2525
fn get_file_list_from_dir<P: AsRef<Path>>(path: P,
2626
prefix: &str,
2727
files: &mut Vec<String>)
28-
-> Result<(), DocBuilderError> {
28+
-> Result<()> {
2929
let path = path.as_ref();
3030

3131
for file in try!(path.read_dir()) {
@@ -44,12 +44,12 @@ fn get_file_list_from_dir<P: AsRef<Path>>(path: P,
4444
}
4545

4646

47-
pub fn get_file_list<P: AsRef<Path>>(path: P) -> Result<Vec<String>, DocBuilderError> {
47+
pub fn get_file_list<P: AsRef<Path>>(path: P) -> Result<Vec<String>> {
4848
let path = path.as_ref();
4949
let mut files: Vec<String> = Vec::new();
5050

5151
if !path.exists() {
52-
return Err(DocBuilderError::FileNotFound);
52+
return Err("File not found".into());
5353
} else if path.is_file() {
5454
path.file_name()
5555
.and_then(|name| name.to_str())
@@ -66,7 +66,7 @@ pub fn get_file_list<P: AsRef<Path>>(path: P) -> Result<Vec<String>, DocBuilderE
6666
pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
6767
prefix: &str,
6868
path: P)
69-
-> Result<Json, DocBuilderError> {
69+
-> Result<Json> {
7070
use magic::{Cookie, flags};
7171
let cookie = try!(Cookie::open(flags::MIME_TYPE));
7272
// FIXME: This is linux specific but idk any alternative
@@ -131,7 +131,7 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
131131

132132

133133

134-
fn file_list_to_json(file_list: Vec<(String, String)>) -> Result<Json, DocBuilderError> {
134+
fn file_list_to_json(file_list: Vec<(String, String)>) -> Result<Json> {
135135

136136
let mut file_list_json: Vec<Json> = Vec::new();
137137

0 commit comments

Comments
 (0)