Skip to content

Commit

Permalink
Replace deprecated try!s with ?
Browse files Browse the repository at this point in the history
Thanks to cargo-rerast, see google/rerast#30
  • Loading branch information
jyn514 committed Nov 24, 2019
1 parent d9d7a33 commit da21020
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 117 deletions.
94 changes: 47 additions & 47 deletions src/db/add_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ pub(crate) fn add_package_into_database(conn: &Connection,
has_examples: bool)
-> Result<i32> {
debug!("Adding package into database");
let crate_id = try!(initialize_package_in_database(&conn, metadata_pkg));
let crate_id = initialize_package_in_database(&conn, metadata_pkg)?;
let dependencies = convert_dependencies(metadata_pkg);
let rustdoc = get_rustdoc(metadata_pkg, source_dir).unwrap_or(None);
let readme = get_readme(metadata_pkg, source_dir).unwrap_or(None);
let (release_time, yanked, downloads) = try!(get_release_time_yanked_downloads(metadata_pkg));
let (release_time, yanked, downloads) = get_release_time_yanked_downloads(metadata_pkg)?;
let is_library = metadata_pkg.is_library();
let metadata = Metadata::from_source_dir(source_dir)?;

let release_id: i32 = {
let rows = try!(conn.query("SELECT id FROM releases WHERE crate_id = $1 AND version = $2",
&[&crate_id, &format!("{}", metadata_pkg.version)]));
let rows = conn.query("SELECT id FROM releases WHERE crate_id = $1 AND version = $2",
&[&crate_id, &format!("{}", metadata_pkg.version)])?;

if rows.len() == 0 {
let rows = try!(conn.query("INSERT INTO releases (
let rows = conn.query("INSERT INTO releases (
crate_id, version, release_time,
dependencies, target_name, yanked, build_status,
rustdoc_status, test_status, license, repository_url,
Expand Down Expand Up @@ -83,12 +83,12 @@ pub(crate) fn add_package_into_database(conn: &Connection,
&is_library,
&res.rustc_version,
&metadata_pkg.documentation,
&metadata.default_target]));
&metadata.default_target])?;
// return id
rows.get(0).get(0)

} else {
try!(conn.query("UPDATE releases
conn.query("UPDATE releases
SET release_time = $3,
dependencies = $4,
target_name = $5,
Expand Down Expand Up @@ -137,22 +137,22 @@ pub(crate) fn add_package_into_database(conn: &Connection,
&is_library,
&res.rustc_version,
&metadata_pkg.documentation,
&metadata.default_target]));
&metadata.default_target])?;
rows.get(0).get(0)
}
};


try!(add_keywords_into_database(&conn, &metadata_pkg, &release_id));
try!(add_authors_into_database(&conn, &metadata_pkg, &release_id));
try!(add_owners_into_database(&conn, &metadata_pkg, &crate_id));
add_keywords_into_database(&conn, &metadata_pkg, &release_id)?;
add_authors_into_database(&conn, &metadata_pkg, &release_id)?;
add_owners_into_database(&conn, &metadata_pkg, &crate_id)?;


// Update versions
{
let metadata_version = Version::parse(&metadata_pkg.version)?;
let mut versions: Json = try!(conn.query("SELECT versions FROM crates WHERE id = $1",
&[&crate_id]))
let mut versions: Json = conn.query("SELECT versions FROM crates WHERE id = $1",
&[&crate_id])?
.get(0)
.get(0);
if let Some(versions_array) = versions.as_array_mut() {
Expand Down Expand Up @@ -181,7 +181,7 @@ pub(crate) fn add_build_into_database(conn: &Connection,
res: &BuildResult)
-> Result<i32> {
debug!("Adding build into database");
let rows = try!(conn.query("INSERT INTO builds (rid, rustc_version,
let rows = conn.query("INSERT INTO builds (rid, rustc_version,
cratesfyi_version,
build_status, output)
VALUES ($1, $2, $3, $4, $5)
Expand All @@ -190,17 +190,17 @@ pub(crate) fn add_build_into_database(conn: &Connection,
&res.rustc_version,
&res.docsrs_version,
&res.successful,
&res.build_log]));
&res.build_log])?;
Ok(rows.get(0).get(0))
}


fn initialize_package_in_database(conn: &Connection, pkg: &MetadataPackage) -> Result<i32> {
let mut rows = try!(conn.query("SELECT id FROM crates WHERE name = $1", &[&pkg.name]));
let mut rows = conn.query("SELECT id FROM crates WHERE name = $1", &[&pkg.name])?;
// insert crate into database if it is not exists
if rows.len() == 0 {
rows = try!(conn.query("INSERT INTO crates (name) VALUES ($1) RETURNING id",
&[&pkg.name]));
rows = conn.query("INSERT INTO crates (name) VALUES ($1) RETURNING id",
&[&pkg.name])?;
}
Ok(rows.get(0).get(0))
}
Expand Down Expand Up @@ -228,9 +228,9 @@ fn get_readme(pkg: &MetadataPackage, source_dir: &Path) -> Result<Option<String>
return Ok(None);
}

let mut reader = try!(fs::File::open(readme_path).map(|f| BufReader::new(f)));
let mut reader = fs::File::open(readme_path).map(|f| BufReader::new(f))?;
let mut readme = String::new();
try!(reader.read_to_string(&mut readme));
reader.read_to_string(&mut readme)?;

if readme.is_empty() {
Ok(None)
Expand Down Expand Up @@ -259,11 +259,11 @@ fn get_rustdoc(pkg: &MetadataPackage, source_dir: &Path) -> Result<Option<String

/// Reads rustdoc from library
fn read_rust_doc(file_path: &Path) -> Result<Option<String>> {
let reader = try!(fs::File::open(file_path).map(|f| BufReader::new(f)));
let reader = fs::File::open(file_path).map(|f| BufReader::new(f))?;
let mut rustdoc = String::new();

for line in reader.lines() {
let line = try!(line);
let line = line?;
if line.starts_with("//!") {
// some lines may or may not have a space between the `//!` and the start of the text
let line = line.trim_start_matches("//!").trim_start();
Expand Down Expand Up @@ -293,40 +293,40 @@ fn get_release_time_yanked_downloads(
// FIXME: There is probably better way to do this
// and so many unwraps...
let client = Client::new();
let mut res = try!(client.get(&url[..])
let mut res = client.get(&url[..])
.header(ACCEPT, "application/json")
.send());
.send()?;
let mut body = String::new();
res.read_to_string(&mut body).unwrap();
let json = Json::from_str(&body[..]).unwrap();
let versions = try!(json.as_object()
let versions = json.as_object()
.and_then(|o| o.get("versions"))
.and_then(|v| v.as_array())
.ok_or_else(|| err_msg("Not a JSON object")));
.ok_or_else(|| err_msg("Not a JSON object"))?;

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

for version in versions {
let version = try!(version.as_object().ok_or_else(|| err_msg("Not a JSON object")));
let version_num = try!(version.get("num")
let version = version.as_object().ok_or_else(|| err_msg("Not a JSON object"))?;
let version_num = version.get("num")
.and_then(|v| v.as_string())
.ok_or_else(|| err_msg("Not a JSON object")));
.ok_or_else(|| err_msg("Not a JSON object"))?;

if semver::Version::parse(version_num).unwrap().to_string() == pkg.version {
let release_time_raw = try!(version.get("created_at")
let release_time_raw = version.get("created_at")
.and_then(|c| c.as_string())
.ok_or_else(|| err_msg("Not a JSON object")));
.ok_or_else(|| err_msg("Not a JSON object"))?;
release_time = Some(time::strptime(release_time_raw, "%Y-%m-%dT%H:%M:%S")
.unwrap()
.to_timespec());

yanked = Some(try!(version.get("yanked")
yanked = Some(version.get("yanked")
.and_then(|c| c.as_boolean())
.ok_or_else(|| err_msg("Not a JSON object"))));
.ok_or_else(|| err_msg("Not a JSON object"))?);

downloads = Some(try!(version.get("downloads")
downloads = Some(version.get("downloads")
.and_then(|c| c.as_i64())
.ok_or_else(|| err_msg("Not a JSON object"))) as i32);
.ok_or_else(|| err_msg("Not a JSON object"))? as i32);

break;
}
Expand All @@ -341,12 +341,12 @@ fn add_keywords_into_database(conn: &Connection, pkg: &MetadataPackage, release_
for keyword in &pkg.keywords {
let slug = slugify(&keyword);
let keyword_id: i32 = {
let rows = try!(conn.query("SELECT id FROM keywords WHERE slug = $1", &[&slug]));
let rows = conn.query("SELECT id FROM keywords WHERE slug = $1", &[&slug])?;
if rows.len() > 0 {
rows.get(0).get(0)
} else {
try!(conn.query("INSERT INTO keywords (name, slug) VALUES ($1, $2) RETURNING id",
&[&keyword, &slug]))
conn.query("INSERT INTO keywords (name, slug) VALUES ($1, $2) RETURNING id",
&[&keyword, &slug])?
.get(0)
.get(0)
}
Expand All @@ -372,13 +372,13 @@ fn add_authors_into_database(conn: &Connection, pkg: &MetadataPackage, release_i
let slug = slugify(&author);

let author_id: i32 = {
let rows = try!(conn.query("SELECT id FROM authors WHERE slug = $1", &[&slug]));
let rows = conn.query("SELECT id FROM authors WHERE slug = $1", &[&slug])?;
if rows.len() > 0 {
rows.get(0).get(0)
} else {
try!(conn.query("INSERT INTO authors (name, email, slug) VALUES ($1, $2, $3)
conn.query("INSERT INTO authors (name, email, slug) VALUES ($1, $2, $3)
RETURNING id",
&[&author, &email, &slug]))
&[&author, &email, &slug])?
.get(0)
.get(0)
}
Expand All @@ -400,14 +400,14 @@ fn add_owners_into_database(conn: &Connection, pkg: &MetadataPackage, crate_id:
// owners available in: https://crates.io/api/v1/crates/rand/owners
let owners_url = format!("https://crates.io/api/v1/crates/{}/owners", pkg.name);
let client = Client::new();
let mut res = try!(client.get(&owners_url[..])
let mut res = client.get(&owners_url[..])
.header(ACCEPT, "application/json")
.send());
.send()?;
// FIXME: There is probably better way to do this
// and so many unwraps...
let mut body = String::new();
res.read_to_string(&mut body).unwrap();
let json = try!(Json::from_str(&body[..]));
let json = Json::from_str(&body[..])?;

if let Some(owners) = json.as_object()
.and_then(|j| j.get("users"))
Expand Down Expand Up @@ -436,14 +436,14 @@ fn add_owners_into_database(conn: &Connection, pkg: &MetadataPackage, crate_id:
}

let owner_id: i32 = {
let rows = try!(conn.query("SELECT id FROM owners WHERE login = $1", &[&login]));
let rows = conn.query("SELECT id FROM owners WHERE login = $1", &[&login])?;
if rows.len() > 0 {
rows.get(0).get(0)
} else {
try!(conn.query("INSERT INTO owners (login, avatar, name, email)
conn.query("INSERT INTO owners (login, avatar, name, email)
VALUES ($1, $2, $3, $4)
RETURNING id",
&[&login, &avatar, &name, &email]))
&[&login, &avatar, &name, &email])?
.get(0)
.get(0)
}
Expand Down
42 changes: 21 additions & 21 deletions src/db/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ fn get_file_list_from_dir<P: AsRef<Path>>(path: P,
let path = path.as_ref();

for file in try!(path.read_dir()) {
let file = try!(file);
let file = file?;

if try!(file.file_type()).is_file() {
if file.file_type()?.is_file() {
files.push(file.path());
} else if try!(file.file_type()).is_dir() {
try!(get_file_list_from_dir(file.path(), files));
} else if file.file_type()?.is_dir() {
get_file_list_from_dir(file.path(), files)?;
}
}

Expand All @@ -49,7 +49,7 @@ pub fn get_file_list<P: AsRef<Path>>(path: P) -> Result<Vec<PathBuf>> {
} else if path.is_file() {
files.push(PathBuf::from(path.file_name().unwrap()));
} else if path.is_dir() {
try!(get_file_list_from_dir(path, &mut files));
get_file_list_from_dir(path, &mut files)?;
for file_path in &mut files {
// We want the paths in this list to not be {path}/bar.txt but just bar.txt
*file_path = PathBuf::from(file_path.strip_prefix(path).unwrap());
Expand Down Expand Up @@ -145,10 +145,10 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
path: P)
-> Result<Json> {
use magic::{Cookie, flags};
let cookie = try!(Cookie::open(flags::MIME_TYPE));
try!(cookie.load::<&str>(&[]));
let cookie = Cookie::open(flags::MIME_TYPE)?;
cookie.load::<&str>(&[])?;

let trans = try!(conn.transaction());
let trans = conn.transaction()?;

use std::collections::HashMap;
let mut file_paths_and_mimes: HashMap<PathBuf, String> = HashMap::new();
Expand All @@ -157,7 +157,7 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,

let mut rt = ::tokio::runtime::Runtime::new().unwrap();

let mut to_upload = try!(get_file_list(&path));
let mut to_upload = get_file_list(&path)?;
let mut batch_size = cmp::min(to_upload.len(), MAX_CONCURRENT_UPLOADS);
let mut currently_uploading: Vec<_> = to_upload.drain(..batch_size).collect();
let mut attempts = 0;
Expand All @@ -176,12 +176,12 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
Err(_) => continue,
};
let mut content: Vec<u8> = Vec::new();
try!(file.read_to_end(&mut content));
file.read_to_end(&mut content)?;
let bucket_path = Path::new(prefix).join(&file_path)
.into_os_string().into_string().unwrap();

let mime = {
let mime = try!(cookie.buffer(&content));
let mime = cookie.buffer(&content)?;
// css's are causing some problem in browsers
// magic will return text/plain for css file types
// convert them to text/css
Expand Down Expand Up @@ -213,15 +213,15 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
} else {
// If AWS credentials are configured, don't insert/update the database
// check if file already exists in database
let rows = try!(conn.query("SELECT COUNT(*) FROM files WHERE path = $1", &[&bucket_path]));
let rows = conn.query("SELECT COUNT(*) FROM files WHERE path = $1", &[&bucket_path])?;

if rows.get(0).get::<usize, i64>(0) == 0 {
try!(trans.query("INSERT INTO files (path, mime, content) VALUES ($1, $2, $3)",
&[&bucket_path, &mime, &content]));
trans.query("INSERT INTO files (path, mime, content) VALUES ($1, $2, $3)",
&[&bucket_path, &mime, &content])?;
} else {
try!(trans.query("UPDATE files SET mime = $2, content = $3, date_updated = NOW() \
trans.query("UPDATE files SET mime = $2, content = $3, date_updated = NOW() \
WHERE path = $1",
&[&bucket_path, &mime, &content]));
&[&bucket_path, &mime, &content])?;
}
}

Expand All @@ -248,7 +248,7 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
}
}

try!(trans.commit());
trans.commit()?;

let file_list_with_mimes: Vec<(String, PathBuf)> = file_paths_and_mimes
.into_iter()
Expand All @@ -274,12 +274,12 @@ fn file_list_to_json(file_list: Vec<(String, PathBuf)>) -> Result<Json> {
}

pub fn move_to_s3(conn: &Connection, n: usize) -> Result<usize> {
let trans = try!(conn.transaction());
let trans = conn.transaction()?;
let client = s3_client().expect("configured s3");

let rows = try!(trans.query(
let rows = trans.query(
&format!("SELECT path, mime, content FROM files WHERE content != E'in-s3' LIMIT {}", n),
&[]));
&[])?;
let count = rows.len();

let mut rt = ::tokio::runtime::Runtime::new().unwrap();
Expand Down Expand Up @@ -315,7 +315,7 @@ pub fn move_to_s3(conn: &Connection, n: usize) -> Result<usize> {
}
}

try!(trans.commit());
trans.commit()?;

Ok(count)
}
Expand Down
Loading

0 comments on commit da21020

Please sign in to comment.