aob-lemmy-bot/src/config.rs

94 lines
2.5 KiB
Rust
Raw Normal View History

2023-12-30 01:27:11 +01:00
use crate::config::PostBody::Description;
use lemmy_api_common::sensitive::Sensitive;
use serde_derive::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub(crate) struct Config {
pub(crate) instance: String,
username: String,
password: String,
pub(crate) status_post_url: Option<String>,
pub(crate) config_reload_seconds: u32,
2023-12-18 11:23:47 +01:00
pub(crate) protected_communities: Vec<String>,
pub(crate) series: Vec<SeriesConfig>,
}
impl Config {
2023-12-29 22:35:16 +01:00
pub(crate) fn load() -> Self {
let cfg: Self = match confy::load(env!("CARGO_PKG_NAME"), "config") {
2023-12-17 22:29:18 +01:00
Ok(data) => data,
Err(e) => panic!("config.toml not found: {e}"),
2023-12-17 22:29:18 +01:00
};
2023-12-17 01:42:41 +01:00
if cfg.instance.is_empty() {
panic!("bot instance not set!")
}
if cfg.username.is_empty() {
panic!("bot username not set!")
}
if cfg.password.is_empty() {
panic!("bot password not provided!")
}
cfg.series.iter().for_each(|series| {
if series.prepub_community.post_body == Description {
panic!("'Description' type Post Body only supported for Volumes!")
}
});
2023-12-29 22:35:16 +01:00
cfg
2023-06-22 22:08:10 +02:00
}
pub(crate) fn get_username(&self) -> Sensitive<String> {
Sensitive::new(self.username.clone())
}
pub(crate) fn get_password(&self) -> Sensitive<String> {
Sensitive::new(self.password.clone())
}
}
2023-12-17 01:42:41 +01:00
impl Default for Config {
fn default() -> Self {
Config {
instance: "".to_owned(),
username: "".to_owned(),
password: "".to_owned(),
2023-12-17 01:42:41 +01:00
status_post_url: None,
config_reload_seconds: 21600,
2023-12-18 11:23:47 +01:00
protected_communities: vec![],
2023-12-30 01:27:11 +01:00
series: vec![],
}
}
}
2023-12-17 01:42:41 +01:00
#[derive(Debug, Serialize, Deserialize, Clone)]
pub(crate) struct SeriesConfig {
pub(crate) slug: String,
pub(crate) parted: bool,
pub(crate) prepub_community: PostConfig,
pub(crate) volume_community: PostConfig,
2023-12-17 01:42:41 +01:00
}
2023-06-22 22:08:10 +02:00
2023-12-17 01:42:41 +01:00
#[derive(Debug, Serialize, Deserialize, Clone)]
pub(crate) struct PostConfig {
2023-12-17 20:17:00 +01:00
pub(crate) name: String,
pub(crate) pin_settings: PinConfig,
pub(crate) post_body: PostBody,
2023-12-17 01:42:41 +01:00
}
2023-06-22 22:08:10 +02:00
2023-12-17 01:42:41 +01:00
#[derive(Debug, Serialize, Deserialize, Clone)]
pub(crate) struct PinConfig {
pub(crate) pin_new_post_local: bool,
pub(crate) pin_new_post_community: bool,
2023-12-17 01:42:41 +01:00
}
2023-06-22 22:08:10 +02:00
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
2023-12-17 01:42:41 +01:00
#[serde(tag = "body_type", content = "body_content")]
pub(crate) enum PostBody {
None,
Description,
Custom(String),
2023-06-22 22:08:10 +02:00
}