refactor: move schemas and cfg

This commit is contained in:
DarkCat09 2024-11-27 17:40:02 +04:00
parent 0e6e6a14fd
commit c2735ccf25
Signed by: DarkCat09
GPG key ID: BD3CE9B65916CD82
2 changed files with 62 additions and 57 deletions

View file

@ -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<Url>,
send_url: Arc<Url>,
}
/// Telegram Bot API response:
/// these idiots use `{ok:true,response:...}` schema
/// for some reason
#[derive(Debug, Deserialize)]
struct TgResponse {
result: Vec<TgUpdate>,
}
/// Telegram Bot API update event schema
#[derive(Debug, Deserialize)]
struct TgUpdate {
update_id: u32,
message: Option<TgMessage>,
}
/// Telegram Bot API message object schema
#[derive(Debug, Deserialize)]
struct TgMessage {
#[serde(rename = "message_thread_id")]
thread: Option<i32>,
chat: TgChat,
text: Option<String>,
}
/// 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 = {

59
src/types.rs Normal file
View file

@ -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<Url>,
pub send_url: Arc<Url>,
}
/// Telegram Bot API response:
/// these idiots use `{ok:true,response:...}` schema
/// for some reason
#[derive(Debug, Deserialize)]
pub struct TgResponse {
pub result: Vec<TgUpdate>,
}
/// Telegram Bot API update event schema
#[derive(Debug, Deserialize)]
pub struct TgUpdate {
pub update_id: u32,
pub message: Option<TgMessage>,
}
/// Telegram Bot API message object schema
#[derive(Debug, Deserialize)]
pub struct TgMessage {
#[serde(rename = "message_thread_id")]
pub thread: Option<i32>,
pub chat: TgChat,
pub text: Option<String>,
}
/// 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
}