From 3068f18f0ceee8c8226e706017b4e37046ad3212 Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Thu, 12 Nov 2020 15:02:45 -0800 Subject: [PATCH] STYLE Clippified. After running 'cargo clippy,' a few changes were made, and then some were reverted. Honestly, 'x.len() > 0' is WAY more readable than '!x.is_empty()'. The exclamation mark gets swallowed up by the surrounding text and is hard to see. --- server/nm-store/clippy.toml | 1 + server/nm-store/src/errors.rs | 1 - .../nm-store/src/store/{store.rs => api.rs} | 29 +++++++++---------- server/nm-store/src/store/mod.rs | 6 ++-- server/nm-store/src/store/private.rs | 18 ++++++------ server/nm-store/src/structs.rs | 8 ----- 6 files changed, 27 insertions(+), 36 deletions(-) create mode 100644 server/nm-store/clippy.toml rename server/nm-store/src/store/{store.rs => api.rs} (89%) diff --git a/server/nm-store/clippy.toml b/server/nm-store/clippy.toml new file mode 100644 index 0000000..eb74b80 --- /dev/null +++ b/server/nm-store/clippy.toml @@ -0,0 +1 @@ +cognitive-complexity-threshold = 9 diff --git a/server/nm-store/src/errors.rs b/server/nm-store/src/errors.rs index b6913f7..e1e01a3 100644 --- a/server/nm-store/src/errors.rs +++ b/server/nm-store/src/errors.rs @@ -1,4 +1,3 @@ -use sqlx; use thiserror::Error; /// All the ways looking up objects can fail diff --git a/server/nm-store/src/store/store.rs b/server/nm-store/src/store/api.rs similarity index 89% rename from server/nm-store/src/store/store.rs rename to server/nm-store/src/store/api.rs index 9ecf17f..fa5e23a 100644 --- a/server/nm-store/src/store/store.rs +++ b/server/nm-store/src/store/api.rs @@ -50,7 +50,7 @@ //! it make sense to move this to a higher layer, and only provide the //! hooks for it here? //! - +#![allow(clippy::len_zero)] use crate::errors::NoteStoreError; use crate::reference_parser::build_references; use crate::store::private::*; @@ -109,7 +109,7 @@ impl NoteStore { /// we also know no backreferences to it exist, so in that case you /// get back two empty vecs. pub async fn get_page_by_title(&self, title: &str) -> NoteResult<(Vec, Vec)> { - if title.len() == 0 { + if title.is_empty() { return Err(NoteStoreError::NotFound); } @@ -131,7 +131,7 @@ impl NoteStore { let mut tx = self.0.begin().await?; let slug = generate_slug(&mut tx, title).await?; let page = create_page(&title, &slug); - let _ = insert_note(&mut tx, &page).await?; + insert_note(&mut tx, &page).await?; tx.commit().await?; Ok((vec![Note::from(page)], vec![])) @@ -157,12 +157,11 @@ impl NoteStore { let old_note_location = old_note.location; let old_note_kind = old_note.kind; - let _ = delete_note_to_note_relationship(&mut tx, &old_parent_id, ¬e_id).await?; - let _ = close_hole_for_deleted_note_relationship(&mut tx, &old_parent_id, old_note_location).await?; + delete_note_to_note_relationship(&mut tx, &old_parent_id, ¬e_id).await?; + close_hole_for_deleted_note_relationship(&mut tx, &old_parent_id, old_note_location).await?; let new_location = determine_max_child_location_for_note(&mut tx, &new_parent_id, Some(new_location)).await?; - let _ = make_room_for_new_note_relationship(&mut tx, &new_parent_id, new_location).await?; - let _ = - insert_note_to_note_relationship(&mut tx, &new_parent_id, ¬e_id, new_location, &old_note_kind).await?; + make_room_for_new_note_relationship(&mut tx, &new_parent_id, new_location).await?; + insert_note_to_note_relationship(&mut tx, &new_parent_id, ¬e_id, new_location, &old_note_kind).await?; tx.commit().await?; Ok(()) } @@ -172,10 +171,10 @@ impl NoteStore { pub async fn update_note_content(&self, note_id: &str, content: &str) -> NoteResult<()> { let references = build_references(&content); let mut tx = self.0.begin().await?; - let _ = update_note_content(&mut tx, ¬e_id, &content).await?; - let _ = delete_bulk_note_to_page_relationships(&mut tx, ¬e_id).await?; + update_note_content(&mut tx, ¬e_id, &content).await?; + delete_bulk_note_to_page_relationships(&mut tx, ¬e_id).await?; let known_reference_ids = validate_or_generate_all_found_references(&mut tx, &references).await?; - let _ = insert_bulk_note_to_page_relationships(&mut tx, ¬e_id, &known_reference_ids).await?; + insert_bulk_note_to_page_relationships(&mut tx, ¬e_id, &known_reference_ids).await?; tx.commit().await?; Ok(()) } @@ -189,13 +188,13 @@ impl NoteStore { let parent_id = note_parent_id.to_string(); if parent_id != note_id { - let _ = delete_note_to_note_relationship(&mut tx, &parent_id, ¬e_id); + delete_note_to_note_relationship(&mut tx, &parent_id, ¬e_id).await?; } // The big one: if zero parents report having an interest in this note, then it, // *and any sub-relationships*, go away. if count_existing_note_relationships(&mut tx, ¬e_id).await? == 0 { - let _ = delete_note_to_page_relationships(&mut tx, ¬e_id).await?; - let _ = delete_note(&mut tx, ¬e_id).await?; + delete_note_to_page_relationships(&mut tx, ¬e_id).await?; + delete_note(&mut tx, ¬e_id).await?; } tx.commit().await?; Ok(()) @@ -248,7 +247,7 @@ impl NoteStore { make_room_for_new_note_relationship(&mut tx, &parent_id, location).await?; insert_note_to_note_relationship(&mut tx, &parent_id, ¬e.id, location, &kind).await?; let known_reference_ids = validate_or_generate_all_found_references(&mut tx, &references).await?; - let _ = insert_bulk_note_to_page_relationships(&mut tx, ¬e.id, &known_reference_ids).await?; + insert_bulk_note_to_page_relationships(&mut tx, ¬e.id, &known_reference_ids).await?; tx.commit().await?; Ok(note.id.to_string()) } diff --git a/server/nm-store/src/store/mod.rs b/server/nm-store/src/store/mod.rs index 40f08a5..ddea00e 100644 --- a/server/nm-store/src/store/mod.rs +++ b/server/nm-store/src/store/mod.rs @@ -51,8 +51,8 @@ //! hooks for it here? //! +mod api; mod private; -mod store; -pub use crate::store::store::NoteResult; -pub use crate::store::store::NoteStore; +pub use crate::store::api::NoteResult; +pub use crate::store::api::NoteStore; diff --git a/server/nm-store/src/store/private.rs b/server/nm-store/src/store/private.rs index 10a9d06..b36f728 100644 --- a/server/nm-store/src/store/private.rs +++ b/server/nm-store/src/store/private.rs @@ -67,7 +67,7 @@ where E: Executor<'a, Database = Sqlite>, { let r: Vec = sqlx::query_as(query).bind(field).fetch_all(executor).await?; - Ok(r.into_iter().map(|z| Note::from(z)).collect()) + Ok(r.into_iter().map(Note::from).collect()) } // Select the requested page via its id. This is fairly rare; @@ -124,7 +124,7 @@ where "VALUES (?, ?, ?, ?, ?, ?);" ); - let _ = sqlx::query(insert_one_note_sql) + sqlx::query(insert_one_note_sql) .bind(¬e.id) .bind(¬e.content) .bind(note.kind.to_string()) @@ -243,7 +243,7 @@ where let update_note_content_sql = "UPDATE notes SET content = ? WHERE note_id = ?"; let count = sqlx::query(update_note_content_sql) .bind(content) - .bind(&**note_id) + .bind(note_id) .execute(executor) .await? .rows_affected(); @@ -303,7 +303,7 @@ where "values (?, ?, ?, ?)" ); - let _ = sqlx::query(insert_note_to_note_relationship_sql) + sqlx::query(insert_note_to_note_relationship_sql) .bind(parent_id) .bind(note_id) .bind(&location) @@ -327,7 +327,7 @@ where "WHERE location >= ? and parent_id = ?;" ); - let _ = sqlx::query(make_room_for_new_note_sql) + sqlx::query(make_room_for_new_note_sql) .bind(&location) .bind(parent_id) .execute(executor) @@ -402,7 +402,7 @@ where E: Executor<'a, Database = Sqlite>, { let delete_note_to_page_relationship_sql = "DELETE FROM note_page_relationships WHERE and note_id = ?;"; - let _ = sqlx::query(delete_note_to_page_relationship_sql) + sqlx::query(delete_note_to_page_relationship_sql) .bind(note_id) .execute(executor) .await?; @@ -498,7 +498,7 @@ where ); } - let _ = sqlx::query(&DELETE_NOTE_TO_PAGE_RELATIONSHIPS_SQL) + sqlx::query(&DELETE_NOTE_TO_PAGE_RELATIONSHIPS_SQL) .bind(note_id) .execute(executor) .await?; @@ -540,7 +540,7 @@ where "WHERE location > ? and parent_id = ?;" ); - let _ = sqlx::query(close_hole_for_deleted_note_sql) + sqlx::query(close_hole_for_deleted_note_sql) .bind(&location) .bind(parent_id) .execute(executor) @@ -565,7 +565,7 @@ pub(crate) async fn validate_or_generate_all_found_references( let slug = generate_slug(&mut tx, one_reference).await?; new_page.push(create_page(&one_reference, &slug)); } - let _ = insert_bulk_notes(&mut tx, &new_page).await?; + insert_bulk_notes(&mut tx, &new_page).await?; let mut all_reference_ids: Vec = found_references.iter().map(|r| r.id.clone()).collect(); all_reference_ids.append(&mut new_page.iter().map(|r| r.id.clone()).collect()); diff --git a/server/nm-store/src/structs.rs b/server/nm-store/src/structs.rs index 663a546..5dfb698 100644 --- a/server/nm-store/src/structs.rs +++ b/server/nm-store/src/structs.rs @@ -1,7 +1,5 @@ use chrono::{DateTime, Utc}; use derive_builder::Builder; -use friendly_id; -use shrinkwraprs::Shrinkwrap; use sqlx::{self, FromRow}; // Page is German for "Box," and is used both because this is @@ -50,12 +48,6 @@ macro_rules! build_conversion_enums { }; } -#[derive(Shrinkwrap, Clone)] -pub(crate) struct NoteId(pub String); - -#[derive(Shrinkwrap, Clone)] -pub(crate) struct ParentId(pub String); - // The different kinds of objects we support. build_conversion_enums!(