From 020cab782b235bfa5727d02e5aba4981a8a5dfb2 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 28 Oct 2024 05:11:28 +0100 Subject: [PATCH] Move `TelegramWrite` impl for `Diario` to its own file --- src/services/telegram/commands/diario.rs | 41 +----------------------- src/services/telegram/utils/diario.rs | 40 +++++++++++++++++++++++ src/services/telegram/utils/mod.rs | 3 +- 3 files changed, 43 insertions(+), 41 deletions(-) create mode 100644 src/services/telegram/utils/diario.rs diff --git a/src/services/telegram/commands/diario.rs b/src/services/telegram/commands/diario.rs index b01573c9..6bf1b1a8 100644 --- a/src/services/telegram/commands/diario.rs +++ b/src/services/telegram/commands/diario.rs @@ -1,4 +1,3 @@ -use std::fmt::{Error, Write}; use std::str::FromStr; use anyhow::Context; @@ -9,11 +8,10 @@ use teloxide::prelude::Requester; use teloxide::types::{Message, ParseMode, ReplyParameters}; use teloxide::Bot; -use crate::interfaces::database::models::Diario; use crate::interfaces::database::models::RoyalnetUser; use crate::services::telegram::commands::CommandResult; use crate::services::telegram::dependencies::interface_database::DatabaseInterface; -use crate::utils::telegram_string::{TelegramEscape, TelegramWrite}; +use crate::utils::telegram_string::TelegramWrite; #[derive(Debug, Clone, PartialEq, Eq)] pub struct DiarioArgs { @@ -65,43 +63,6 @@ impl FromStr for DiarioArgs { } } -impl TelegramWrite for Diario { - fn write_telegram(&self, f: &mut T) -> Result<(), Error> - where - T: Write, - { - // Diario ID - write!(f, "#{}", self.id)?; - - // Optional content warning - if let Some(warning) = self.to_owned().warning { - write!(f, ", {}", warning.escape_telegram_html())?; - } - - // Newline - writeln!(f)?; - - // Quote optionally covered by a spoiler tag - match self.warning.to_owned() { - None => write!(f, "
{}
", self.clone().quote.escape_telegram_html())?, - Some(_) => write!(f, "
{}
", self.clone().quote.escape_telegram_html())?, - } - - // Newline - writeln!(f)?; - - // Optional citation with optional context - match (self.quoted_name.to_owned(), self.context.to_owned()) { - (Some(name), Some(context)) => write!(f, "—{}, {}", name.escape_telegram_html(), context.escape_telegram_html())?, - (Some(name), None) => write!(f, "—{}", name.escape_telegram_html())?, - (None, Some(context)) => write!(f, "...{}", context.escape_telegram_html())?, - (None, None) => write!(f, "")?, - }; - - Ok(()) - } -} - pub async fn handler(bot: &Bot, message: &Message, args: &DiarioArgs, database: &DatabaseInterface) -> CommandResult { let author = message.from.as_ref() .context("Non è stato possibile determinare chi abbia inviato il comando.")?; diff --git a/src/services/telegram/utils/diario.rs b/src/services/telegram/utils/diario.rs new file mode 100644 index 00000000..762e864c --- /dev/null +++ b/src/services/telegram/utils/diario.rs @@ -0,0 +1,40 @@ +use crate::interfaces::database::models::Diario; +use crate::utils::telegram_string::{TelegramEscape, TelegramWrite}; +use std::fmt::{Error, Write}; + +impl TelegramWrite for Diario { + fn write_telegram(&self, f: &mut T) -> Result<(), Error> + where + T: Write, + { + // Diario ID + write!(f, "#{}", self.id)?; + + // Optional content warning + if let Some(warning) = self.to_owned().warning { + write!(f, ", {}", warning.escape_telegram_html())?; + } + + // Newline + writeln!(f)?; + + // Quote optionally covered by a spoiler tag + match self.warning.to_owned() { + None => write!(f, "
{}
", self.clone().quote.escape_telegram_html())?, + Some(_) => write!(f, "
{}
", self.clone().quote.escape_telegram_html())?, + } + + // Newline + writeln!(f)?; + + // Optional citation with optional context + match (self.quoted_name.to_owned(), self.context.to_owned()) { + (Some(name), Some(context)) => write!(f, "—{}, {}", name.escape_telegram_html(), context.escape_telegram_html())?, + (Some(name), None) => write!(f, "—{}", name.escape_telegram_html())?, + (None, Some(context)) => write!(f, "...{}", context.escape_telegram_html())?, + (None, None) => write!(f, "")?, + }; + + Ok(()) + } +} \ No newline at end of file diff --git a/src/services/telegram/utils/mod.rs b/src/services/telegram/utils/mod.rs index 7c3ee5e9..44972632 100644 --- a/src/services/telegram/utils/mod.rs +++ b/src/services/telegram/utils/mod.rs @@ -1,2 +1,3 @@ pub mod database; -pub mod matchmaking; \ No newline at end of file +pub mod matchmaking; +pub mod diario; \ No newline at end of file