Skip to content

Commit

Permalink
Merge pull request #41 from kod-kristoff/40-gen-services
Browse files Browse the repository at this point in the history
rewrite to gen services
  • Loading branch information
kod-kristoff authored Jun 23, 2024
2 parents 7b26774 + 1aaf5d8 commit 501bebe
Show file tree
Hide file tree
Showing 47 changed files with 101 additions and 167 deletions.
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -39,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"
2 changes: 0 additions & 2 deletions crates/ancestors-cli/src/cli/flags.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::path::PathBuf;

xflags::xflags! {
src "./src/cli/flags.rs"

Expand Down
1 change: 0 additions & 1 deletion crates/ancestors-cli/src/progress.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Various `prodash` types along with various utilities for comfort.
use std::io;
// #[cfg(feature = "progress-unit-bytes")]
// pub use bytesize;
Expand Down
3 changes: 2 additions & 1 deletion crates/ancestors-infra-json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ edition = "2021"
doctest = false

[dependencies]
persons.workspace = true
gen-services.workspace = true
gen-types.workspace = true
# gedcomx-model = { workspace = true }
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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(())
}
}
6 changes: 5 additions & 1 deletion crates/ancestors-kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 2 additions & 11 deletions crates/ancestors-kernel/src/context.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf>,
}

impl Default for AppContext {
fn default() -> Self {
Self {
db: SharedMemStorage::default(),
db_path: None,
}
}
}

impl AppContext {
pub fn db(&self) -> &SharedMemStorage {
&self.db
Expand Down
3 changes: 2 additions & 1 deletion crates/ancestors-kernel/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod context;
pub use persons as core;
pub use gen_services;
pub use gen_types;
3 changes: 3 additions & 0 deletions crates/ancestors-tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ tui-input.workspace = true

# local deps
ancestors-kernel.workspace = true

[lints]
workspace = true
4 changes: 2 additions & 2 deletions crates/ancestors-tui/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions crates/ancestors-tui/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::single_match)]
#![allow(clippy::match_single_binding)]
pub mod components;

use ancestors_kernel::context::AppContext;
Expand All @@ -24,6 +26,7 @@ pub struct App {
pub ctx: AppContext,
}

#[allow(dead_code)]
pub struct AppComponent {
config: Config,
menu: MenuComponent,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -185,7 +188,7 @@ impl AppComponent {
Ok(None)
}

fn move_focus(&mut self, event: InputEvent) -> eyre::Result<Option<Action>> {
fn move_focus(&mut self, _event: InputEvent) -> eyre::Result<Option<Action>> {
match self.mode {
// Mode::Menu => {
// if matches!(event, InputEvent::Key(key) if key.code == KeyCode::Esc) {
Expand Down
7 changes: 3 additions & 4 deletions crates/ancestors-tui/src/app/components/menu.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crossterm::event::KeyCode;
use ratatui::{
style::{Color, Modifier, Style},
text::{Line, Span},
widgets::Tabs,
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;
Expand Down Expand Up @@ -75,9 +74,9 @@ 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 = menu_titles
let menu: Vec<Line> = menu_titles
.iter()
.enumerate()
.map(|(index, t)| {
Expand Down
5 changes: 2 additions & 3 deletions crates/ancestors-tui/src/app/components/person_editor.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -52,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);
Expand Down
9 changes: 1 addition & 8 deletions crates/ancestors-tui/src/app/components/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S: Into<String>>(&mut self, status: S) {
self.status = status.into();
Expand Down
14 changes: 3 additions & 11 deletions crates/ancestors-tui/src/config.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions crates/ancestors-tui/src/event.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
3 changes: 1 addition & 2 deletions crates/ancestors-tui/src/kx_tui/run_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/ancestors-tui/src/kx_tui/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use eyre::Result;
pub use ratatui::Frame;
pub type CrosstermTerminal = ratatui::Terminal<ratatui::backend::CrosstermBackend<std::io::Stderr>>;

use crate::{app::App, event::EventHandler};
use crate::event::EventHandler;

/// Representation of a terminal user interface.
///
Expand Down
3 changes: 3 additions & 0 deletions crates/gen-scraper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ ureq = { workspace = true, features = ["json"] }

# local deps
gen-types.workspace = true

[lints]
workspace = true
6 changes: 2 additions & 4 deletions crates/gen-scraper/src/scrape/process.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@



use gen_types::{
value_objects::{Fact, FactType},
Batch, Household, Person,
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions crates/gen-services/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ edition = "2021"
doctest = false

[dependencies]
# external deps
hashbrown.workspace = true

# local deps
gen-types.workspace = true
16 changes: 2 additions & 14 deletions crates/gen-services/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod mem;
mod person_repository;

use std::sync::Arc;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ 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};

#[derive(Default)]
pub struct InMemoryPersonRepo {
storage: Arc<RwLock<HashMap<PersonId, Person>>>,
}
Expand All @@ -28,7 +29,7 @@ impl PersonRepository for InMemoryPersonRepo {
self.storage
.write()
.unwrap()
.insert(person.id().clone(), person.clone());
.insert(*person.id(), person.clone());
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
File renamed without changes.
Loading

0 comments on commit 501bebe

Please sign in to comment.