Skip to content

Instantly share code, notes, and snippets.

@nerodono
Created March 25, 2023 07:33
Show Gist options
  • Save nerodono/f022dfa07556e664dfc7be5397fde280 to your computer and use it in GitHub Desktop.
Save nerodono/f022dfa07556e664dfc7be5397fde280 to your computer and use it in GitHub Desktop.
use std::{
fs,
num::{NonZeroU8, NonZeroUsize},
path::Path,
};
use regex::Regex;
macro_rules! entries {
() => {};
(
$(#[$outer_meta:meta])*
enum $name:ident {
$($item:tt)*
}
$($tail:tt)*
) => {
#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub enum $name {
$($item)*
}
entries!($($tail)*);
};
(
$(#[$outer_meta:meta])*
int $name:ident {
$(
$(#[$inner_meta:meta])*
$variant:ident
),*
$(,)?
}
$($tail:tt)*
) => {
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
$(#[$outer_meta])*
#[serde(rename_all = "snake_case")]
pub enum $name {
$(
$(#[$inner_meta])*
$variant
),*
}
entries!($($tail)*);
};
(
$(#[$outer_meta:meta])*
struct $name:ident {
$(
$(#[$inner_meta:meta])*
$field:ident : $type:ty
),*
$(,)?
}
$($tail:tt)*
) => {
#[derive(Debug, serde::Serialize, serde::Deserialize)]
$(#[$outer_meta])*
pub struct $name {
$(
$(#[$inner_meta])*
pub $field : $type
),*
}
entries!($($tail)*);
};
}
entries! {
//
int Sex { Male, Female }
struct CharacterConfig {
sex: Sex,
}
struct AiConfig {
character: CharacterConfig,
}
//
enum TelegramPullMethod {
#[serde(rename = "longpoll")]
LongPoll {
wait_secs: NonZeroUsize,
limit: NonZeroU8,
},
#[serde(rename = "webhook")]
WebHook {
endpoint_path: String,
secret_token: String,
}
}
struct TelegramConfig {
token: String,
pull: TelegramPullMethod,
}
struct BotConfig {
#[serde(with = "serde_regex")]
prefix: Regex,
telegram: TelegramConfig,
}
struct Config {
bot: BotConfig,
ai: AiConfig,
}
}
impl Config {
pub fn load(path: impl AsRef<Path>) -> Self {
let content = fs::read_to_string(path).expect("Failed to read config");
toml::from_str(&content).expect("Failed to parse config")
}
}
[rt]
workers = 4
[bot]
prefix = "(?i)^(м[еэ][ий]да?|медуза?|горни[чш]ная?)[\\s,]*"
[bot.telegram]
token = "Telegram token"
[bot.telegram.pull.longpoll]
wait_secs = 30
limit = 100
[ai.character]
sex = "female"
# — WebHook
# [bot.telegram.pull.webhook]
# endpoint_path = “/telegram”
# secret_token = “Meduso”
pub mod config;
use maid::config::Config;
fn main() {
let config = Config::load("assets/config.toml");
dbg!(config);
}
@nerodono
Copy link
Author

Output:

[bin/main.rs:5] config = Config {
    bot: BotConfig {
        prefix: (?i)^(м[еэ][ий]да?|медуза?|горни[чш]ная?)[\s,]*,
        telegram: TelegramConfig {
            token: "Telegram token",
            pull: LongPoll {
                wait_secs: 30,
                limit: 100,
            },
        },
    },
    ai: AiConfig {
        character: CharacterConfig {
            sex: Female,
        },
    },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment