From f080bf59d9dcdd95376b6c8a128070c6528e2167 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 24 Jul 2018 17:08:31 -0700 Subject: whole bunch of clippy fixes --- rust/src/api_helpers.rs | 2 +- rust/src/api_server.rs | 116 ++++++++++++++++++++++---------------------- rust/src/api_wrappers.rs | 16 +++--- rust/src/database_models.rs | 8 +-- rust/src/lib.rs | 3 +- 5 files changed, 73 insertions(+), 72 deletions(-) (limited to 'rust') diff --git a/rust/src/api_helpers.rs b/rust/src/api_helpers.rs index 14d3dd5e..f0f56a6b 100644 --- a/rust/src/api_helpers.rs +++ b/rust/src/api_helpers.rs @@ -59,7 +59,7 @@ pub fn accept_editgroup(editgroup_id: Uuid, conn: &PgConnection) -> Result { - pub fn $post_batch_handler(&self, entity_list: &Vec) -> + pub fn $post_batch_handler(&self, entity_list: &[models::$model]) -> Result> { let conn = self.db_pool.get().expect("db_pool error"); // TODO: start a transaction let mut ret: Vec = vec![]; - for entity in entity_list.into_iter() { + for entity in entity_list { ret.push(self.$post_handler(entity.clone(), Some(&conn))?); } Ok(ret) @@ -38,11 +38,11 @@ macro_rules! entity_history_handler { ($history_handler:ident, $edit_row_type:ident, $edit_table:ident) => { pub fn $history_handler( &self, - id: String, + id: &str, limit: Option, ) -> Result> { let conn = self.db_pool.get().expect("db_pool error"); - let id = fcid2uuid(&id)?; + let id = fcid2uuid(id)?; let limit = limit.unwrap_or(50); let rows: Vec<(EditgroupRow, ChangelogRow, $edit_row_type)> = editgroup::table @@ -55,9 +55,9 @@ macro_rules! entity_history_handler { let history: Vec = rows.into_iter() .map(|(eg_row, cl_row, e_row)| EntityHistoryEntry { - edit: e_row.to_model().expect("edit row to model"), - editgroup: eg_row.to_model_partial(), - changelog_entry: cl_row.to_model(), + edit: e_row.into_model().expect("edit row to model"), + editgroup: eg_row.into_model_partial(), + changelog_entry: cl_row.into_model(), }) .collect(); Ok(history) @@ -288,9 +288,9 @@ fn work_row2entity(ident: Option, rev: WorkRevRow) -> Result Result { + pub fn get_container_handler(&self, id: &str) -> Result { let conn = self.db_pool.get().expect("db_pool error"); - let id = fcid2uuid(&id)?; + let id = fcid2uuid(id)?; // TODO: handle Deletions let (ident, rev): (ContainerIdentRow, ContainerRevRow) = container_ident::table @@ -301,12 +301,12 @@ impl Server { container_row2entity(Some(ident), rev) } - pub fn lookup_container_handler(&self, issnl: String) -> Result { + pub fn lookup_container_handler(&self, issnl: &str) -> Result { let conn = self.db_pool.get().expect("db_pool error"); let (ident, rev): (ContainerIdentRow, ContainerRevRow) = container_ident::table .inner_join(container_rev::table) - .filter(container_rev::issnl.eq(&issnl)) + .filter(container_rev::issnl.eq(issnl)) .filter(container_ident::is_live.eq(true)) .filter(container_ident::redirect_id.is_null()) .first(&conn)?; @@ -314,9 +314,9 @@ impl Server { container_row2entity(Some(ident), rev) } - pub fn get_creator_handler(&self, id: String) -> Result { + pub fn get_creator_handler(&self, id: &str) -> Result { let conn = self.db_pool.get().expect("db_pool error"); - let id = fcid2uuid(&id)?; + let id = fcid2uuid(id)?; let (ident, rev): (CreatorIdentRow, CreatorRevRow) = creator_ident::table .find(id) @@ -326,12 +326,12 @@ impl Server { creator_row2entity(Some(ident), rev) } - pub fn lookup_creator_handler(&self, orcid: String) -> Result { + pub fn lookup_creator_handler(&self, orcid: &str) -> Result { let conn = self.db_pool.get().expect("db_pool error"); let (ident, rev): (CreatorIdentRow, CreatorRevRow) = creator_ident::table .inner_join(creator_rev::table) - .filter(creator_rev::orcid.eq(&orcid)) + .filter(creator_rev::orcid.eq(orcid)) .filter(creator_ident::is_live.eq(true)) .filter(creator_ident::redirect_id.is_null()) .first(&conn)?; @@ -339,9 +339,9 @@ impl Server { creator_row2entity(Some(ident), rev) } - pub fn get_creator_releases_handler(&self, id: String) -> Result> { + pub fn get_creator_releases_handler(&self, id: &str) -> Result> { let conn = self.db_pool.get().expect("db_pool error"); - let id = fcid2uuid(&id)?; + let id = fcid2uuid(id)?; // TODO: some kind of unique or group-by? let rows: Vec<(ReleaseRevRow, ReleaseIdentRow, ReleaseContribRow)> = release_rev::table @@ -357,9 +357,9 @@ impl Server { .collect() } - pub fn get_file_handler(&self, id: String) -> Result { + pub fn get_file_handler(&self, id: &str) -> Result { let conn = self.db_pool.get().expect("db_pool error"); - let id = fcid2uuid(&id)?; + let id = fcid2uuid(id)?; let (ident, rev): (FileIdentRow, FileRevRow) = file_ident::table .find(id) @@ -369,12 +369,12 @@ impl Server { file_row2entity(Some(ident), rev, &conn) } - pub fn lookup_file_handler(&self, sha1: String) -> Result { + pub fn lookup_file_handler(&self, sha1: &str) -> Result { let conn = self.db_pool.get().expect("db_pool error"); let (ident, rev): (FileIdentRow, FileRevRow) = file_ident::table .inner_join(file_rev::table) - .filter(file_rev::sha1.eq(&sha1)) + .filter(file_rev::sha1.eq(sha1)) .filter(file_ident::is_live.eq(true)) .filter(file_ident::redirect_id.is_null()) .first(&conn)?; @@ -382,9 +382,9 @@ impl Server { file_row2entity(Some(ident), rev, &conn) } - pub fn get_release_handler(&self, id: String) -> Result { + pub fn get_release_handler(&self, id: &str) -> Result { let conn = self.db_pool.get().expect("db_pool error"); - let id = fcid2uuid(&id)?; + let id = fcid2uuid(id)?; let (ident, rev): (ReleaseIdentRow, ReleaseRevRow) = release_ident::table .find(id) @@ -394,12 +394,12 @@ impl Server { release_row2entity(Some(ident), rev, &conn) } - pub fn lookup_release_handler(&self, doi: String) -> Result { + pub fn lookup_release_handler(&self, doi: &str) -> Result { let conn = self.db_pool.get().expect("db_pool error"); let (ident, rev): (ReleaseIdentRow, ReleaseRevRow) = release_ident::table .inner_join(release_rev::table) - .filter(release_rev::doi.eq(&doi)) + .filter(release_rev::doi.eq(doi)) .filter(release_ident::is_live.eq(true)) .filter(release_ident::redirect_id.is_null()) .first(&conn)?; @@ -407,9 +407,9 @@ impl Server { release_row2entity(Some(ident), rev, &conn) } - pub fn get_release_files_handler(&self, id: String) -> Result> { + pub fn get_release_files_handler(&self, id: &str) -> Result> { let conn = self.db_pool.get().expect("db_pool error"); - let id = fcid2uuid(&id)?; + let id = fcid2uuid(id)?; let rows: Vec<(FileRevRow, FileIdentRow, FileReleaseRow)> = file_rev::table .inner_join(file_ident::table) @@ -424,9 +424,9 @@ impl Server { .collect() } - pub fn get_work_handler(&self, id: String) -> Result { + pub fn get_work_handler(&self, id: &str) -> Result { let conn = self.db_pool.get().expect("db_pool error"); - let id = fcid2uuid(&id)?; + let id = fcid2uuid(id)?; let (ident, rev): (WorkIdentRow, WorkRevRow) = work_ident::table .find(id) @@ -436,9 +436,9 @@ impl Server { work_row2entity(Some(ident), rev) } - pub fn get_work_releases_handler(&self, id: String) -> Result> { + pub fn get_work_releases_handler(&self, id: &str) -> Result> { let conn = self.db_pool.get().expect("db_pool error"); - let id = fcid2uuid(&id)?; + let id = fcid2uuid(id)?; let rows: Vec<(ReleaseRevRow, ReleaseIdentRow)> = release_rev::table .inner_join(release_ident::table) @@ -492,7 +492,7 @@ impl Server { .bind::(editgroup_id) .get_result(conn)?; - edit.to_model() + edit.into_model() } pub fn create_creator_handler( @@ -533,7 +533,7 @@ impl Server { .bind::(editgroup_id) .get_result(conn)?; - edit.to_model() + edit.into_model() } pub fn create_file_handler( @@ -579,7 +579,7 @@ impl Server { let _releases: Option> = match entity.releases { None => None, Some(release_list) => { - if release_list.len() == 0 { + if release_list.is_empty() { Some(vec![]) } else { let release_rows: Vec = release_list @@ -602,7 +602,7 @@ impl Server { let _urls: Option> = match entity.urls { None => None, Some(url_list) => { - if url_list.len() == 0 { + if url_list.is_empty() { Some(vec![]) } else { let url_rows: Vec = url_list @@ -622,7 +622,7 @@ impl Server { } }; - edit.to_model() + edit.into_model() } pub fn create_release_handler( @@ -698,7 +698,7 @@ impl Server { let _refs: Option> = match entity.refs { None => None, Some(ref_list) => { - if ref_list.len() == 0 { + if ref_list.is_empty() { Some(vec![]) } else { let ref_rows: Vec = ref_list @@ -729,7 +729,7 @@ impl Server { let _contribs: Option> = match entity.contribs { None => None, Some(contrib_list) => { - if contrib_list.len() == 0 { + if contrib_list.is_empty() { Some(vec![]) } else { let contrib_rows: Vec = contrib_list @@ -754,7 +754,7 @@ impl Server { } }; - edit.to_model() + edit.into_model() } pub fn create_work_handler( @@ -793,12 +793,12 @@ impl Server { .bind::(editgroup_id) .get_result(conn)?; - edit.to_model() + edit.into_model() } - pub fn accept_editgroup_handler(&self, id: String) -> Result<()> { + pub fn accept_editgroup_handler(&self, id: &str) -> Result<()> { let conn = self.db_pool.get().expect("db_pool error"); - accept_editgroup(fcid2uuid(&id)?, &conn)?; + accept_editgroup(fcid2uuid(id)?, &conn)?; Ok(()) } @@ -823,10 +823,10 @@ impl Server { }) } - pub fn get_editgroup_handler(&self, id: String) -> Result { + pub fn get_editgroup_handler(&self, id: &str) -> Result { let conn = self.db_pool.get().expect("db_pool error"); - let id = fcid2uuid(&id)?; + let id = fcid2uuid(id)?; let row: EditgroupRow = editgroup::table.find(id).first(&conn)?; let edits = EditgroupEdits { @@ -835,7 +835,7 @@ impl Server { .filter(container_edit::editgroup_id.eq(id)) .get_results(&conn)? .into_iter() - .map(|e: ContainerEditRow| e.to_model().unwrap()) + .map(|e: ContainerEditRow| e.into_model().unwrap()) .collect(), ), creators: Some( @@ -843,7 +843,7 @@ impl Server { .filter(creator_edit::editgroup_id.eq(id)) .get_results(&conn)? .into_iter() - .map(|e: CreatorEditRow| e.to_model().unwrap()) + .map(|e: CreatorEditRow| e.into_model().unwrap()) .collect(), ), files: Some( @@ -851,7 +851,7 @@ impl Server { .filter(file_edit::editgroup_id.eq(id)) .get_results(&conn)? .into_iter() - .map(|e: FileEditRow| e.to_model().unwrap()) + .map(|e: FileEditRow| e.into_model().unwrap()) .collect(), ), releases: Some( @@ -859,7 +859,7 @@ impl Server { .filter(release_edit::editgroup_id.eq(id)) .get_results(&conn)? .into_iter() - .map(|e: ReleaseEditRow| e.to_model().unwrap()) + .map(|e: ReleaseEditRow| e.into_model().unwrap()) .collect(), ), works: Some( @@ -867,7 +867,7 @@ impl Server { .filter(work_edit::editgroup_id.eq(id)) .get_results(&conn)? .into_iter() - .map(|e: WorkEditRow| e.to_model().unwrap()) + .map(|e: WorkEditRow| e.into_model().unwrap()) .collect(), ), }; @@ -882,10 +882,10 @@ impl Server { Ok(eg) } - pub fn get_editor_handler(&self, id: String) -> Result { + pub fn get_editor_handler(&self, id: &str) -> Result { let conn = self.db_pool.get().expect("db_pool error"); - let id = fcid2uuid(&id)?; + let id = fcid2uuid(id)?; let row: EditorRow = editor::table.find(id).first(&conn)?; let ed = Editor { @@ -895,10 +895,10 @@ impl Server { Ok(ed) } - pub fn editor_changelog_get_handler(&self, id: String) -> Result> { + pub fn editor_changelog_get_handler(&self, id: &str) -> Result> { let conn = self.db_pool.get().expect("db_pool error"); - let id = fcid2uuid(&id)?; + let id = fcid2uuid(id)?; // TODO: single query let editor: EditorRow = editor::table.find(id).first(&conn)?; let changes: Vec<(ChangelogRow, EditgroupRow)> = changelog::table @@ -910,7 +910,7 @@ impl Server { .into_iter() .map(|(cl_row, eg_row)| ChangelogEntry { index: cl_row.id, - editgroup: Some(eg_row.to_model_partial()), + editgroup: Some(eg_row.into_model_partial()), editgroup_id: uuid2fcid(&cl_row.editgroup_id), timestamp: chrono::DateTime::from_utc(cl_row.timestamp, chrono::Utc), }) @@ -932,7 +932,7 @@ impl Server { .into_iter() .map(|(cl_row, eg_row)| ChangelogEntry { index: cl_row.id, - editgroup: Some(eg_row.to_model_partial()), + editgroup: Some(eg_row.into_model_partial()), editgroup_id: uuid2fcid(&cl_row.editgroup_id), timestamp: chrono::DateTime::from_utc(cl_row.timestamp, chrono::Utc), }) @@ -944,14 +944,14 @@ impl Server { let conn = self.db_pool.get().expect("db_pool error"); let cl_row: ChangelogRow = changelog::table.find(id).first(&conn)?; - let editgroup = self.get_editgroup_handler(uuid2fcid(&cl_row.editgroup_id))?; + let editgroup = self.get_editgroup_handler(&uuid2fcid(&cl_row.editgroup_id))?; - let mut entry = cl_row.to_model(); + let mut entry = cl_row.into_model(); entry.editgroup = Some(editgroup); Ok(entry) } - pub fn get_stats_handler(&self, more: Option) -> Result { + pub fn get_stats_handler(&self, more: &Option) -> Result { let conn = self.db_pool.get().expect("db_pool error"); let merged_editgroups: i64 = changelog::table diff --git a/rust/src/api_wrappers.rs b/rust/src/api_wrappers.rs index 4d5c6ddf..8be661e1 100644 --- a/rust/src/api_wrappers.rs +++ b/rust/src/api_wrappers.rs @@ -25,7 +25,7 @@ macro_rules! wrap_entity_handlers { id: String, _context: &Context, ) -> Box + Send> { - let ret = match self.$get_handler(id.clone()) { + let ret = match self.$get_handler(&id) { Ok(entity) => $get_resp::FoundEntity(entity), Err(Error(ErrorKind::Diesel(::diesel::result::Error::NotFound), _)) => @@ -95,7 +95,7 @@ macro_rules! wrap_entity_handlers { limit: Option, _context: &Context, ) -> Box + Send> { - let ret = match self.$get_history_handler(id.clone(), limit) { + let ret = match self.$get_history_handler(&id, limit) { Ok(history) => $get_history_resp::FoundEntityHistory(history), Err(Error(ErrorKind::Diesel(::diesel::result::Error::NotFound), _)) => @@ -122,7 +122,7 @@ macro_rules! wrap_lookup_handler { $idname: $idtype, _context: &Context, ) -> Box + Send> { - let ret = match self.$get_handler($idname.clone()) { + let ret = match self.$get_handler(&$idname) { Ok(entity) => $get_resp::FoundEntity(entity), Err(Error(ErrorKind::Diesel(::diesel::result::Error::NotFound), _)) => @@ -271,7 +271,7 @@ impl Api for Server { id: String, _context: &Context, ) -> Box + Send> { - let ret = match self.accept_editgroup_handler(id.clone()) { + let ret = match self.accept_editgroup_handler(&id) { Ok(()) => AcceptEditgroupResponse::MergedSuccessfully(Success { message: "horray!".to_string(), }), @@ -292,7 +292,7 @@ impl Api for Server { id: String, _context: &Context, ) -> Box + Send> { - let ret = match self.get_editgroup_handler(id.clone()) { + let ret = match self.get_editgroup_handler(&id) { Ok(entity) => GetEditgroupResponse::FoundEntity(entity), Err(Error(ErrorKind::Diesel(::diesel::result::Error::NotFound), _)) => @@ -327,7 +327,7 @@ impl Api for Server { username: String, _context: &Context, ) -> Box + Send> { - let ret = match self.editor_changelog_get_handler(username.clone()) { + let ret = match self.editor_changelog_get_handler(&username) { Ok(entries) => GetEditorChangelogResponse::FoundMergedChanges(entries), Err(Error(ErrorKind::Diesel(::diesel::result::Error::NotFound), _)) => { GetEditorChangelogResponse::NotFound(ErrorResponse { @@ -350,7 +350,7 @@ impl Api for Server { username: String, _context: &Context, ) -> Box + Send> { - let ret = match self.get_editor_handler(username.clone()) { + let ret = match self.get_editor_handler(&username) { Ok(entity) => GetEditorResponse::FoundEditor(entity), Err(Error(ErrorKind::Diesel(::diesel::result::Error::NotFound), _)) => { GetEditorResponse::NotFound(ErrorResponse { @@ -412,7 +412,7 @@ impl Api for Server { more: Option, _context: &Context, ) -> Box + Send> { - let ret = match self.get_stats_handler(more.clone()) { + let ret = match self.get_stats_handler(&more) { Ok(stats) => GetStatsResponse::Success(stats), Err(e) => { error!("{}", e); diff --git a/rust/src/database_models.rs b/rust/src/database_models.rs index 58dc4d42..f875b492 100644 --- a/rust/src/database_models.rs +++ b/rust/src/database_models.rs @@ -32,7 +32,7 @@ pub trait EntityIdentRow { } pub trait EntityEditRow { - fn to_model(self) -> Result; + fn into_model(self) -> Result; } // Helper for constructing tables @@ -53,7 +53,7 @@ macro_rules! entity_structs { impl EntityEditRow for $edit_struct { /// Go from a row (SQL model) to an API model - fn to_model(self) -> Result { + fn into_model(self) -> Result { Ok(EntityEdit { editgroup_id: uuid2fcid(&self.editgroup_id), revision: self.rev_id.map(|v| v.to_string()), @@ -291,7 +291,7 @@ pub struct EditgroupRow { impl EditgroupRow { /// Returns an Edigroup API model *without* the entity edits actually populated. Useful for, /// eg, entity history queries (where we already have the entity edit we want) - pub fn to_model_partial(self) -> Editgroup { + pub fn into_model_partial(self) -> Editgroup { Editgroup { id: Some(uuid2fcid(&self.id)), editor_id: uuid2fcid(&self.editor_id), @@ -321,7 +321,7 @@ pub struct ChangelogRow { } impl ChangelogRow { - pub fn to_model(self) -> ChangelogEntry { + pub fn into_model(self) -> ChangelogEntry { ChangelogEntry { index: self.id, editgroup_id: uuid2fcid(&self.editgroup_id), diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 86e367e4..fd871f55 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -66,7 +66,8 @@ pub fn establish_connection() -> PgConnection { dotenv().ok(); let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); - PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url)) + PgConnection::establish(&database_url) + .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) } /// Instantiate a new API server with a pooled database connection -- cgit v1.2.3