ts_config/
lib.rs

1//! # `ts-config`
2//!
3//! Helpers for application config.
4
5mod load;
6
7use std::{fs, io, path::PathBuf};
8
9use schemars::JsonSchema;
10use serde::{Serialize, de::DeserializeOwned};
11
12pub use load::{LoadConfigError, try_load};
13
14/// Trait defining a struct as representing a config file.
15pub trait ConfigFile: Default + DeserializeOwned + Serialize + JsonSchema {
16    /// The path to the config file.
17    fn config_file_path() -> PathBuf;
18
19    /// Delete the config file.
20    fn delete(&self) -> io::Result<()> {
21        fs::remove_file(Self::config_file_path())
22    }
23
24    /// Write the config file.
25    fn write(&self) -> io::Result<()> {
26        let json = serde_json::to_string_pretty(self).map_err(io::Error::other)?;
27        fs::write(Self::config_file_path(), json)
28    }
29}