From 5b7947028ff25db339472c673fa3feb9a5d3c752 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 13 Aug 2022 04:10:25 +0000 Subject: [PATCH] Move the main function to `patched_porobot::telegram::main` --- src/bin/patched_porobot_telegram.rs | 66 +-------------------------- src/telegram/main.rs | 70 +++++++++++++++++++++++++++++ src/telegram/mod.rs | 1 + 3 files changed, 72 insertions(+), 65 deletions(-) create mode 100644 src/telegram/main.rs diff --git a/src/bin/patched_porobot_telegram.rs b/src/bin/patched_porobot_telegram.rs index 0012c09..40c1352 100644 --- a/src/bin/patched_porobot_telegram.rs +++ b/src/bin/patched_porobot_telegram.rs @@ -77,73 +77,9 @@ #![doc(html_logo_url = "https://raw.githubusercontent.com/Steffo99/patched-porobot/main/icon.png")] -use std::path::PathBuf; -use log::*; -use patched_porobot::data::setbundle::card::{Card, CardIndex}; -use patched_porobot::data::corebundle::CoreBundle; -use patched_porobot::data::setbundle::SetBundle; -use patched_porobot::data::corebundle::globals::LocalizedGlobalsIndexes; -use patched_porobot::search::cardsearch::CardSearchEngine; -use patched_porobot::telegram::handler::{inline_query_handler, message_handler}; -use teloxide::prelude::*; - #[doc(hidden)] #[tokio::main] async fn main() { - pretty_env_logger::init(); - debug!("Logger initialized successfully!"); - - debug!("Loading bundles..."); - let core = CoreBundle::load(&*PathBuf::from("./data/core-en_us")).expect("to be able to load `core-en_us` bundle"); - let set1 = SetBundle::load(&*PathBuf::from("./data/set1-en_us")).expect("to be able to load `set1-en_us` bundle"); - let set2 = SetBundle::load(&*PathBuf::from("./data/set2-en_us")).expect("to be able to load `set2-en_us` bundle"); - let set3 = SetBundle::load(&*PathBuf::from("./data/set3-en_us")).expect("to be able to load `set3-en_us` bundle"); - let set4 = SetBundle::load(&*PathBuf::from("./data/set4-en_us")).expect("to be able to load `set4-en_us` bundle"); - let set5 = SetBundle::load(&*PathBuf::from("./data/set5-en_us")).expect("to be able to load `set5-en_us` bundle"); - let set6 = SetBundle::load(&*PathBuf::from("./data/set6-en_us")).expect("to be able to load `set6-en_us` bundle"); - debug!("Loaded all bundles!"); - - debug!("Indexing globals..."); - let globals = LocalizedGlobalsIndexes::from(core.globals); - debug!("Indexed globals!"); - - debug!("Indexing cards..."); - let cards: Vec = [ - set1.cards, - set2.cards, - set3.cards, - set4.cards, - set5.cards, - set6.cards - ].concat(); - - let mut index = CardIndex::new(); - for card in cards { - index.insert(card.code.clone(), card); - } - let cards = index; - debug!("Indexed cards!"); - - debug!("Creating search engine..."); - let engine = CardSearchEngine::new(globals, cards); - debug!("Created search engine!"); - - debug!("Creating Telegram bot with parameters from the environment..."); - let bot = Bot::from_env(); - let me = bot.get_me().send().await.expect("Telegram bot parameters to be valid"); - debug!("Created Telegram bot!"); - - debug!("Creating handlers..."); - let handler = dptree::entry() - .branch(inline_query_handler(engine)) - .branch(message_handler()); - debug!("Created handlers!"); - - info!("@{} is ready!", &me.username.as_ref().expect("bot to have an username")); - Dispatcher::builder(bot, handler) - .enable_ctrlc_handler() - .build() - .dispatch() - .await; + patched_porobot::telegram::main::main().await; } diff --git a/src/telegram/main.rs b/src/telegram/main.rs new file mode 100644 index 0000000..c902d74 --- /dev/null +++ b/src/telegram/main.rs @@ -0,0 +1,70 @@ +//! This module defines the [`main`] function for [`patched_porobot_telegram`]. + +use std::path::PathBuf; +use log::*; +use crate::data::setbundle::card::{Card, CardIndex}; +use crate::data::corebundle::CoreBundle; +use crate::data::setbundle::SetBundle; +use crate::data::corebundle::globals::LocalizedGlobalsIndexes; +use crate::search::cardsearch::CardSearchEngine; +use crate::telegram::handler::{inline_query_handler, message_handler}; +use teloxide::prelude::*; + +/// The main function that [`patched_porobot_telegram`] should run when it's started. +pub async fn main() { + pretty_env_logger::init(); + debug!("Logger initialized successfully!"); + + debug!("Loading bundles..."); + let core = CoreBundle::load(&*PathBuf::from("./data/core-en_us")).expect("to be able to load `core-en_us` bundle"); + let set1 = SetBundle::load(&*PathBuf::from("./data/set1-en_us")).expect("to be able to load `set1-en_us` bundle"); + let set2 = SetBundle::load(&*PathBuf::from("./data/set2-en_us")).expect("to be able to load `set2-en_us` bundle"); + let set3 = SetBundle::load(&*PathBuf::from("./data/set3-en_us")).expect("to be able to load `set3-en_us` bundle"); + let set4 = SetBundle::load(&*PathBuf::from("./data/set4-en_us")).expect("to be able to load `set4-en_us` bundle"); + let set5 = SetBundle::load(&*PathBuf::from("./data/set5-en_us")).expect("to be able to load `set5-en_us` bundle"); + let set6 = SetBundle::load(&*PathBuf::from("./data/set6-en_us")).expect("to be able to load `set6-en_us` bundle"); + debug!("Loaded all bundles!"); + + debug!("Indexing globals..."); + let globals = LocalizedGlobalsIndexes::from(core.globals); + debug!("Indexed globals!"); + + debug!("Indexing cards..."); + let cards: Vec = [ + set1.cards, + set2.cards, + set3.cards, + set4.cards, + set5.cards, + set6.cards + ].concat(); + + let mut index = CardIndex::new(); + for card in cards { + index.insert(card.code.clone(), card); + } + let cards = index; + debug!("Indexed cards!"); + + debug!("Creating search engine..."); + let engine = CardSearchEngine::new(globals, cards); + debug!("Created search engine!"); + + debug!("Creating Telegram bot with parameters from the environment..."); + let bot = Bot::from_env(); + let me = bot.get_me().send().await.expect("Telegram bot parameters to be valid"); + debug!("Created Telegram bot!"); + + debug!("Creating handlers..."); + let handler = dptree::entry() + .branch(inline_query_handler(engine)) + .branch(message_handler()); + debug!("Created handlers!"); + + info!("@{} is ready!", &me.username.as_ref().expect("bot to have an username")); + Dispatcher::builder(bot, handler) + .enable_ctrlc_handler() + .build() + .dispatch() + .await; +} \ No newline at end of file diff --git a/src/telegram/mod.rs b/src/telegram/mod.rs index b396088..acc6877 100644 --- a/src/telegram/mod.rs +++ b/src/telegram/mod.rs @@ -5,3 +5,4 @@ pub mod display; pub mod inline; pub mod handler; +pub mod main;