mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-22 02:54:21 +00:00
Refactor services to use traits
This commit is contained in:
parent
cd4214cb4b
commit
7a8d30e1d0
13 changed files with 42 additions and 38 deletions
13
src/main.rs
13
src/main.rs
|
@ -1,9 +1,11 @@
|
|||
use anyhow::Result;
|
||||
use crate::telegram::DispatchWithResult;
|
||||
use services::telegram;
|
||||
use crate::services::RoyalnetService;
|
||||
|
||||
pub(crate) mod database;
|
||||
pub(crate) mod utils;
|
||||
mod telegram;
|
||||
mod services;
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
|
@ -12,14 +14,13 @@ async fn main() -> Result<()> {
|
|||
log::debug!("Logging initialized successfully!");
|
||||
|
||||
// Telegram setup
|
||||
log::trace!("Setting up Telegram bot dispatcher...");
|
||||
let mut telegram_dispatcher = telegram::dispatcher();
|
||||
let telegram_awaitable = telegram_dispatcher.dispatch_with_result();
|
||||
log::trace!("Setting up Telegram bot service...");
|
||||
let telegram = telegram::init();
|
||||
|
||||
// Run all services concurrently
|
||||
log::info!("Starting services...");
|
||||
let result = tokio::try_join![
|
||||
telegram_awaitable,
|
||||
telegram.run_royalnet(),
|
||||
];
|
||||
|
||||
// This should never happen, but just in case...
|
||||
|
|
18
src/services/mod.rs
Normal file
18
src/services/mod.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
use std::convert::Infallible;
|
||||
use anyhow::Result;
|
||||
|
||||
pub mod telegram;
|
||||
|
||||
pub trait RoyalnetService {
|
||||
async fn run_royalnet(self) -> Result<Infallible>;
|
||||
}
|
||||
|
||||
impl RoyalnetService for teloxide::dispatching::Dispatcher<teloxide::Bot, anyhow::Error, teloxide::dispatching::DefaultKey> {
|
||||
async fn run_royalnet(mut self) -> Result<Infallible> {
|
||||
log::info!("Starting Telegram service...");
|
||||
self.dispatch().await;
|
||||
|
||||
log::error!("Telegram dispatcher has exited, bailing out...");
|
||||
anyhow::bail!("Telegram dispatcher has exited.")
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@ use rand::seq::SliceRandom;
|
|||
use teloxide::Bot;
|
||||
use teloxide::payloads::SendMessageSetters;
|
||||
use teloxide::prelude::{Message, Requester};
|
||||
use crate::telegram::commands::{CommandResult};
|
||||
use crate::services::telegram::commands::{CommandResult};
|
||||
|
||||
// Cerchiamo di tenere bilanciate le tre colonne, o almeno le prime due.
|
||||
// Se avete un'idea ma metterebbe troppe opzioni in un'unica categoria, mettetela sotto commento.
|
|
@ -6,7 +6,7 @@ use rand::seq::SliceRandom;
|
|||
use teloxide::Bot;
|
||||
use teloxide::payloads::SendMessageSetters;
|
||||
use teloxide::prelude::{Message, Requester};
|
||||
use crate::telegram::commands::{CommandResult};
|
||||
use crate::services::telegram::commands::{CommandResult};
|
||||
|
||||
// Tutte le fortune devono essere positive, o almeno neutrali, per poter essere aggiunte.
|
||||
const FORTUNES: [&str; 164] = [
|
|
@ -20,7 +20,7 @@ mod reminder;
|
|||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, BotCommands)]
|
||||
#[command(rename_rule = "lowercase")]
|
||||
enum Command {
|
||||
pub enum Command {
|
||||
#[command(description = "Invia messaggio di introduzione.")]
|
||||
Start,
|
||||
#[command(description = "Visualizza l'elenco dei comandi disponibili, o mostra informazioni su uno specifico comando.")]
|
14
src/services/telegram/mod.rs
Normal file
14
src/services/telegram/mod.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
use anyhow::Error;
|
||||
use teloxide::Bot;
|
||||
use teloxide::dispatching::{DefaultKey, Dispatcher};
|
||||
|
||||
mod config;
|
||||
mod commands;
|
||||
|
||||
pub fn init() -> Dispatcher<Bot, Error, DefaultKey> {
|
||||
commands::dispatcher(
|
||||
Bot::new(
|
||||
config::TELEGRAM_BOT_TOKEN()
|
||||
)
|
||||
)
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
use std::convert::Infallible;
|
||||
use anyhow::{Error, Result};
|
||||
use teloxide::Bot;
|
||||
use teloxide::dispatching::{DefaultKey, Dispatcher};
|
||||
|
||||
mod config;
|
||||
mod commands;
|
||||
|
||||
pub fn dispatcher() -> Dispatcher<Bot, Error, DefaultKey> {
|
||||
commands::dispatcher(
|
||||
Bot::new(
|
||||
config::TELEGRAM_BOT_TOKEN()
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
pub trait DispatchWithResult {
|
||||
async fn dispatch_with_result(&mut self) -> Result<Infallible>;
|
||||
}
|
||||
|
||||
impl DispatchWithResult for Dispatcher<Bot, Error, DefaultKey> {
|
||||
async fn dispatch_with_result(&mut self) -> Result<Infallible> {
|
||||
log::info!("Starting Telegram dispatcher...");
|
||||
self.dispatch().await;
|
||||
log::error!("Telegram dispatcher has exited, bailing out...");
|
||||
|
||||
anyhow::bail!("Telegram dispatcher has exited.")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue