From 2df38cd4d677c4b2e2667863bfb8fcdeafad911c Mon Sep 17 00:00:00 2001 From: Kristoffer Andersson Date: Mon, 29 Apr 2024 08:44:52 +0200 Subject: [PATCH 1/8] refactor: move person_repository to gen-services --- crates/{persons/src/port => gen-services/src}/repository.rs | 0 .../src/port => gen-services/src}/repository/person_repository.rs | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename crates/{persons/src/port => gen-services/src}/repository.rs (100%) rename crates/{persons/src/port => gen-services/src}/repository/person_repository.rs (100%) diff --git a/crates/persons/src/port/repository.rs b/crates/gen-services/src/repository.rs similarity index 100% rename from crates/persons/src/port/repository.rs rename to crates/gen-services/src/repository.rs diff --git a/crates/persons/src/port/repository/person_repository.rs b/crates/gen-services/src/repository/person_repository.rs similarity index 100% rename from crates/persons/src/port/repository/person_repository.rs rename to crates/gen-services/src/repository/person_repository.rs From 7b778c291d6465e27151fe862192f3545386977f Mon Sep 17 00:00:00 2001 From: Kristoffer Andersson Date: Mon, 29 Apr 2024 08:48:48 +0200 Subject: [PATCH 2/8] refactor: move service to gen-services --- crates/gen-services/src/lib.rs | 16 ++-------------- .../src/{repository.rs => repositories.rs} | 0 .../person_repository.rs | 0 .../service.rs => gen-services/src/services.rs} | 0 .../src/services}/error.rs | 0 .../src/services}/person_service.rs | 0 .../src/services}/person_service/tests.rs | 0 crates/persons/src/application.rs | 1 - crates/persons/src/port.rs | 1 - 9 files changed, 2 insertions(+), 16 deletions(-) rename crates/gen-services/src/{repository.rs => repositories.rs} (100%) rename crates/gen-services/src/{repository => repositories}/person_repository.rs (100%) rename crates/{persons/src/application/service.rs => gen-services/src/services.rs} (100%) rename crates/{persons/src/application/service => gen-services/src/services}/error.rs (100%) rename crates/{persons/src/application/service => gen-services/src/services}/person_service.rs (100%) rename crates/{persons/src/application/service => gen-services/src/services}/person_service/tests.rs (100%) delete mode 100644 crates/persons/src/application.rs delete mode 100644 crates/persons/src/port.rs diff --git a/crates/gen-services/src/lib.rs b/crates/gen-services/src/lib.rs index 7d12d9a..4fc67ea 100644 --- a/crates/gen-services/src/lib.rs +++ b/crates/gen-services/src/lib.rs @@ -1,14 +1,2 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} +pub mod repositories; +pub mod services; diff --git a/crates/gen-services/src/repository.rs b/crates/gen-services/src/repositories.rs similarity index 100% rename from crates/gen-services/src/repository.rs rename to crates/gen-services/src/repositories.rs diff --git a/crates/gen-services/src/repository/person_repository.rs b/crates/gen-services/src/repositories/person_repository.rs similarity index 100% rename from crates/gen-services/src/repository/person_repository.rs rename to crates/gen-services/src/repositories/person_repository.rs diff --git a/crates/persons/src/application/service.rs b/crates/gen-services/src/services.rs similarity index 100% rename from crates/persons/src/application/service.rs rename to crates/gen-services/src/services.rs diff --git a/crates/persons/src/application/service/error.rs b/crates/gen-services/src/services/error.rs similarity index 100% rename from crates/persons/src/application/service/error.rs rename to crates/gen-services/src/services/error.rs diff --git a/crates/persons/src/application/service/person_service.rs b/crates/gen-services/src/services/person_service.rs similarity index 100% rename from crates/persons/src/application/service/person_service.rs rename to crates/gen-services/src/services/person_service.rs diff --git a/crates/persons/src/application/service/person_service/tests.rs b/crates/gen-services/src/services/person_service/tests.rs similarity index 100% rename from crates/persons/src/application/service/person_service/tests.rs rename to crates/gen-services/src/services/person_service/tests.rs diff --git a/crates/persons/src/application.rs b/crates/persons/src/application.rs deleted file mode 100644 index 1f278a4..0000000 --- a/crates/persons/src/application.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod service; diff --git a/crates/persons/src/port.rs b/crates/persons/src/port.rs deleted file mode 100644 index bb787f4..0000000 --- a/crates/persons/src/port.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod repository; From 5ce9c960171b106504522884e24eeabdf03145b0 Mon Sep 17 00:00:00 2001 From: Kristoffer Andersson Date: Fri, 17 May 2024 08:33:26 +0200 Subject: [PATCH 3/8] refactor: move mem_persons to gen-services --- .../in_memory/mod.rs => gen-services/src/repositories/mem.rs} | 0 .../src/repositories/mem}/mem_persons.rs | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename crates/{persons/src/infrastructure/repository/in_memory/mod.rs => gen-services/src/repositories/mem.rs} (100%) rename crates/{persons/src/infrastructure/repository/in_memory => gen-services/src/repositories/mem}/mem_persons.rs (100%) diff --git a/crates/persons/src/infrastructure/repository/in_memory/mod.rs b/crates/gen-services/src/repositories/mem.rs similarity index 100% rename from crates/persons/src/infrastructure/repository/in_memory/mod.rs rename to crates/gen-services/src/repositories/mem.rs diff --git a/crates/persons/src/infrastructure/repository/in_memory/mem_persons.rs b/crates/gen-services/src/repositories/mem/mem_persons.rs similarity index 100% rename from crates/persons/src/infrastructure/repository/in_memory/mem_persons.rs rename to crates/gen-services/src/repositories/mem/mem_persons.rs From 72d271f14c42db2035c729c900cd96ce7993c610 Mon Sep 17 00:00:00 2001 From: Kristoffer Andersson Date: Fri, 17 May 2024 08:38:50 +0200 Subject: [PATCH 4/8] fix: update imports and types --- crates/gen-services/Cargo.toml | 5 +++++ crates/gen-services/src/repositories.rs | 1 + .../src/repositories/mem/mem_persons.rs | 2 +- crates/gen-services/src/services/error.rs | 14 ++++++++++---- crates/gen-services/src/services/person_service.rs | 7 ++++--- .../src/services/person_service/tests.rs | 4 ++-- crates/gen-types/src/entities/person.rs | 1 + 7 files changed, 24 insertions(+), 10 deletions(-) diff --git a/crates/gen-services/Cargo.toml b/crates/gen-services/Cargo.toml index b933fcd..24d288a 100644 --- a/crates/gen-services/Cargo.toml +++ b/crates/gen-services/Cargo.toml @@ -8,3 +8,8 @@ edition = "2021" doctest = false [dependencies] +# external deps +hashbrown.workspace = true + +# local deps +gen-types.workspace = true diff --git a/crates/gen-services/src/repositories.rs b/crates/gen-services/src/repositories.rs index 693b136..a020b40 100644 --- a/crates/gen-services/src/repositories.rs +++ b/crates/gen-services/src/repositories.rs @@ -1,4 +1,5 @@ mod person_repository; +pub mod mem; use std::sync::Arc; diff --git a/crates/gen-services/src/repositories/mem/mem_persons.rs b/crates/gen-services/src/repositories/mem/mem_persons.rs index b0bdd6d..5998a1e 100644 --- a/crates/gen-services/src/repositories/mem/mem_persons.rs +++ b/crates/gen-services/src/repositories/mem/mem_persons.rs @@ -2,7 +2,7 @@ use gen_types::{Person, PersonId}; use hashbrown::HashMap; use std::sync::{Arc, RwLock}; -use crate::port::repository::{PersonRepository, PersonRepositoryError, SharedPersonRepository}; +use crate::repositories::{PersonRepository, PersonRepositoryError, SharedPersonRepository}; pub struct InMemoryPersonRepo { storage: Arc>>, diff --git a/crates/gen-services/src/services/error.rs b/crates/gen-services/src/services/error.rs index c13cd73..061c0b1 100644 --- a/crates/gen-services/src/services/error.rs +++ b/crates/gen-services/src/services/error.rs @@ -1,11 +1,10 @@ use std::{error::Error, fmt::Display}; -use crate::shared_kernel; #[derive(Debug)] pub enum UseCaseError { GedcomxError(String), - IdParseError(shared_kernel::IriParseError), + IdParseError(gen_types::shared::IriParseError), IdError(gen_types::Error), } @@ -15,8 +14,8 @@ pub enum UseCaseError { // } // } -impl From for UseCaseError { - fn from(value: shared_kernel::IriParseError) -> Self { +impl From for UseCaseError { + fn from(value: gen_types::shared::IriParseError) -> Self { Self::IdParseError(value) } } @@ -25,6 +24,7 @@ impl Display for UseCaseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::GedcomxError(err) => write!(f, "gedcomx model error: {}", err), + Self::IdError(err) => f.write_fmt(format_args!("failed to parse id '{}'", err)), Self::IdParseError(err) => f.write_fmt(format_args!("failed to parse id '{}'", err)), } } @@ -38,3 +38,9 @@ impl Error for UseCaseError { } } } + +impl From for UseCaseError { + fn from(value: gen_types::Error) -> Self { + Self::IdError(value) + } +} \ No newline at end of file diff --git a/crates/gen-services/src/services/person_service.rs b/crates/gen-services/src/services/person_service.rs index d46c32a..5aa45dd 100644 --- a/crates/gen-services/src/services/person_service.rs +++ b/crates/gen-services/src/services/person_service.rs @@ -1,6 +1,7 @@ use gen_types::{Person, PersonId}; -use crate::port::repository::SharedPersonRepository; + +use crate::repositories::SharedPersonRepository; use super::UseCaseResult; @@ -55,7 +56,7 @@ impl PersonService { impl PersonService { pub fn add(&self, cmd: &AddPerson) -> UseCaseResult<()> { - let mut person = Person::with_id(cmd.id.clone())?; + let mut person = Person::new(cmd.id); if let Some(name) = &cmd.name { person = person.name(name.as_str()); } @@ -65,7 +66,7 @@ impl PersonService { } pub fn edit(&self, cmd: &EditPerson) -> UseCaseResult<()> { - let mut person = Person::with_id(cmd.id.clone())?; + let mut person = Person::new(cmd.id); // if let Some(name) = &cmd.name { // person = person.name(name.as_str()); // } diff --git a/crates/gen-services/src/services/person_service/tests.rs b/crates/gen-services/src/services/person_service/tests.rs index d1b65b4..fc1d909 100644 --- a/crates/gen-services/src/services/person_service/tests.rs +++ b/crates/gen-services/src/services/person_service/tests.rs @@ -1,6 +1,6 @@ use super::*; -use crate::infrastructure::repository::in_memory::InMemoryPersonRepo; +use crate::repositories::mem::InMemoryPersonRepo; #[test] fn adding_person_succeeds() { let repo = InMemoryPersonRepo::arc_new(); @@ -12,7 +12,7 @@ fn adding_person_succeeds() { let person = repo.get(&cmd.id).unwrap(); } #[test] -fn editing_person_succeds() { +fn editing_person_succeeds() { let repo = InMemoryPersonRepo::arc_new(); let service = PersonService::new(repo.clone()); diff --git a/crates/gen-types/src/entities/person.rs b/crates/gen-types/src/entities/person.rs index 2946013..4d9b8da 100644 --- a/crates/gen-types/src/entities/person.rs +++ b/crates/gen-types/src/entities/person.rs @@ -36,6 +36,7 @@ impl Person { ..Default::default() } } + pub fn with_id(id: I) -> Result where I: TryInto, From f239ad5052212eb10957b89b619b00eccc8de397 Mon Sep 17 00:00:00 2001 From: Kristoffer Andersson Date: Sat, 22 Jun 2024 17:49:38 +0200 Subject: [PATCH 5/8] fix: imports --- Cargo.toml | 1 - crates/ancestors-infra-json/Cargo.toml | 3 ++- .../repository/json/mem_gedcomx_repository.rs | 13 +++---------- crates/ancestors-kernel/Cargo.toml | 6 +++++- crates/ancestors-kernel/src/context.rs | 13 ++----------- crates/ancestors-kernel/src/lib.rs | 3 ++- .../ancestors-tui/src/app/components/menu.rs | 2 +- .../src/app/components/person_editor.rs | 3 +-- crates/persons/Cargo.toml | 19 ------------------- crates/persons/src/infrastructure.rs | 1 - .../persons/src/infrastructure/repository.rs | 1 - crates/persons/src/lib.rs | 4 ---- crates/persons/src/shared_kernel.rs | 3 --- 13 files changed, 16 insertions(+), 56 deletions(-) delete mode 100644 crates/persons/Cargo.toml delete mode 100644 crates/persons/src/infrastructure.rs delete mode 100644 crates/persons/src/infrastructure/repository.rs delete mode 100644 crates/persons/src/lib.rs delete mode 100644 crates/persons/src/shared_kernel.rs diff --git a/Cargo.toml b/Cargo.toml index 692afea..5c44c29 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,6 @@ ancestors-kernel = { version = "0.1.0", path = "./crates/ancestors-kernel" } gen-scraper = { path = "./crates/gen-scraper" } gen-services = { path = "./crates/gen-services" } gen-types = { path = "./crates/gen-types" } -persons = { version = "0.1.0", path = "./crates/persons" } id-ulid = { path = "./crates/id-ulid" } # non-local crates diff --git a/crates/ancestors-infra-json/Cargo.toml b/crates/ancestors-infra-json/Cargo.toml index 19346a5..c9e3393 100644 --- a/crates/ancestors-infra-json/Cargo.toml +++ b/crates/ancestors-infra-json/Cargo.toml @@ -9,5 +9,6 @@ edition = "2021" doctest = false [dependencies] -persons.workspace = true +gen-services.workspace = true +gen-types.workspace = true # gedcomx-model = { workspace = true } diff --git a/crates/ancestors-infra-json/src/repository/json/mem_gedcomx_repository.rs b/crates/ancestors-infra-json/src/repository/json/mem_gedcomx_repository.rs index 0aab1ba..36c7855 100644 --- a/crates/ancestors-infra-json/src/repository/json/mem_gedcomx_repository.rs +++ b/crates/ancestors-infra-json/src/repository/json/mem_gedcomx_repository.rs @@ -3,11 +3,8 @@ use std::{ sync::{Arc, RwLock}, }; -use persons::{ - domain::Person, - port::repository::{PersonRepository, PersonRepositoryError}, - shared_kernel::PersonId, -}; +use gen_services::repositories::{PersonRepository, PersonRepositoryError}; +use gen_types::{Person, PersonId}; // use gedcomx_model::GedcomX; #[derive(Clone)] @@ -39,11 +36,7 @@ impl PersonRepository for MemGedcomxPersonRepo { } fn save(&self, person: Person) -> Result<(), PersonRepositoryError> { - self.storage - .0 - .write() - .unwrap() - .insert(person.id().clone(), person); + self.storage.0.write().unwrap().insert(*person.id(), person); Ok(()) } } diff --git a/crates/ancestors-kernel/Cargo.toml b/crates/ancestors-kernel/Cargo.toml index 7bd5d10..2fc6796 100644 --- a/crates/ancestors-kernel/Cargo.toml +++ b/crates/ancestors-kernel/Cargo.toml @@ -9,6 +9,10 @@ edition = "2021" doctest = false [dependencies] -persons.workspace = true +# local deps +gen-services.workspace = true +gen-types.workspace = true ancestors-infra-json.workspace = true + +# external deps serde_json.workspace = true diff --git a/crates/ancestors-kernel/src/context.rs b/crates/ancestors-kernel/src/context.rs index a42a623..1c17cc9 100644 --- a/crates/ancestors-kernel/src/context.rs +++ b/crates/ancestors-kernel/src/context.rs @@ -1,26 +1,17 @@ use ancestors_infra_json::repository::json::mem_gedcomx_repository::{ MemGedcomxPersonRepo, SharedMemStorage, }; -use persons::port::repository::SharedPersonRepository; +use gen_services::repositories::SharedPersonRepository; use std::path::{Path, PathBuf}; use std::sync::{Arc, RwLock}; use std::{fs, io}; -#[derive(Clone)] +#[derive(Clone, Default)] pub struct AppContext { db: SharedMemStorage, db_path: Option, } -impl Default for AppContext { - fn default() -> Self { - Self { - db: SharedMemStorage::default(), - db_path: None, - } - } -} - impl AppContext { pub fn db(&self) -> &SharedMemStorage { &self.db diff --git a/crates/ancestors-kernel/src/lib.rs b/crates/ancestors-kernel/src/lib.rs index 0548df4..c29987f 100644 --- a/crates/ancestors-kernel/src/lib.rs +++ b/crates/ancestors-kernel/src/lib.rs @@ -1,2 +1,3 @@ pub mod context; -pub use persons as core; +pub use gen_services; +pub use gen_types; diff --git a/crates/ancestors-tui/src/app/components/menu.rs b/crates/ancestors-tui/src/app/components/menu.rs index 6a25d71..b2da632 100644 --- a/crates/ancestors-tui/src/app/components/menu.rs +++ b/crates/ancestors-tui/src/app/components/menu.rs @@ -77,7 +77,7 @@ impl Component for MenuComponent { ) -> eyre::Result<()> { let menu_titles = vec!["Ancestors", "Home", "Persons", "Quit"]; - let menu = menu_titles + let menu: Vec = menu_titles .iter() .enumerate() .map(|(index, t)| { diff --git a/crates/ancestors-tui/src/app/components/person_editor.rs b/crates/ancestors-tui/src/app/components/person_editor.rs index 7830a00..61db260 100644 --- a/crates/ancestors-tui/src/app/components/person_editor.rs +++ b/crates/ancestors-tui/src/app/components/person_editor.rs @@ -1,9 +1,8 @@ +use ancestors_kernel::gen_services::services::UpsertPerson; use ratatui::layout::{Constraint, Direction, Layout}; use ratatui::style::{Color, Modifier, Style}; use ratatui::widgets::{Cell, Row, Table}; -use ancestors_kernel::core::application::service::UpsertPerson; - use crate::app::components::shared::container::render_container; use crate::config::Config; diff --git a/crates/persons/Cargo.toml b/crates/persons/Cargo.toml deleted file mode 100644 index 6f4ecb8..0000000 --- a/crates/persons/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -name = "persons" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[lib] -doctest = false - -[dependencies] -# gedcomx-model = { workspace = true } -hashbrown.workspace = true -oxiri = { workspace = true, features = ["serde"] } -serde = { workspace = true, features = ["derive"] } -ulid.workspace = true - -# local deps -gen-types.workspace = true diff --git a/crates/persons/src/infrastructure.rs b/crates/persons/src/infrastructure.rs deleted file mode 100644 index bb787f4..0000000 --- a/crates/persons/src/infrastructure.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod repository; diff --git a/crates/persons/src/infrastructure/repository.rs b/crates/persons/src/infrastructure/repository.rs deleted file mode 100644 index fa2e126..0000000 --- a/crates/persons/src/infrastructure/repository.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod in_memory; diff --git a/crates/persons/src/lib.rs b/crates/persons/src/lib.rs deleted file mode 100644 index 490b2e6..0000000 --- a/crates/persons/src/lib.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub mod application; -pub mod infrastructure; -pub mod port; -pub mod shared_kernel; diff --git a/crates/persons/src/shared_kernel.rs b/crates/persons/src/shared_kernel.rs deleted file mode 100644 index 0b1f58c..0000000 --- a/crates/persons/src/shared_kernel.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub type IriRef = oxiri::IriRef; -pub use oxiri::IriParseError; -// pub use gedcomx_model::common::{IriParseError, IriRef}; From fec0480257f58389ea26a1a2fd887c7dd1942388 Mon Sep 17 00:00:00 2001 From: Kristoffer Andersson Date: Sat, 22 Jun 2024 17:52:32 +0200 Subject: [PATCH 6/8] refactor: use array --- crates/ancestors-tui/src/app/components/menu.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ancestors-tui/src/app/components/menu.rs b/crates/ancestors-tui/src/app/components/menu.rs index b2da632..8d37bcb 100644 --- a/crates/ancestors-tui/src/app/components/menu.rs +++ b/crates/ancestors-tui/src/app/components/menu.rs @@ -75,7 +75,7 @@ impl Component for MenuComponent { area: ratatui::prelude::Rect, in_focus: bool, ) -> eyre::Result<()> { - let menu_titles = vec!["Ancestors", "Home", "Persons", "Quit"]; + let menu_titles = &["Ancestors", "Home", "Persons", "Quit"]; let menu: Vec = menu_titles .iter() From 38a14a0516e61ec97f7e9d1aa34dc1910698903a Mon Sep 17 00:00:00 2001 From: Kristoffer Andersson Date: Sat, 22 Jun 2024 17:53:10 +0200 Subject: [PATCH 7/8] style: fix format --- crates/gen-scraper/src/scrape/process.rs | 6 ++---- crates/gen-services/src/repositories.rs | 2 +- crates/gen-services/src/services/error.rs | 3 +-- crates/gen-services/src/services/person_service.rs | 1 - crates/gen-types/src/entities/person.rs | 2 +- 5 files changed, 5 insertions(+), 9 deletions(-) diff --git a/crates/gen-scraper/src/scrape/process.rs b/crates/gen-scraper/src/scrape/process.rs index da07db8..56a8cf0 100644 --- a/crates/gen-scraper/src/scrape/process.rs +++ b/crates/gen-scraper/src/scrape/process.rs @@ -1,6 +1,3 @@ - - - use gen_types::{ value_objects::{Fact, FactType}, Batch, Household, Person, @@ -65,7 +62,8 @@ fn extract_person( .unwrap(); let post_date = post_type .split(' ') - .map(|s| s.trim()).find(|s| s.chars().all(|c| c.is_ascii_digit())) + .map(|s| s.trim()) + .find(|s| s.chars().all(|c| c.is_ascii_digit())) .unwrap(); // let year: i32 = year_str.parse().unwrap(); // let post_date = NaiveDate::from_ymd_opt(year, 12, 31).unwrap(); diff --git a/crates/gen-services/src/repositories.rs b/crates/gen-services/src/repositories.rs index a020b40..061406f 100644 --- a/crates/gen-services/src/repositories.rs +++ b/crates/gen-services/src/repositories.rs @@ -1,5 +1,5 @@ -mod person_repository; pub mod mem; +mod person_repository; use std::sync::Arc; diff --git a/crates/gen-services/src/services/error.rs b/crates/gen-services/src/services/error.rs index 061c0b1..25a79f7 100644 --- a/crates/gen-services/src/services/error.rs +++ b/crates/gen-services/src/services/error.rs @@ -1,6 +1,5 @@ use std::{error::Error, fmt::Display}; - #[derive(Debug)] pub enum UseCaseError { GedcomxError(String), @@ -43,4 +42,4 @@ impl From for UseCaseError { fn from(value: gen_types::Error) -> Self { Self::IdError(value) } -} \ No newline at end of file +} diff --git a/crates/gen-services/src/services/person_service.rs b/crates/gen-services/src/services/person_service.rs index 5aa45dd..a0bc378 100644 --- a/crates/gen-services/src/services/person_service.rs +++ b/crates/gen-services/src/services/person_service.rs @@ -1,6 +1,5 @@ use gen_types::{Person, PersonId}; - use crate::repositories::SharedPersonRepository; use super::UseCaseResult; diff --git a/crates/gen-types/src/entities/person.rs b/crates/gen-types/src/entities/person.rs index 4d9b8da..66e66af 100644 --- a/crates/gen-types/src/entities/person.rs +++ b/crates/gen-types/src/entities/person.rs @@ -36,7 +36,7 @@ impl Person { ..Default::default() } } - + pub fn with_id(id: I) -> Result where I: TryInto, From 1aaf5d8fc326de8f5cb5f6dc243910546af105f3 Mon Sep 17 00:00:00 2001 From: Kristoffer Andersson Date: Sun, 23 Jun 2024 21:39:01 +0200 Subject: [PATCH 8/8] refactor: fix lints --- Cargo.toml | 7 +++++++ crates/ancestors-cli/src/cli/flags.rs | 2 -- crates/ancestors-cli/src/progress.rs | 1 - crates/ancestors-tui/Cargo.toml | 3 +++ crates/ancestors-tui/src/action.rs | 4 ++-- crates/ancestors-tui/src/app.rs | 7 +++++-- crates/ancestors-tui/src/app/components/menu.rs | 3 +-- .../src/app/components/person_editor.rs | 2 +- crates/ancestors-tui/src/app/components/status.rs | 9 +-------- crates/ancestors-tui/src/config.rs | 14 +++----------- crates/ancestors-tui/src/event.rs | 2 -- crates/ancestors-tui/src/kx_tui/run_app.rs | 3 +-- crates/ancestors-tui/src/kx_tui/tui.rs | 2 +- crates/gen-scraper/Cargo.toml | 3 +++ .../src/repositories/mem/mem_persons.rs | 3 ++- .../src/repositories/person_repository.rs | 4 +--- crates/gen-services/src/services/person_service.rs | 4 ++-- crates/gen-types/Cargo.toml | 3 +++ .../src/entities/place/place_reference.rs | 6 ++---- .../src/entities/source/source_citation.rs | 7 ++----- .../src/entities/source/source_description.rs | 11 ++++++++--- crates/gen-types/src/value_objects/attribution.rs | 13 ++++--------- crates/gen-types/src/value_objects/date.rs | 6 ++---- crates/gen-types/src/value_objects/name.rs | 10 +--------- .../gen-types/src/value_objects/name/name_form.rs | 7 ++----- crates/gen-types/src/value_objects/text_value.rs | 2 +- 26 files changed, 58 insertions(+), 80 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5c44c29..b42b8aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,3 +38,10 @@ tui-input = "0.8.0" ulid = "1.0.0" ureq = { version = "2.9.6", features = ["json"] } xflags = "0.3.2" + +[workspace.lints.rust] +dead_code = "allow" +unused_variables = "allow" + +[workspace.lints.clippy] +match_single_binding = "allow" diff --git a/crates/ancestors-cli/src/cli/flags.rs b/crates/ancestors-cli/src/cli/flags.rs index 573cde3..fa2535e 100644 --- a/crates/ancestors-cli/src/cli/flags.rs +++ b/crates/ancestors-cli/src/cli/flags.rs @@ -1,5 +1,3 @@ -use std::path::PathBuf; - xflags::xflags! { src "./src/cli/flags.rs" diff --git a/crates/ancestors-cli/src/progress.rs b/crates/ancestors-cli/src/progress.rs index 00e47bd..81bf9d1 100644 --- a/crates/ancestors-cli/src/progress.rs +++ b/crates/ancestors-cli/src/progress.rs @@ -1,5 +1,4 @@ //! Various `prodash` types along with various utilities for comfort. -use std::io; // #[cfg(feature = "progress-unit-bytes")] // pub use bytesize; diff --git a/crates/ancestors-tui/Cargo.toml b/crates/ancestors-tui/Cargo.toml index f32142a..ce3569f 100644 --- a/crates/ancestors-tui/Cargo.toml +++ b/crates/ancestors-tui/Cargo.toml @@ -25,3 +25,6 @@ tui-input.workspace = true # local deps ancestors-kernel.workspace = true + +[lints] +workspace = true diff --git a/crates/ancestors-tui/src/action.rs b/crates/ancestors-tui/src/action.rs index 94fa7fe..ef8212e 100644 --- a/crates/ancestors-tui/src/action.rs +++ b/crates/ancestors-tui/src/action.rs @@ -47,13 +47,13 @@ impl<'de> Deserialize<'de> for Action { "Help" => Ok(Action::Help), "AddPerson" => Ok(Action::AddPerson), data if data.starts_with("Error(") => { - let error_msg = data.trim_start_matches("Error(").trim_end_matches(")"); + let error_msg = data.trim_start_matches("Error(").trim_end_matches(')'); Ok(Action::Error(error_msg.to_string())) } data if data.starts_with("Resize(") => { let parts: Vec<&str> = data .trim_start_matches("Resize(") - .trim_end_matches(")") + .trim_end_matches(')') .split(',') .collect(); if parts.len() == 2 { diff --git a/crates/ancestors-tui/src/app.rs b/crates/ancestors-tui/src/app.rs index a330501..965ebfb 100644 --- a/crates/ancestors-tui/src/app.rs +++ b/crates/ancestors-tui/src/app.rs @@ -1,3 +1,5 @@ +#![allow(clippy::single_match)] +#![allow(clippy::match_single_binding)] pub mod components; use ancestors_kernel::context::AppContext; @@ -24,6 +26,7 @@ pub struct App { pub ctx: AppContext, } +#[allow(dead_code)] pub struct AppComponent { config: Config, menu: MenuComponent, @@ -62,7 +65,7 @@ impl App { self.should_quit = true; } - pub fn load_file(&mut self, filename: String) { + pub fn load_file(&mut self, _filename: String) { // self.current_screen = CurrentScreen::Main; } pub fn increment_counter(&mut self) { @@ -185,7 +188,7 @@ impl AppComponent { Ok(None) } - fn move_focus(&mut self, event: InputEvent) -> eyre::Result> { + fn move_focus(&mut self, _event: InputEvent) -> eyre::Result> { match self.mode { // Mode::Menu => { // if matches!(event, InputEvent::Key(key) if key.code == KeyCode::Esc) { diff --git a/crates/ancestors-tui/src/app/components/menu.rs b/crates/ancestors-tui/src/app/components/menu.rs index 8d37bcb..6b3c7c5 100644 --- a/crates/ancestors-tui/src/app/components/menu.rs +++ b/crates/ancestors-tui/src/app/components/menu.rs @@ -1,4 +1,3 @@ -use crossterm::event::KeyCode; use ratatui::{ style::{Color, Modifier, Style}, text::{Line, Span}, @@ -6,7 +5,7 @@ use ratatui::{ Frame, }; -use crate::{action::Action, config::Config, event::EventState, mode::Mode}; +use crate::{action::Action, config::Config, mode::Mode}; use super::shared::{constants::HIGHLIGHT_COLOR, container::render_container}; use crate::kx_tui::Component; diff --git a/crates/ancestors-tui/src/app/components/person_editor.rs b/crates/ancestors-tui/src/app/components/person_editor.rs index 61db260..dc99b1e 100644 --- a/crates/ancestors-tui/src/app/components/person_editor.rs +++ b/crates/ancestors-tui/src/app/components/person_editor.rs @@ -51,7 +51,7 @@ impl Component for PersonEditorComponent { area: ratatui::prelude::Rect, in_focus: bool, ) -> eyre::Result<()> { - let mut chunks = Layout::default() + let chunks = Layout::default() .direction(Direction::Vertical) .constraints([Constraint::Percentage(100), Constraint::Percentage(0)]) .split(area); diff --git a/crates/ancestors-tui/src/app/components/status.rs b/crates/ancestors-tui/src/app/components/status.rs index 58da80d..6126cc6 100644 --- a/crates/ancestors-tui/src/app/components/status.rs +++ b/crates/ancestors-tui/src/app/components/status.rs @@ -3,18 +3,11 @@ use ratatui::widgets::Paragraph; use super::shared::container::render_container; use crate::kx_tui::Component; +#[derive(Debug, Clone, Default)] pub struct StatusComponent { status: String, } -impl Default for StatusComponent { - fn default() -> Self { - Self { - status: Default::default(), - } - } -} - impl StatusComponent { pub fn write_status>(&mut self, status: S) { self.status = status.into(); diff --git a/crates/ancestors-tui/src/config.rs b/crates/ancestors-tui/src/config.rs index 0f1319d..9ce1030 100644 --- a/crates/ancestors-tui/src/config.rs +++ b/crates/ancestors-tui/src/config.rs @@ -1,19 +1,11 @@ -use std::{ - collections::HashMap, - fmt, - path::{Path, PathBuf}, -}; +use std::{collections::HashMap, path::Path}; use eyre::Result; // use color_eyre::eyre::Result; -use config::Value; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; // use derive_deref::{Deref, DerefMut}; // use ratatui::style::{Color, Modifier, Style}; -use serde::{ - de::{self, Deserializer, MapAccess, Visitor}, - Deserialize, Serialize, -}; +use serde::{de::Deserializer, Deserialize}; // use serde_json::Value as JsonValue; use crate::{action::Action, mode::Mode}; @@ -235,7 +227,7 @@ pub fn key_event_to_string(key_event: &KeyEvent) -> String { char = format!("f({c})"); &char } - KeyCode::Char(c) if c == ' ' => "space", + KeyCode::Char(' ') => "space", KeyCode::Char(c) => { char = c.to_string(); &char diff --git a/crates/ancestors-tui/src/event.rs b/crates/ancestors-tui/src/event.rs index 3bfed33..c7f081e 100644 --- a/crates/ancestors-tui/src/event.rs +++ b/crates/ancestors-tui/src/event.rs @@ -1,12 +1,10 @@ use std::{ - io, sync::mpsc, thread, time::{Duration, Instant}, }; use crossterm::event::{self, Event as CrosstermEvent, KeyEvent, MouseEvent}; -use eyre::Result; /// Terminal events. #[derive(Clone, Copy, Debug)] diff --git a/crates/ancestors-tui/src/kx_tui/run_app.rs b/crates/ancestors-tui/src/kx_tui/run_app.rs index adb976c..3556635 100644 --- a/crates/ancestors-tui/src/kx_tui/run_app.rs +++ b/crates/ancestors-tui/src/kx_tui/run_app.rs @@ -43,8 +43,7 @@ pub fn run_app(t: &mut Tui, app: &mut dyn Component) -> eyre::Result<()> { Event::Tick => Some(Action::Tick), Event::Input(event) => match app.handle_event(event) { Ok(action) => { - if !action.is_some() { - todo!("what to do here?"); + if action.is_none() { Some(Action::Quit) } else { action diff --git a/crates/ancestors-tui/src/kx_tui/tui.rs b/crates/ancestors-tui/src/kx_tui/tui.rs index ddb1531..d439624 100644 --- a/crates/ancestors-tui/src/kx_tui/tui.rs +++ b/crates/ancestors-tui/src/kx_tui/tui.rs @@ -10,7 +10,7 @@ use eyre::Result; pub use ratatui::Frame; pub type CrosstermTerminal = ratatui::Terminal>; -use crate::{app::App, event::EventHandler}; +use crate::event::EventHandler; /// Representation of a terminal user interface. /// diff --git a/crates/gen-scraper/Cargo.toml b/crates/gen-scraper/Cargo.toml index d543726..f41268f 100644 --- a/crates/gen-scraper/Cargo.toml +++ b/crates/gen-scraper/Cargo.toml @@ -16,3 +16,6 @@ ureq = { workspace = true, features = ["json"] } # local deps gen-types.workspace = true + +[lints] +workspace = true diff --git a/crates/gen-services/src/repositories/mem/mem_persons.rs b/crates/gen-services/src/repositories/mem/mem_persons.rs index 5998a1e..18fa6fe 100644 --- a/crates/gen-services/src/repositories/mem/mem_persons.rs +++ b/crates/gen-services/src/repositories/mem/mem_persons.rs @@ -4,6 +4,7 @@ use std::sync::{Arc, RwLock}; use crate::repositories::{PersonRepository, PersonRepositoryError, SharedPersonRepository}; +#[derive(Default)] pub struct InMemoryPersonRepo { storage: Arc>>, } @@ -28,7 +29,7 @@ impl PersonRepository for InMemoryPersonRepo { self.storage .write() .unwrap() - .insert(person.id().clone(), person.clone()); + .insert(*person.id(), person.clone()); Ok(()) } } diff --git a/crates/gen-services/src/repositories/person_repository.rs b/crates/gen-services/src/repositories/person_repository.rs index 6a3873b..d5b1887 100644 --- a/crates/gen-services/src/repositories/person_repository.rs +++ b/crates/gen-services/src/repositories/person_repository.rs @@ -22,8 +22,6 @@ impl fmt::Display for PersonRepositoryError { impl error::Error for PersonRepositoryError { fn source(&self) -> Option<&(dyn error::Error + 'static)> { - match self { - _ => None, - } + None } } diff --git a/crates/gen-services/src/services/person_service.rs b/crates/gen-services/src/services/person_service.rs index a0bc378..6b7fedf 100644 --- a/crates/gen-services/src/services/person_service.rs +++ b/crates/gen-services/src/services/person_service.rs @@ -65,7 +65,7 @@ impl PersonService { } pub fn edit(&self, cmd: &EditPerson) -> UseCaseResult<()> { - let mut person = Person::new(cmd.id); + let person = Person::new(cmd.id); // if let Some(name) = &cmd.name { // person = person.name(name.as_str()); // } @@ -85,7 +85,7 @@ pub struct EditPerson { impl From for EditPerson { fn from(value: Person) -> Self { Self { - id: value.id().clone(), + id: *value.id(), // name: None, // extracted: true, } diff --git a/crates/gen-types/Cargo.toml b/crates/gen-types/Cargo.toml index 8d94ae9..8f15716 100644 --- a/crates/gen-types/Cargo.toml +++ b/crates/gen-types/Cargo.toml @@ -18,3 +18,6 @@ serde_with = { version = "3.6.1", features = ["chrono"] } [dev-dependencies] eyre.workspace = true + +[lints] +workspace = true diff --git a/crates/gen-types/src/entities/place/place_reference.rs b/crates/gen-types/src/entities/place/place_reference.rs index d6fb7a4..ebdf708 100644 --- a/crates/gen-types/src/entities/place/place_reference.rs +++ b/crates/gen-types/src/entities/place/place_reference.rs @@ -1,14 +1,12 @@ /// A place conclusion -#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)] +#[derive(Debug, Clone, Default, PartialEq, serde::Deserialize, serde::Serialize)] pub struct PlaceReference { original: String, } impl PlaceReference { pub fn new() -> Self { - Self { - original: String::new(), - } + Self::default() } } diff --git a/crates/gen-types/src/entities/source/source_citation.rs b/crates/gen-types/src/entities/source/source_citation.rs index 136dc84..17144d0 100644 --- a/crates/gen-types/src/entities/source/source_citation.rs +++ b/crates/gen-types/src/entities/source/source_citation.rs @@ -1,5 +1,5 @@ #[serde_with::skip_serializing_none] -#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)] +#[derive(Debug, Clone, Default, PartialEq, serde::Deserialize, serde::Serialize)] pub struct SourceCitation { value: String, lang: Option, @@ -7,10 +7,7 @@ pub struct SourceCitation { impl SourceCitation { pub fn new() -> Self { - Self { - value: String::new(), - lang: None, - } + Self::default() } } diff --git a/crates/gen-types/src/entities/source/source_description.rs b/crates/gen-types/src/entities/source/source_description.rs index e946099..afadb1f 100644 --- a/crates/gen-types/src/entities/source/source_description.rs +++ b/crates/gen-types/src/entities/source/source_description.rs @@ -1,4 +1,4 @@ -use chrono::{DateTime, Utc}; +use chrono::{DateTime, LocalResult, Utc}; use serde::{Deserialize, Deserializer, Serializer}; use crate::{ @@ -53,7 +53,12 @@ impl<'de> serde::de::Visitor<'de> for OptionalDateTimeVisitor { D: Deserializer<'de>, { use chrono::TimeZone; - Ok(Some(Utc.timestamp_millis(i64::deserialize(deserializer)?))) + match Utc.timestamp_millis_opt(i64::deserialize(deserializer)?) { + LocalResult::Single(dt) => Ok(Some(dt)), + LocalResult::None => todo!("handle error"), + LocalResult::Ambiguous(_dt1, _dt2) => todo!("handle ambiguous"), + } + // Ok(Some(Utc.timestamp_millis(i64::deserialize(deserializer)?))) } } @@ -162,6 +167,6 @@ impl SourceDescription { impl From<&SourceDescription> for SourceReference { fn from(source: &SourceDescription) -> Self { - Self::new(source.id.clone()) //, source.id.clone().into_inner()) + Self::new(source.id) //, source.id.clone().into_inner()) } } diff --git a/crates/gen-types/src/value_objects/attribution.rs b/crates/gen-types/src/value_objects/attribution.rs index c052b40..29a01d0 100644 --- a/crates/gen-types/src/value_objects/attribution.rs +++ b/crates/gen-types/src/value_objects/attribution.rs @@ -4,7 +4,7 @@ use serde_with::TimestampMilliSeconds; use crate::entities::AgentReference; #[serde_with::serde_as] -#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)] +#[derive(Debug, Clone, Default, PartialEq, serde::Deserialize, serde::Serialize)] pub struct Attribution { contributor: Option, #[serde_as(as = "TimestampMilliSeconds")] @@ -13,10 +13,7 @@ pub struct Attribution { impl Attribution { pub fn new() -> Self { - Self { - contributor: None, - modified: Utc::now(), - } + Self::default() } } @@ -60,10 +57,8 @@ pub fn verify_attribution_opt( } else { return Err(format!("a != b, '{:?}' != 'None'", a)); } - } else { - if b.is_some() { - return Err(format!("a != b, 'None' != '{:?}'", b)); - } + } else if b.is_some() { + return Err(format!("a != b, 'None' != '{:?}'", b)); } Ok(()) } diff --git a/crates/gen-types/src/value_objects/date.rs b/crates/gen-types/src/value_objects/date.rs index 793355d..d0b4dad 100644 --- a/crates/gen-types/src/value_objects/date.rs +++ b/crates/gen-types/src/value_objects/date.rs @@ -1,14 +1,12 @@ /// A name conclusion -#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)] +#[derive(Debug, Clone, Default, PartialEq, serde::Deserialize, serde::Serialize)] pub struct Date { original: String, } impl Date { pub fn new() -> Self { - Self { - original: String::new(), - } + Self::default() } } diff --git a/crates/gen-types/src/value_objects/name.rs b/crates/gen-types/src/value_objects/name.rs index 836b54b..22a78c2 100644 --- a/crates/gen-types/src/value_objects/name.rs +++ b/crates/gen-types/src/value_objects/name.rs @@ -5,20 +5,12 @@ pub use name_form::NameForm; pub use name_part_type::NamePartType; /// A name conclusion -#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)] +#[derive(Debug, Clone, Default, PartialEq, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] pub struct Name { name_forms: Vec, } -impl Default for Name { - fn default() -> Self { - Self { - name_forms: Vec::new(), - } - } -} - impl Name { pub fn new() -> Name { Self::default() diff --git a/crates/gen-types/src/value_objects/name/name_form.rs b/crates/gen-types/src/value_objects/name/name_form.rs index bf1ed20..d4d7e7c 100644 --- a/crates/gen-types/src/value_objects/name/name_form.rs +++ b/crates/gen-types/src/value_objects/name/name_form.rs @@ -1,5 +1,5 @@ /// A name form conclusion -#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)] +#[derive(Debug, Clone, Default, PartialEq, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] pub struct NameForm { full_text: String, @@ -9,10 +9,7 @@ pub struct NameForm { impl NameForm { pub fn new() -> Self { - Self { - full_text: String::new(), - lang: String::new(), - } + Self::default() } } diff --git a/crates/gen-types/src/value_objects/text_value.rs b/crates/gen-types/src/value_objects/text_value.rs index b91d85d..e471bee 100644 --- a/crates/gen-types/src/value_objects/text_value.rs +++ b/crates/gen-types/src/value_objects/text_value.rs @@ -11,7 +11,7 @@ impl TextValue { } pub fn lang(&self) -> Option<&str> { - self.lang.as_ref().map(|s| s.as_str()) + self.lang.as_deref() } }