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.
This commit is contained in:
Elf M. Sternberg 2020-11-12 15:02:45 -08:00
parent e3fe863235
commit 3068f18f0c
6 changed files with 27 additions and 36 deletions

View File

@ -0,0 +1 @@
cognitive-complexity-threshold = 9

View File

@ -1,4 +1,3 @@
use sqlx;
use thiserror::Error; use thiserror::Error;
/// All the ways looking up objects can fail /// All the ways looking up objects can fail

View File

@ -50,7 +50,7 @@
//! it make sense to move this to a higher layer, and only provide the //! it make sense to move this to a higher layer, and only provide the
//! hooks for it here? //! hooks for it here?
//! //!
#![allow(clippy::len_zero)]
use crate::errors::NoteStoreError; use crate::errors::NoteStoreError;
use crate::reference_parser::build_references; use crate::reference_parser::build_references;
use crate::store::private::*; use crate::store::private::*;
@ -109,7 +109,7 @@ impl NoteStore {
/// we also know no backreferences to it exist, so in that case you /// we also know no backreferences to it exist, so in that case you
/// get back two empty vecs. /// get back two empty vecs.
pub async fn get_page_by_title(&self, title: &str) -> NoteResult<(Vec<Note>, Vec<Note>)> { pub async fn get_page_by_title(&self, title: &str) -> NoteResult<(Vec<Note>, Vec<Note>)> {
if title.len() == 0 { if title.is_empty() {
return Err(NoteStoreError::NotFound); return Err(NoteStoreError::NotFound);
} }
@ -131,7 +131,7 @@ impl NoteStore {
let mut tx = self.0.begin().await?; let mut tx = self.0.begin().await?;
let slug = generate_slug(&mut tx, title).await?; let slug = generate_slug(&mut tx, title).await?;
let page = create_page(&title, &slug); let page = create_page(&title, &slug);
let _ = insert_note(&mut tx, &page).await?; insert_note(&mut tx, &page).await?;
tx.commit().await?; tx.commit().await?;
Ok((vec![Note::from(page)], vec![])) Ok((vec![Note::from(page)], vec![]))
@ -157,12 +157,11 @@ impl NoteStore {
let old_note_location = old_note.location; let old_note_location = old_note.location;
let old_note_kind = old_note.kind; let old_note_kind = old_note.kind;
let _ = delete_note_to_note_relationship(&mut tx, &old_parent_id, &note_id).await?; delete_note_to_note_relationship(&mut tx, &old_parent_id, &note_id).await?;
let _ = close_hole_for_deleted_note_relationship(&mut tx, &old_parent_id, old_note_location).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 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?; 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, &note_id, new_location, &old_note_kind).await?;
insert_note_to_note_relationship(&mut tx, &new_parent_id, &note_id, new_location, &old_note_kind).await?;
tx.commit().await?; tx.commit().await?;
Ok(()) Ok(())
} }
@ -172,10 +171,10 @@ impl NoteStore {
pub async fn update_note_content(&self, note_id: &str, content: &str) -> NoteResult<()> { pub async fn update_note_content(&self, note_id: &str, content: &str) -> NoteResult<()> {
let references = build_references(&content); let references = build_references(&content);
let mut tx = self.0.begin().await?; let mut tx = self.0.begin().await?;
let _ = update_note_content(&mut tx, &note_id, &content).await?; update_note_content(&mut tx, &note_id, &content).await?;
let _ = delete_bulk_note_to_page_relationships(&mut tx, &note_id).await?; delete_bulk_note_to_page_relationships(&mut tx, &note_id).await?;
let known_reference_ids = validate_or_generate_all_found_references(&mut tx, &references).await?; let known_reference_ids = validate_or_generate_all_found_references(&mut tx, &references).await?;
let _ = insert_bulk_note_to_page_relationships(&mut tx, &note_id, &known_reference_ids).await?; insert_bulk_note_to_page_relationships(&mut tx, &note_id, &known_reference_ids).await?;
tx.commit().await?; tx.commit().await?;
Ok(()) Ok(())
} }
@ -189,13 +188,13 @@ impl NoteStore {
let parent_id = note_parent_id.to_string(); let parent_id = note_parent_id.to_string();
if parent_id != note_id { if parent_id != note_id {
let _ = delete_note_to_note_relationship(&mut tx, &parent_id, &note_id); delete_note_to_note_relationship(&mut tx, &parent_id, &note_id).await?;
} }
// The big one: if zero parents report having an interest in this note, then it, // The big one: if zero parents report having an interest in this note, then it,
// *and any sub-relationships*, go away. // *and any sub-relationships*, go away.
if count_existing_note_relationships(&mut tx, &note_id).await? == 0 { if count_existing_note_relationships(&mut tx, &note_id).await? == 0 {
let _ = delete_note_to_page_relationships(&mut tx, &note_id).await?; delete_note_to_page_relationships(&mut tx, &note_id).await?;
let _ = delete_note(&mut tx, &note_id).await?; delete_note(&mut tx, &note_id).await?;
} }
tx.commit().await?; tx.commit().await?;
Ok(()) Ok(())
@ -248,7 +247,7 @@ impl NoteStore {
make_room_for_new_note_relationship(&mut tx, &parent_id, location).await?; make_room_for_new_note_relationship(&mut tx, &parent_id, location).await?;
insert_note_to_note_relationship(&mut tx, &parent_id, &note.id, location, &kind).await?; insert_note_to_note_relationship(&mut tx, &parent_id, &note.id, location, &kind).await?;
let known_reference_ids = validate_or_generate_all_found_references(&mut tx, &references).await?; let known_reference_ids = validate_or_generate_all_found_references(&mut tx, &references).await?;
let _ = insert_bulk_note_to_page_relationships(&mut tx, &note.id, &known_reference_ids).await?; insert_bulk_note_to_page_relationships(&mut tx, &note.id, &known_reference_ids).await?;
tx.commit().await?; tx.commit().await?;
Ok(note.id.to_string()) Ok(note.id.to_string())
} }

View File

@ -51,8 +51,8 @@
//! hooks for it here? //! hooks for it here?
//! //!
mod api;
mod private; mod private;
mod store;
pub use crate::store::store::NoteResult; pub use crate::store::api::NoteResult;
pub use crate::store::store::NoteStore; pub use crate::store::api::NoteStore;

View File

@ -67,7 +67,7 @@ where
E: Executor<'a, Database = Sqlite>, E: Executor<'a, Database = Sqlite>,
{ {
let r: Vec<RowNote> = sqlx::query_as(query).bind(field).fetch_all(executor).await?; let r: Vec<RowNote> = 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; // Select the requested page via its id. This is fairly rare;
@ -124,7 +124,7 @@ where
"VALUES (?, ?, ?, ?, ?, ?);" "VALUES (?, ?, ?, ?, ?, ?);"
); );
let _ = sqlx::query(insert_one_note_sql) sqlx::query(insert_one_note_sql)
.bind(&note.id) .bind(&note.id)
.bind(&note.content) .bind(&note.content)
.bind(note.kind.to_string()) .bind(note.kind.to_string())
@ -243,7 +243,7 @@ where
let update_note_content_sql = "UPDATE notes SET content = ? WHERE note_id = ?"; let update_note_content_sql = "UPDATE notes SET content = ? WHERE note_id = ?";
let count = sqlx::query(update_note_content_sql) let count = sqlx::query(update_note_content_sql)
.bind(content) .bind(content)
.bind(&**note_id) .bind(note_id)
.execute(executor) .execute(executor)
.await? .await?
.rows_affected(); .rows_affected();
@ -303,7 +303,7 @@ where
"values (?, ?, ?, ?)" "values (?, ?, ?, ?)"
); );
let _ = sqlx::query(insert_note_to_note_relationship_sql) sqlx::query(insert_note_to_note_relationship_sql)
.bind(parent_id) .bind(parent_id)
.bind(note_id) .bind(note_id)
.bind(&location) .bind(&location)
@ -327,7 +327,7 @@ where
"WHERE location >= ? and parent_id = ?;" "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(&location)
.bind(parent_id) .bind(parent_id)
.execute(executor) .execute(executor)
@ -402,7 +402,7 @@ where
E: Executor<'a, Database = Sqlite>, E: Executor<'a, Database = Sqlite>,
{ {
let delete_note_to_page_relationship_sql = "DELETE FROM note_page_relationships WHERE and note_id = ?;"; 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) .bind(note_id)
.execute(executor) .execute(executor)
.await?; .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) .bind(note_id)
.execute(executor) .execute(executor)
.await?; .await?;
@ -540,7 +540,7 @@ where
"WHERE location > ? and parent_id = ?;" "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(&location)
.bind(parent_id) .bind(parent_id)
.execute(executor) .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?; let slug = generate_slug(&mut tx, one_reference).await?;
new_page.push(create_page(&one_reference, &slug)); 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<String> = found_references.iter().map(|r| r.id.clone()).collect(); let mut all_reference_ids: Vec<String> = found_references.iter().map(|r| r.id.clone()).collect();
all_reference_ids.append(&mut new_page.iter().map(|r| r.id.clone()).collect()); all_reference_ids.append(&mut new_page.iter().map(|r| r.id.clone()).collect());

View File

@ -1,7 +1,5 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use derive_builder::Builder; use derive_builder::Builder;
use friendly_id;
use shrinkwraprs::Shrinkwrap;
use sqlx::{self, FromRow}; use sqlx::{self, FromRow};
// Page is German for "Box," and is used both because this is // 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. // The different kinds of objects we support.
build_conversion_enums!( build_conversion_enums!(