From c2735ccf2567f10e62458864d9be2d78902d3c52 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Wed, 27 Nov 2024 17:40:02 +0400 Subject: [PATCH] refactor: move schemas and cfg --- src/main.rs | 60 +++------------------------------------------------- src/types.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 57 deletions(-) create mode 100644 src/types.rs diff --git a/src/main.rs b/src/main.rs index d035b06..cac576e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ -use std::sync::Arc; +mod types; + +use types::*; use reqwest::{Client, Url}; -use serde::Deserialize; use tokio::signal::unix::{signal, SignalKind}; const BOT_URL: &str = "https://api.telegram.org/bot"; @@ -14,61 +15,6 @@ const POLL_TIMEOUT: &str = "300"; /// Update types to receive (filtered out on tg backend) const POLL_TYPES: &str = r#"["message"]"#; -/// Shared mutable state object -#[derive(Debug)] -struct State { - offset: u32, -} - -/// Shared config object -#[derive(Debug, Clone)] -struct Config { - client: Client, - upd_url: Arc, - send_url: Arc, -} - -/// Telegram Bot API response: -/// these idiots use `{ok:true,response:...}` schema -/// for some reason -#[derive(Debug, Deserialize)] -struct TgResponse { - result: Vec, -} - -/// Telegram Bot API update event schema -#[derive(Debug, Deserialize)] -struct TgUpdate { - update_id: u32, - message: Option, -} - -/// Telegram Bot API message object schema -#[derive(Debug, Deserialize)] -struct TgMessage { - #[serde(rename = "message_thread_id")] - thread: Option, - chat: TgChat, - text: Option, -} - -/// Telegram Bot API chat object schema -#[derive(Debug, Deserialize)] -struct TgChat { - id: i64, - // #[serde(default)] - // is_forum: bool, -} - -/// XKCD API comic info schema -#[derive(Debug, Deserialize)] -struct XkcdInfo { - #[serde(alias = "text")] // xkcd.ru - alt: String, // xkcd.com - #[serde(alias = "image")] // xkcd.ru - img: String, // xkcd.com -} - #[tokio::main] async fn main() -> std::io::Result<()> { let cfg = { diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..b5e7dfa --- /dev/null +++ b/src/types.rs @@ -0,0 +1,59 @@ +use std::sync::Arc; + +use reqwest::{Client, Url}; +use serde::Deserialize; + +/// Shared mutable state object +#[derive(Debug)] +pub struct State { + pub offset: u32, +} + +/// Shared config object +#[derive(Debug, Clone)] +pub struct Config { + pub client: Client, + pub upd_url: Arc, + pub send_url: Arc, +} + +/// Telegram Bot API response: +/// these idiots use `{ok:true,response:...}` schema +/// for some reason +#[derive(Debug, Deserialize)] +pub struct TgResponse { + pub result: Vec, +} + +/// Telegram Bot API update event schema +#[derive(Debug, Deserialize)] +pub struct TgUpdate { + pub update_id: u32, + pub message: Option, +} + +/// Telegram Bot API message object schema +#[derive(Debug, Deserialize)] +pub struct TgMessage { + #[serde(rename = "message_thread_id")] + pub thread: Option, + pub chat: TgChat, + pub text: Option, +} + +/// Telegram Bot API chat object schema +#[derive(Debug, Deserialize)] +pub struct TgChat { + pub id: i64, + // #[serde(default)] + // is_forum: bool, +} + +/// XKCD API comic info schema +#[derive(Debug, Deserialize)] +pub struct XkcdInfo { + #[serde(alias = "text")] // xkcd.ru + pub alt: String, // xkcd.com + #[serde(alias = "image")] // xkcd.ru + pub img: String, // xkcd.com +}