From 89f8d21ebb175b8b2a9ae93ea0438bce28b302aa Mon Sep 17 00:00:00 2001 From: James Magahern Date: Sat, 21 Dec 2024 18:01:00 -0800 Subject: [PATCH] clippy/cleanup --- kordophone-db/src/chat_database.rs | 2 +- kordophone-db/src/models/db/conversation.rs | 12 ++++++++---- kordophone-db/src/models/db/participant.rs | 16 +++++++++++++++- kordophone-db/src/models/mod.rs | 1 - kordophone-db/src/models/participant.rs | 5 +---- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/kordophone-db/src/chat_database.rs b/kordophone-db/src/chat_database.rs index 8f4f1eb..71dc246 100644 --- a/kordophone-db/src/chat_database.rs +++ b/kordophone-db/src/chat_database.rs @@ -24,7 +24,7 @@ impl ChatDatabase { db.run_pending_migrations(MIGRATIONS) .map_err(|e| anyhow::anyhow!("Error running migrations: {}", e))?; - return Ok(Self { db }); + Ok(Self { db }) } pub fn insert_conversation(&mut self, conversation: Conversation) -> Result<()> { diff --git a/kordophone-db/src/models/db/conversation.rs b/kordophone-db/src/models/db/conversation.rs index 0ca384e..f1b37c5 100644 --- a/kordophone-db/src/models/db/conversation.rs +++ b/kordophone-db/src/models/db/conversation.rs @@ -1,6 +1,9 @@ use diesel::prelude::*; use chrono::NaiveDateTime; -use crate::models::{Conversation, Participant}; +use crate::models::{ + Conversation, + db::participant::InsertableRecord as InsertableParticipant, +}; #[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, Identifiable)] #[diesel(table_name = crate::schema::conversations)] @@ -26,14 +29,15 @@ impl From for Record { } // This implementation returns the insertable data types for the conversation and participants -impl From for (Record, Vec) { +impl From for (Record, Vec) { fn from(conversation: Conversation) -> Self { ( Record::from(conversation.clone()), - // Keep in mind, db::participant::Record is the selectable data type for the - // participants table, whereas Participant is the insertable model type. conversation.participants + .into_iter() + .map(InsertableParticipant::from) + .collect() ) } } diff --git a/kordophone-db/src/models/db/participant.rs b/kordophone-db/src/models/db/participant.rs index 2e4ead1..ba6d50b 100644 --- a/kordophone-db/src/models/db/participant.rs +++ b/kordophone-db/src/models/db/participant.rs @@ -2,13 +2,27 @@ use diesel::prelude::*; use crate::models::Participant; use crate::schema::conversation_participants; -#[derive(Queryable, Selectable, AsChangeset, Clone, PartialEq, Debug, Identifiable)] +#[derive(Queryable, Selectable, AsChangeset, Identifiable)] #[diesel(table_name = crate::schema::participants)] pub struct Record { pub id: i32, pub display_name: String } +#[derive(Insertable)] +#[diesel(table_name = crate::schema::participants)] +pub struct InsertableRecord { + pub display_name: String +} + +impl From for InsertableRecord { + fn from(participant: Participant) -> Self { + InsertableRecord { + display_name: participant.display_name + } + } +} + #[derive(Identifiable, Selectable, Queryable, Associations, Debug)] #[diesel(belongs_to(super::conversation::Record, foreign_key = conversation_id))] #[diesel(belongs_to(Record, foreign_key = participant_id))] diff --git a/kordophone-db/src/models/mod.rs b/kordophone-db/src/models/mod.rs index 910f406..f0b8b0c 100644 --- a/kordophone-db/src/models/mod.rs +++ b/kordophone-db/src/models/mod.rs @@ -2,6 +2,5 @@ pub mod conversation; pub mod participant; pub mod db; -// Re-export the public types pub use conversation::Conversation; pub use participant::Participant; \ No newline at end of file diff --git a/kordophone-db/src/models/participant.rs b/kordophone-db/src/models/participant.rs index 349187a..9528e2e 100644 --- a/kordophone-db/src/models/participant.rs +++ b/kordophone-db/src/models/participant.rs @@ -1,7 +1,4 @@ -use diesel::prelude::*; - -#[derive(Debug, Clone, PartialEq, Insertable)] -#[diesel(table_name = crate::schema::participants)] +#[derive(Debug, Clone, PartialEq)] pub struct Participant { pub display_name: String, }