CLIPPY Refactor code for improved readability and performance

19f8d9b · gmorell · 2025-01-09 01:34

7 files +149 -187
Message
{commit_body(@commit)}

Files changed

modified src/data.rs
+5 −8
@@ -8,15 +8,12 @@ use crate::entities::{
8 8 contact, ppl, relation, sig_date, tier, tier_defaults, trait_defaults, traits,
9 9 };
10 10 use crate::PplError;
11 −use chrono::{Date, Local, NaiveDate};
12 −use clap::builder::Str;
11 +use chrono::{Local, NaiveDate};
13 12 use color_eyre::owo_colors::OwoColorize;
14 13 use log::warn;
15 −use ratatui::style::palette::tailwind::{AMBER, PURPLE};
14 +use ratatui::style::palette::tailwind::PURPLE;
16 15 use sea_orm::ActiveValue::Set;
17 16 use sea_orm::{ActiveModelTrait, DatabaseConnection, DbErr, EntityTrait};
18 −use tokio::fs::symlink;
19 −use tracing_subscriber::registry::Data;
20 17
21 18 fn bool_from_string(input: Option<String>) -> bool {
22 19 if input.is_none() {
@@ -134,7 +131,7 @@ impl PplOps {
134 131 pub async fn create_me(
135 132 db: &DatabaseConnection,
136 133 name: String,
137 − ) -> Result<(ppl::Model), PplError> {
134 + ) -> Result<ppl::Model, PplError> {
138 135 let p = ppl::ActiveModel {
139 136 id: Default::default(),
140 137 name: Set(name),
@@ -154,7 +151,7 @@ impl PplOps {
154 151 }
155 152 }
156 153 }
157 − pub async fn create(db: &DatabaseConnection, name: String) -> Result<(ppl::Model), PplError> {
154 + pub async fn create(db: &DatabaseConnection, name: String) -> Result<ppl::Model, PplError> {
158 155 let p = ppl::ActiveModel {
159 156 id: Default::default(),
160 157 name: Set(name),
@@ -363,7 +360,7 @@ impl SigDateOps {
363 360 let mut am: sig_date::ActiveModel = instance.into();
364 361
365 362 am.event = Set(event);
366 − am.date = Set(date.unwrap_or(NaiveDate::default()));
363 + am.date = Set(date.unwrap_or_default());
367 364 am.do_remind = Set(do_remind);
368 365 am.update(db).await?;
369 366 Ok(())
modified src/do_add.rs
+11 −16
@@ -1,19 +1,14 @@
1 1 use crate::data::{ContactOps, PplOps, RelationOps, SigDateOps, TierOps, TraitOps};
2 2 use crate::entities::ppl::Column::Me;
3 3 use crate::entities::prelude::Ppl;
4 −use crate::entities::tier;
5 −use crate::entities::tier::Model;
6 −use crate::entities::{contact, ppl};
7 4 use crate::PplError;
8 5 use chrono::Local;
9 6 use clap::Parser;
10 7 use color_eyre::owo_colors::OwoColorize;
11 −use interim::datetime::Date;
12 8 use interim::{parse_date_string, Dialect};
13 9 use log::warn;
14 −use sea_orm::ActiveValue::Set;
15 10 use sea_orm::ColumnTrait;
16 −use sea_orm::{ActiveModelTrait, DatabaseConnection, DbErr, EntityTrait, QueryFilter};
11 +use sea_orm::{DatabaseConnection, EntityTrait, QueryFilter};
17 12 use tracing::info;
18 13
19 14 // RUST_LOG=warn cargo run -- add Anjali Budday "+1 5088089658" avenkatesh.wpi@gmail.com 1993-08-28 2018-12-20 Wife 2012-01-01 WPI
@@ -43,14 +38,14 @@ pub async fn do_add(args: &AddArgs, db: DatabaseConnection) -> Result<(), PplErr
43 38
44 39 match &args.tier {
45 40 None => Ok(()),
46 − Some(tier) => TierOps::create(&db, pp.id.clone(), tier.clone()).await,
41 + Some(tier) => TierOps::create(&db, pp.id, tier.clone()).await,
47 42 }?;
48 43 match &args.phone {
49 44 None => Ok(()),
50 45 Some(phone) => {
51 46 ContactOps::create(
52 47 &db,
53 − pp.id.clone(),
48 + pp.id,
54 49 "phone".to_string(),
55 50 "primary".to_string(),
56 51 phone.to_string(),
@@ -63,7 +58,7 @@ pub async fn do_add(args: &AddArgs, db: DatabaseConnection) -> Result<(), PplErr
63 58 Some(phone) => {
64 59 ContactOps::create(
65 60 &db,
66 − pp.id.clone(),
61 + pp.id,
67 62 "email".to_string(),
68 63 "primary".to_string(),
69 64 phone.to_string(),
@@ -79,7 +74,7 @@ pub async fn do_add(args: &AddArgs, db: DatabaseConnection) -> Result<(), PplErr
79 74 Ok(parsed) => {
80 75 SigDateOps::create(
81 76 &db,
82 − pp.id.clone(),
77 + pp.id,
83 78 parsed.date_naive(),
84 79 "birthday".to_string(),
85 80 true,
@@ -102,7 +97,7 @@ pub async fn do_add(args: &AddArgs, db: DatabaseConnection) -> Result<(), PplErr
102 97 Ok(parsed) => {
103 98 SigDateOps::create(
104 99 &db,
105 − pp.id.clone(),
100 + pp.id,
106 101 parsed.date_naive(),
107 102 "wedding".to_string(),
108 103 false,
@@ -122,8 +117,8 @@ pub async fn do_add(args: &AddArgs, db: DatabaseConnection) -> Result<(), PplErr
122 117 Some(relation) => {
123 118 RelationOps::create(
124 119 &db,
125 − me.id.clone(),
126 − pp.id.clone(),
120 + me.id,
121 + pp.id,
127 122 relation.to_owned(),
128 123 false,
129 124 None,
@@ -141,7 +136,7 @@ pub async fn do_add(args: &AddArgs, db: DatabaseConnection) -> Result<(), PplErr
141 136 Ok(parsed) => {
142 137 SigDateOps::create(
143 138 &db,
144 − pp.id.clone(),
139 + pp.id,
145 140 parsed.date_naive(),
146 141 "met".to_string(),
147 142 false,
@@ -161,7 +156,7 @@ pub async fn do_add(args: &AddArgs, db: DatabaseConnection) -> Result<(), PplErr
161 156 Some(from) => {
162 157 TraitOps::create(
163 158 &db,
164 − pp.id.clone(),
159 + pp.id,
165 160 "from".to_string(),
166 161 from.to_string(),
167 162 false,
@@ -175,7 +170,7 @@ pub async fn do_add(args: &AddArgs, db: DatabaseConnection) -> Result<(), PplErr
175 170 Some(addr) => {
176 171 ContactOps::create(
177 172 &db,
178 − pp.id.clone(),
173 + pp.id,
179 174 "address".to_string(),
180 175 "primary".to_string(),
181 176 addr.to_string(),
modified src/do_tui.rs
+132 −157
@@ -2,25 +2,22 @@ use crate::data::{
2 2 ContactOps, PplOps, RelationOps, SigDateOps, TierDefaultOps, TierOps, TraitDefaultOps, TraitOps,
3 3 };
4 4 use crate::entities::ppl::Model;
5 −use crate::entities::prelude::{Ppl, TierDefaults, TraitDefaults};
6 5 use crate::entities::{
7 6 contact, ppl, relation, sig_date, tier, tier_defaults, trait_defaults, traits,
8 7 };
9 8 use crate::PplError;
10 −use chrono::NaiveDate;
11 9 use color_eyre::owo_colors::OwoColorize;
12 10 use crossterm::event;
13 −use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
11 +use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind};
14 12 use enum_iterator::{next, Sequence};
15 13 use ratatui::prelude::{Constraint, Direction, Layout, Line, Modifier, Span, Style, Stylize, Text};
16 14 use ratatui::style::palette::material::AMBER;
17 −use ratatui::style::palette::tailwind::{GREEN, ORANGE, SLATE, WHITE};
15 +use ratatui::style::palette::tailwind::{ORANGE, SLATE, WHITE};
18 16 use ratatui::style::{Color, Styled};
19 −use ratatui::widgets::{Block, Borders, List, ListDirection, ListItem, ListState, Paragraph, Wrap};
17 +use ratatui::widgets::{Block, Borders, List, ListDirection, ListItem, ListState, Paragraph};
20 18 use ratatui::{DefaultTerminal, Frame};
21 19 use sea_orm::DatabaseConnection;
22 20 use std::ops::Index;
23 −use tokio::task::id;
24 21 use tui_input::backend::crossterm::EventHandler;
25 22 use tui_input::Input;
26 23
@@ -179,79 +176,68 @@ pub async fn run_tui(
179 176 },
180 177 KeyCode::Tab => match app.ppl_field_editing {
181 178 true => {
182 − app.ppl_field_idx = app.ppl_field_idx + 1;
179 + app.ppl_field_idx += 1;
183 180 }
184 181 false => {
185 − app.current_tab = next(&app.current_tab).unwrap_or_else(|| Tabs::Ppl)
182 + app.current_tab = next(&app.current_tab).unwrap_or(Tabs::Ppl)
186 183 }
187 184 },
188 185 KeyCode::Esc => break Ok(()),
189 − KeyCode::Char('e') => match app.current_tab {
190 − Tabs::Ppl => match app.ppl_field_editing {
191 − true => {
192 − default_editing_handler(&mut app, &key_event);
193 − }
194 − false => {
195 − app.ppl_editing = !app.ppl_editing;
196 − }
197 − },
198 − _ => {}
199 − },
186 + KeyCode::Char('e') => if app.current_tab == Tabs::Ppl { match app.ppl_field_editing {
187 + true => {
188 + default_editing_handler(&mut app, &key_event);
189 + }
190 + false => {
191 + app.ppl_editing = !app.ppl_editing;
192 + }
193 + } },
200 194 KeyCode::Char('f') => {
201 195 default_editing_handler(&mut app, &key_event);
202 196 }
203 − KeyCode::Enter => match app.current_tab {
204 − Tabs::Ppl => match app.ppl_editing {
205 − true => {
206 − if app.ppl_detail_state.selected().is_some() {
207 − let idx = &app.ppl_detail_state.selected().unwrap();
208 − let e = app.ppl_editables.get(*idx).unwrap();
209 − if !app.ppl_field_editing {
210 − // let k
211 − app.ppl_input_a = e.first.clone().into();
212 − app.ppl_input_b =
213 − e.second.clone().unwrap_or("".to_string()).into();
214 − app.ppl_input_c =
215 − e.third.clone().unwrap_or("".to_string()).into();
216 − } else {
217 − let new_editable = Editable {
218 − tgt: e.tgt.clone(),
219 − id: e.id,
220 − first: app.ppl_input_a.value().parse().unwrap(),
221 − second: Option::from(
222 − app.ppl_input_b.value().to_string(),
223 − ),
224 − third: Option::from(
225 − app.ppl_input_c.value().to_string(),
226 − ),
227 − };
228 −
229 − match e.tgt {
230 − Editablez::Trait => {
231 − TraitOps::updatee(&db, new_editable).await;
232 − }
233 − Editablez::Tier => {
234 − TierOps::updatee(&db, new_editable).await;
235 − }
236 − Editablez::Contact => {
237 − ContactOps::updatee(&db, new_editable).await;
238 − }
239 − Editablez::Relation => {
240 − RelationOps::updatee(&db, new_editable).await;
241 − }
242 − Editablez::SigDate => {
243 − SigDateOps::updatee(&db, new_editable).await;
244 − }
245 − };
246 − app.reload(&db).await?;
247 − }
248 − app.ppl_field_editing = !app.ppl_field_editing;
197 + KeyCode::Enter => if app.current_tab == Tabs::Ppl { if app.ppl_editing && app.ppl_detail_state.selected().is_some() {
198 + let idx = &app.ppl_detail_state.selected().unwrap();
199 + let e = app.ppl_editables.get(*idx).unwrap();
200 + if !app.ppl_field_editing {
201 + // let k
202 + app.ppl_input_a = e.first.clone().into();
203 + app.ppl_input_b =
204 + e.second.clone().unwrap_or("".to_string()).into();
205 + app.ppl_input_c =
206 + e.third.clone().unwrap_or("".to_string()).into();
207 + } else {
208 + let new_editable = Editable {
209 + tgt: e.tgt.clone(),
210 + id: e.id,
211 + first: app.ppl_input_a.value().parse().unwrap(),
212 + second: Option::from(
213 + app.ppl_input_b.value().to_string(),
214 + ),
215 + third: Option::from(
216 + app.ppl_input_c.value().to_string(),
217 + ),
218 + };
219 +
220 + match e.tgt {
221 + Editablez::Trait => {
222 + TraitOps::updatee(&db, new_editable).await;
249 223 }
250 − }
251 − false => {}
252 − },
253 − _ => {}
254 − },
224 + Editablez::Tier => {
225 + TierOps::updatee(&db, new_editable).await;
226 + }
227 + Editablez::Contact => {
228 + ContactOps::updatee(&db, new_editable).await;
229 + }
230 + Editablez::Relation => {
231 + RelationOps::updatee(&db, new_editable).await;
232 + }
233 + Editablez::SigDate => {
234 + SigDateOps::updatee(&db, new_editable).await;
235 + }
236 + };
237 + app.reload(&db).await?;
238 + }
239 + app.ppl_field_editing = !app.ppl_field_editing;
240 + } },
255 241 _ => {
256 242 default_editing_handler(&mut app, &key_event);
257 243 }
@@ -264,74 +250,69 @@ pub async fn run_tui(
264 250 }
265 251
266 252 fn default_editing_handler(app: &mut Tui, key_event: &KeyEvent) {
267 − match app.ppl_editing {
268 − true => {
269 − let idx = &app.ppl_detail_state.selected().unwrap();
270 − let e = app.ppl_editables.get(*idx).unwrap();
271 − match e.tgt {
272 − Editablez::Trait => match app.ppl_field_idx % 2 {
273 − 0 => {
274 − app.ppl_input_a.handle_event(&Event::Key(*key_event));
275 − }
276 − 1 => {
277 − app.ppl_input_b.handle_event(&Event::Key(*key_event));
278 − }
279 − _ => {}
280 − },
281 − Editablez::Tier => match app.ppl_field_idx % 3 {
282 − 0 => {
283 − app.ppl_input_a.handle_event(&Event::Key(*key_event));
284 − }
285 − 1 => {
286 − app.ppl_input_b.handle_event(&Event::Key(*key_event));
287 − }
288 − 2 => {
289 − app.ppl_input_c.handle_event(&Event::Key(*key_event));
290 − }
291 − _ => {}
292 − },
293 − Editablez::Contact => match app.ppl_field_idx % 3 {
294 − 0 => {
295 − app.ppl_input_a.handle_event(&Event::Key(*key_event));
296 − }
297 − 1 => {
298 − app.ppl_input_b.handle_event(&Event::Key(*key_event));
299 − }
300 − 2 => {
301 − app.ppl_input_c.handle_event(&Event::Key(*key_event));
302 − }
303 − _ => {}
304 − },
305 − Editablez::Relation => match app.ppl_field_idx % 3 {
306 − 0 => {
307 − app.ppl_input_a.handle_event(&Event::Key(*key_event));
308 − }
309 − 1 => {
310 − app.ppl_input_b.handle_event(&Event::Key(*key_event));
311 − }
312 − 2 => {
313 − app.ppl_input_c.handle_event(&Event::Key(*key_event));
314 − }
315 − _ => {}
316 − },
317 − Editablez::SigDate => match app.ppl_field_idx % 3 {
318 − 0 => {
319 − app.ppl_input_a.handle_event(&Event::Key(*key_event));
320 − }
321 − 1 => {
322 − app.ppl_input_b.handle_event(&Event::Key(*key_event));
323 − }
324 − 2 => {
325 − app.ppl_input_c.handle_event(&Event::Key(*key_event));
326 − }
327 − _ => {}
328 − },
329 − }
330 − match app.ppl_field_idx {
253 + if app.ppl_editing {
254 + let idx = &app.ppl_detail_state.selected().unwrap();
255 + let e = app.ppl_editables.get(*idx).unwrap();
256 + match e.tgt {
257 + Editablez::Trait => match app.ppl_field_idx % 2 {
258 + 0 => {
259 + app.ppl_input_a.handle_event(&Event::Key(*key_event));
260 + }
261 + 1 => {
262 + app.ppl_input_b.handle_event(&Event::Key(*key_event));
263 + }
331 264 _ => {}
332 − }
265 + },
266 + Editablez::Tier => match app.ppl_field_idx % 3 {
267 + 0 => {
268 + app.ppl_input_a.handle_event(&Event::Key(*key_event));
269 + }
270 + 1 => {
271 + app.ppl_input_b.handle_event(&Event::Key(*key_event));
272 + }
273 + 2 => {
274 + app.ppl_input_c.handle_event(&Event::Key(*key_event));
275 + }
276 + _ => {}
277 + },
278 + Editablez::Contact => match app.ppl_field_idx % 3 {
279 + 0 => {
280 + app.ppl_input_a.handle_event(&Event::Key(*key_event));
281 + }
282 + 1 => {
283 + app.ppl_input_b.handle_event(&Event::Key(*key_event));
284 + }
285 + 2 => {
286 + app.ppl_input_c.handle_event(&Event::Key(*key_event));
287 + }
288 + _ => {}
289 + },
290 + Editablez::Relation => match app.ppl_field_idx % 3 {
291 + 0 => {
292 + app.ppl_input_a.handle_event(&Event::Key(*key_event));
293 + }
294 + 1 => {
295 + app.ppl_input_b.handle_event(&Event::Key(*key_event));
296 + }
297 + 2 => {
298 + app.ppl_input_c.handle_event(&Event::Key(*key_event));
299 + }
300 + _ => {}
301 + },
302 + Editablez::SigDate => match app.ppl_field_idx % 3 {
303 + 0 => {
304 + app.ppl_input_a.handle_event(&Event::Key(*key_event));
305 + }
306 + 1 => {
307 + app.ppl_input_b.handle_event(&Event::Key(*key_event));
308 + }
309 + 2 => {
310 + app.ppl_input_c.handle_event(&Event::Key(*key_event));
311 + }
312 + _ => {}
313 + },
333 314 }
334 − false => {}
315 + {}
335 316 }
336 317 }
337 318
@@ -407,12 +388,12 @@ fn render(f: &mut Frame, app: &mut Tui) {
407 388 let trait_defaults = app
408 389 .def_trait_list
409 390 .iter()
410 − .filter(|t| t.is_date == false && t.is_contact == false)
391 + .filter(|t| !t.is_date && !t.is_contact)
411 392 .collect::<Vec<&trait_defaults::Model>>();
412 393 for t in &curr_traits {
413 394 editables.push(Editable {
414 395 tgt: Editablez::Trait,
415 − id: t.id.clone(),
396 + id: t.id,
416 397 first: t.key.clone(),
417 398 second: Option::from(t.value.clone()),
418 399 third: t.hidden.clone().to_string().into(),
@@ -445,11 +426,11 @@ fn render(f: &mut Frame, app: &mut Tui) {
445 426 for t in &curr_tiers {
446 427 let othersym = app.def_tier_list.iter().find(|dt| dt.key == t.name);
447 428 editables.push(Editable {
448 − id: t.id.clone(),
429 + id: t.id,
449 430 tgt: Editablez::Tier,
450 431 first: t.name.clone(),
451 − second: Option::from(t.color.clone()),
452 − third: Option::from(t.symbol.clone()),
432 + second: t.color.clone(),
433 + third: t.symbol.clone(),
453 434 });
454 435 match (&t.symbol, othersym) {
455 436 (None, Some(other)) => msgs.push(Line::from(vec![
@@ -495,16 +476,10 @@ fn render(f: &mut Frame, app: &mut Tui) {
495 476 .map(|t| t.to_owned())
496 477 .collect::<Vec<relation::Model>>();
497 478 for t in &curr_rels {
498 − let i2 = match t.date_entered {
499 − None => None,
500 − Some(d) => Some(d.to_string()),
501 − };
502 − let i3 = match t.date_ended {
503 − None => None,
504 − Some(d) => Some(d.to_string()),
505 − };
479 + let i2 = t.date_entered.map(|d| d.to_string());
480 + let i3 = t.date_ended.map(|d| d.to_string());
506 481 editables.push(Editable {
507 − id: t.id.clone(),
482 + id: t.id,
508 483 tgt: Editablez::Relation,
509 484 first: t.r#type.clone(),
510 485 second: i2,
@@ -529,11 +504,11 @@ fn render(f: &mut Frame, app: &mut Tui) {
529 504 let date_traits = app
530 505 .def_trait_list
531 506 .iter()
532 − .filter(|t| t.is_date == true && t.enabled == true)
507 + .filter(|t| t.is_date && t.enabled)
533 508 .collect::<Vec<&trait_defaults::Model>>();
534 509 for t in &curr_dates {
535 510 editables.push(Editable {
536 − id: t.id.clone(),
511 + id: t.id,
537 512 tgt: Editablez::SigDate,
538 513 first: t.event.clone(),
539 514 second: Option::from(t.date.to_string()),
@@ -573,11 +548,11 @@ fn render(f: &mut Frame, app: &mut Tui) {
573 548 let contact_traits = app
574 549 .def_trait_list
575 550 .iter()
576 − .filter(|t| t.is_contact == true && t.enabled == true)
551 + .filter(|t| t.is_contact && t.enabled)
577 552 .collect::<Vec<&trait_defaults::Model>>();
578 553 for t in &contacts {
579 554 editables.push(Editable {
580 − id: t.id.clone(),
555 + id: t.id,
581 556 tgt: Editablez::Contact,
582 557 first: t.r#type.clone(),
583 558 second: Option::from(t.designator.clone().unwrap_or("".to_string())),
@@ -613,13 +588,13 @@ fn render(f: &mut Frame, app: &mut Tui) {
613 588
614 589 let textw = Text::from(msgs.clone()).style(Style::default());
615 590 let paragraph = Paragraph::new(textw);
616 − let block = match app.ppl_editing {
591 + match app.ppl_editing {
617 592 true => match app.ppl_field_editing {
618 593 true => {
619 594 let ced = editables
620 595 .get(app.ppl_detail_state.selected().unwrap())
621 596 .unwrap();
622 − match { ced.tgt.clone() } {
597 + match ced.tgt.clone() {
623 598 Editablez::Trait => {
624 599 let input_a = Paragraph::new(app.ppl_input_a.value())
625 600 .block(
@@ -764,7 +739,7 @@ fn render(f: &mut Frame, app: &mut Tui) {
764 739 false => {
765 740 let block = Block::new()
766 741 .borders(Borders::ALL)
767 − .title(format!("{}", curr.name.clone()));
742 + .title(curr.name.clone().to_string());
768 743 f.render_widget(paragraph.clone().block(block), layout_ppl[1]);
769 744 }
770 745 };
modified src/entities/prelude.rs
+0 −1
@@ -1,7 +1,6 @@
1 1 //! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
2 2
3 3 pub use super::contact::Entity as Contact;
4 −pub use super::entitys::Entity as Entitys;
5 4 pub use super::ppl::Entity as Ppl;
6 5 pub use super::relation::Entity as Relation;
7 6 pub use super::sig_date::Entity as SigDate;
modified src/main.rs
+1 −5
@@ -1,6 +1,5 @@
1 1 use clap::{Parser, Subcommand};
2 2 use color_eyre::eyre::ErrReport;
3 −use crossterm::event::Event;
4 3 use interim::DateError;
5 4 use log::info;
6 5 use sea_orm::*;
@@ -94,10 +93,7 @@ async fn main() -> Result<(), PplError> {
94 93
95 94 let cli = Cli::parse();
96 95
97 − let is_init = match Ppl::find().filter(Me.eq(true)).one(&db).await? {
98 − None => false,
99 − Some(_) => true,
100 − };
96 + let is_init = Ppl::find().filter(Me.eq(true)).one(&db).await?.is_some();
101 97
102 98 match &cli.command {
103 99 Some(Commands::Edit { name }) => {

Parents: 429aa90