From 7af4dae96dd5f1b0cfdb7be92d7a142c37c96344 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 15 Dec 2024 20:07:34 +0000 Subject: [PATCH] refactor: Execute::execute passes itself by ref --- src/execute.rs | 4 ++-- src/nextcloud/card.rs | 26 +++++++++++++------------- src/nextcloud/deck.rs | 4 ++-- src/nextcloud/mod.rs | 2 +- src/nextcloud/stack.rs | 4 ++-- src/trello/attachment.rs | 6 +++--- src/trello/board.rs | 4 ++-- src/trello/card.rs | 4 ++-- src/trello/client.rs | 6 ++++-- src/trello/member.rs | 4 ++-- src/trello/mod.rs | 2 +- src/trello/stack.rs | 4 ++-- 12 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/execute.rs b/src/execute.rs index f99dd45..e078c93 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -5,11 +5,11 @@ use color_eyre::Result; use crate::FullCtx; pub(crate) trait Execute { - async fn execute(self, ctx: &FullCtx) -> Result<()>; + async fn execute(&self, ctx: &FullCtx) -> Result<()>; } impl Execute for crate::Command { - async fn execute(self, ctx: &FullCtx) -> Result<()> { + async fn execute(&self, ctx: &FullCtx) -> Result<()> { match self { Self::Init => Err(eyre!("Config file already exists. Not overwriting it.")), Self::Check => crate::check::run(ctx).await, diff --git a/src/nextcloud/card.rs b/src/nextcloud/card.rs index a798111..92115f0 100644 --- a/src/nextcloud/card.rs +++ b/src/nextcloud/card.rs @@ -41,10 +41,10 @@ pub(crate) enum NextcloudCardCommand { } impl Execute for NextcloudCardCommand { - async fn execute(self, ctx: &FullCtx) -> color_eyre::Result<()> { + async fn execute(&self, ctx: &FullCtx) -> color_eyre::Result<()> { match self { Self::List { dump, stack_id } => { - list(ctx, dump, NextcloudStackId::from(stack_id)).await + list(ctx, *dump, NextcloudStackId::from(*stack_id)).await } Self::Get { dump, @@ -53,9 +53,9 @@ impl Execute for NextcloudCardCommand { } => { get( ctx, - dump, - NextcloudStackId::from(stack_id), - NextcloudCardId::from(card_id), + *dump, + NextcloudStackId::from(*stack_id), + NextcloudCardId::from(*card_id), ) .await } @@ -68,10 +68,10 @@ impl Execute for NextcloudCardCommand { create( ctx, Create { - dump, - stack_id, - title, - description, + dump: *dump, + stack_id: (*stack_id), + title: title.clone(), + description: description.clone(), }, ) .await @@ -85,10 +85,10 @@ impl Execute for NextcloudCardCommand { add_label( ctx, AddLabel { - dump, - stack_id: NextcloudStackId::from(stack_id), - card_id: NextcloudCardId::from(card_id), - label_id: NextcloudLabelId::from(label_id), + dump: *dump, + stack_id: NextcloudStackId::from(*stack_id), + card_id: NextcloudCardId::from(*card_id), + label_id: NextcloudLabelId::from(*label_id), }, ) .await diff --git a/src/nextcloud/deck.rs b/src/nextcloud/deck.rs index ef31024..2fc87fe 100644 --- a/src/nextcloud/deck.rs +++ b/src/nextcloud/deck.rs @@ -13,11 +13,11 @@ pub(crate) enum NextcloudDeckCommand { } impl Execute for NextcloudDeckCommand { - async fn execute(self, ctx: &FullCtx) -> color_eyre::Result<()> { + async fn execute(&self, ctx: &FullCtx) -> color_eyre::Result<()> { match self { Self::Get { dump } => { let api_result = ctx.deck_client().get_boards().await; - if dump { + if *dump { p!(ctx.prt, "{}", api_result.text); } else { let mut boards = api_result.result?; diff --git a/src/nextcloud/mod.rs b/src/nextcloud/mod.rs index c63d0fe..d4a191e 100644 --- a/src/nextcloud/mod.rs +++ b/src/nextcloud/mod.rs @@ -38,7 +38,7 @@ pub(crate) enum NextcloudCommand { Card(NextcloudCardCommand), } impl Execute for NextcloudCommand { - async fn execute(self, ctx: &FullCtx) -> color_eyre::Result<()> { + async fn execute(&self, ctx: &FullCtx) -> color_eyre::Result<()> { match self { NextcloudCommand::Deck(cmd) => cmd.execute(ctx).await, NextcloudCommand::Stack(cmd) => cmd.execute(ctx).await, diff --git a/src/nextcloud/stack.rs b/src/nextcloud/stack.rs index 129057c..1446fe9 100644 --- a/src/nextcloud/stack.rs +++ b/src/nextcloud/stack.rs @@ -13,9 +13,9 @@ pub(crate) enum NextcloudStackCommand { } impl Execute for NextcloudStackCommand { - async fn execute(self, ctx: &FullCtx) -> color_eyre::Result<()> { + async fn execute(&self, ctx: &FullCtx) -> color_eyre::Result<()> { match self { - Self::List { dump } => list(ctx, dump).await, + Self::List { dump } => list(ctx, *dump).await, } } } diff --git a/src/trello/attachment.rs b/src/trello/attachment.rs index 656884f..893b4c7 100644 --- a/src/trello/attachment.rs +++ b/src/trello/attachment.rs @@ -25,7 +25,7 @@ pub(crate) enum TrelloAttachmentCommand { } impl Execute for TrelloAttachmentCommand { - async fn execute(self, ctx: &FullCtx) -> Result<()> { + async fn execute(&self, ctx: &FullCtx) -> Result<()> { match self { Self::Get { dump, @@ -38,7 +38,7 @@ impl Execute for TrelloAttachmentCommand { .trello_client() .card_attachment(&trello_card_id, &trello_attachment_id) .await; - if dump { + if *dump { p!(ctx.prt, "{}", api_result.text); } else { let attachment = api_result.result?; @@ -55,7 +55,7 @@ impl Execute for TrelloAttachmentCommand { let trello_attachment_id = TrelloAttachmentId::new(attachment_id); let file_name = ctx .trello_client() - .save_attachment(&trello_card_id, &trello_attachment_id, file_name) + .save_attachment(&trello_card_id, &trello_attachment_id, file_name.as_ref()) .await?; p!(ctx.prt, "Wrote: {}", file_name.display()); Ok(()) diff --git a/src/trello/board.rs b/src/trello/board.rs index d9e54d3..65a8d03 100644 --- a/src/trello/board.rs +++ b/src/trello/board.rs @@ -17,14 +17,14 @@ pub(crate) enum TrelloBoardCommand { } impl Execute for TrelloBoardCommand { - async fn execute(self, ctx: &FullCtx) -> color_eyre::Result<()> { + async fn execute(&self, ctx: &FullCtx) -> color_eyre::Result<()> { match self { Self::Get { dump, board_id } => { let api_result = ctx .trello_client() .board(&TrelloBoardId::new(board_id)) .await; - if dump { + if *dump { p!(ctx.prt, "{}", api_result.text); } else { let mut lists = api_result.result?.lists; diff --git a/src/trello/card.rs b/src/trello/card.rs index ff8a375..51bb597 100644 --- a/src/trello/card.rs +++ b/src/trello/card.rs @@ -17,11 +17,11 @@ pub(crate) enum TrelloCardCommand { } impl Execute for TrelloCardCommand { - async fn execute(self, ctx: &FullCtx) -> Result<()> { + async fn execute(&self, ctx: &FullCtx) -> Result<()> { match self { Self::Get { dump, card_id } => { let api_result = ctx.trello_client().card(&TrelloCardId::new(card_id)).await; - if dump { + if *dump { p!(ctx.prt, "{}", api_result.text); } else { let attachments = api_result.result?.attachments; diff --git a/src/trello/client.rs b/src/trello/client.rs index f0d2739..54ea871 100644 --- a/src/trello/client.rs +++ b/src/trello/client.rs @@ -68,11 +68,13 @@ impl<'ctx> TrelloClient<'ctx> { &self, card_id: &TrelloCardId, attachment_id: &TrelloAttachmentId, - file_name: Option, + file_name: Option<&PathBuf>, ) -> color_eyre::Result { let attachment = self.card_attachment(card_id, attachment_id).await.result?; let url = attachment.url; - let file_name = file_name.unwrap_or_else(|| attachment.file_name.into()); + let file_name = file_name + .cloned() + .unwrap_or_else(|| PathBuf::from(attachment.file_name)); crate::e!(self.ctx.prt, "file_name: {}", file_name.display()); crate::e!(self.ctx.prt, "base: {}", self.ctx.fs.base().display()); let file_name = self.ctx.fs.base().join(file_name); diff --git a/src/trello/member.rs b/src/trello/member.rs index 170e19c..5b780f1 100644 --- a/src/trello/member.rs +++ b/src/trello/member.rs @@ -13,11 +13,11 @@ pub(crate) enum TrelloMemberCommand { } impl Execute for TrelloMemberCommand { - async fn execute(self, ctx: &FullCtx) -> color_eyre::Result<()> { + async fn execute(&self, ctx: &FullCtx) -> color_eyre::Result<()> { match self { Self::Get { dump } => { let api_result = ctx.trello_client().boards().await; - if dump { + if *dump { p!(ctx.prt, "{}", api_result.text); } else { let mut boards = api_result.result?; diff --git a/src/trello/mod.rs b/src/trello/mod.rs index abe1785..6db58b0 100644 --- a/src/trello/mod.rs +++ b/src/trello/mod.rs @@ -47,7 +47,7 @@ pub(crate) enum TrelloCommand { } impl Execute for TrelloCommand { - async fn execute(self, ctx: &FullCtx) -> color_eyre::Result<()> { + async fn execute(&self, ctx: &FullCtx) -> color_eyre::Result<()> { match self { Self::Member(cmd) => cmd.execute(ctx).await, Self::Board(cmd) => cmd.execute(ctx).await, diff --git a/src/trello/stack.rs b/src/trello/stack.rs index 420f576..22e9119 100644 --- a/src/trello/stack.rs +++ b/src/trello/stack.rs @@ -17,14 +17,14 @@ pub(crate) enum TrelloStackCommand { } impl Execute for TrelloStackCommand { - async fn execute(self, ctx: &FullCtx) -> Result<()> { + async fn execute(&self, ctx: &FullCtx) -> Result<()> { match self { Self::Get { dump, list_id } => { let api_result = ctx .trello_client() .list_cards(&TrelloListId::new(list_id)) .await; - if dump { + if *dump { p!(ctx.prt, "{}", api_result.text); } else { let cards = api_result.result?;