Created
March 25, 2023 07:33
-
-
Save nerodono/f022dfa07556e664dfc7be5397fde280 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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” |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub mod config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use maid::config::Config; | |
fn main() { | |
let config = Config::load("assets/config.toml"); | |
dbg!(config); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: