clemsbot/src/main.rs

64 lines
1.6 KiB
Rust
Raw Normal View History

2021-08-20 07:49:24 +00:00
#![feature(try_blocks, drain_filter)]
2021-08-18 05:45:44 +00:00
mod app;
2021-08-20 07:49:24 +00:00
use futures::FutureExt;
use tokio::{
fs::OpenOptions,
io::{AsyncReadExt, AsyncWriteExt},
runtime::{Builder, Handle},
};
2021-08-18 05:45:44 +00:00
use crate::app::{
State,
config::Config,
user_config::UserConfig,
};
2021-08-20 07:49:24 +00:00
use std::{
path::Path,
sync::Arc,
};
2021-08-18 05:45:44 +00:00
fn main() -> anyhow::Result<()> {
let runtime = Builder::new_multi_thread()
.enable_all()
.build()?;
let handle = runtime.handle().clone();
runtime.block_on(inner(handle))
}
async fn inner(runtime: Handle) -> anyhow::Result<()> {
let mut uc_toml = String::new();
tokio::fs::File::open("config.toml").await?.read_to_string(&mut uc_toml).await?;
let user_config: UserConfig = toml::from_str(&uc_toml)?;
let c_path = Path::new("data.json");
let config: Config = if c_path.exists() {
let mut c_json = String::new();
tokio::fs::File::open(c_path).await?.read_to_string(&mut c_json).await?;
serde_json::from_str(&c_json)?
} else {
Config::default()
};
let state = State::new(runtime, user_config, config).await?;
save_config(c_path, Arc::clone(&state)).await?;
tokio::signal::ctrl_c()
.then(|_| save_config(c_path, Arc::clone(&state)))
.await?;
Ok(())
}
async fn save_config(path: &Path, state: Arc<State>) -> anyhow::Result<()> {
let mut config_file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(path)
.await?;
config_file.write_all(&serde_json::to_vec(&*state.config.read().await)?).await?;
Ok(())
}