ts_config/
lib.rs

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