2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-11-20 11:21:14 +00:00
pict-rs/src/config.rs

154 lines
3.9 KiB
Rust
Raw Normal View History

2022-03-28 04:27:07 +00:00
use clap::Parser;
use std::path::{Path, PathBuf};
2022-03-28 04:27:07 +00:00
2022-03-28 00:10:06 +00:00
mod commandline;
mod defaults;
mod file;
pub mod primitives;
2020-06-07 00:54:06 +00:00
2022-03-28 04:27:07 +00:00
use commandline::{Args, Output};
use config::Config;
use defaults::Defaults;
pub(crate) use commandline::Operation;
2023-02-04 23:32:36 +00:00
pub(crate) use file::{
2024-02-04 00:20:17 +00:00
Animation, ConfigFile as Configuration, Media, ObjectStorage, OpenTelemetry, Postgres, Repo,
Sled, Store, Tracing, Video,
};
pub(crate) use primitives::{Filesystem, LogFormat};
2022-03-28 04:27:07 +00:00
/// Source for pict-rs configuration when embedding as a library
pub enum ConfigSource<P, T> {
/// A File source for pict-rs configuration
File { path: P },
/// An in-memory source for pict-rs configuration
Memory { values: T },
/// No configuration
Empty,
}
impl<T> ConfigSource<PathBuf, T>
where
T: serde::Serialize,
{
/// Create a new memory source
pub fn memory(values: T) -> Self {
Self::Memory { values }
}
}
impl<P> ConfigSource<P, ()>
where
P: AsRef<Path>,
{
/// Create a new file source
pub fn file(path: P) -> Self {
Self::File { path }
}
}
impl ConfigSource<PathBuf, ()> {
/// Create a new empty source
pub fn empty() -> Self {
Self::Empty
}
}
// Instantiated configuration for the pict-rs application
#[derive(Debug)]
pub struct PictRsConfiguration {
pub(crate) config: Configuration,
pub(crate) operation: Operation,
}
pub(crate) fn configure_without_clap<P: AsRef<Path>, T: serde::Serialize, Q: AsRef<Path>>(
source: ConfigSource<P, T>,
2022-12-23 18:56:15 +00:00
save_to: Option<Q>,
) -> color_eyre::Result<PictRsConfiguration> {
2022-12-23 18:56:15 +00:00
let config = Config::builder().add_source(config::Config::try_from(&Defaults::default())?);
let config = match source {
ConfigSource::Empty => config,
ConfigSource::File { path } => config.add_source(config::File::from(path.as_ref())),
ConfigSource::Memory { values } => config.add_source(config::Config::try_from(&values)?),
2022-12-23 18:56:15 +00:00
};
2023-12-28 18:40:59 +00:00
let config = if let Ok(targets) = std::env::var("RUST_LOG") {
config.add_source(config::Config::try_from(&serde_json::json!({
"tracing": {
"logging": {
"targets": targets,
}
}
}))?)
} else {
config
};
2022-12-23 18:56:15 +00:00
let built = config
2023-01-04 20:42:04 +00:00
.add_source(
config::Environment::with_prefix("PICTRS")
.separator("__")
.try_parsing(true),
)
2022-12-23 18:56:15 +00:00
.build()?;
let operation = Operation::Run;
let config: Configuration = built.try_deserialize()?;
if let Some(save_to) = save_to {
let output = toml::to_string_pretty(&config)?;
std::fs::write(save_to, output)?;
}
Ok(PictRsConfiguration { config, operation })
2022-12-23 18:56:15 +00:00
}
pub(crate) fn configure() -> color_eyre::Result<PictRsConfiguration> {
2022-03-28 04:27:07 +00:00
let Output {
config_format,
operation,
save_to,
config_file,
} = Args::parse().into_output();
let config = Config::builder().add_source(config::Config::try_from(&Defaults::default())?);
let config = if let Some(config_file) = config_file {
config.add_source(config::File::from(config_file))
} else {
config
};
2023-12-28 18:40:59 +00:00
let config = if let Ok(targets) = std::env::var("RUST_LOG") {
config.add_source(config::Config::try_from(&serde_json::json!({
"tracing": {
"logging": {
"targets": targets,
}
}
}))?)
} else {
config
};
2022-03-28 04:27:07 +00:00
let built = config
2023-01-04 21:05:34 +00:00
.add_source(
config::Environment::with_prefix("PICTRS")
.separator("__")
.try_parsing(true),
)
2022-03-28 04:27:07 +00:00
.add_source(config::Config::try_from(&config_format)?)
.build()?;
let config: Configuration = built.try_deserialize()?;
2021-10-23 04:48:56 +00:00
2022-03-28 04:27:07 +00:00
if let Some(save_to) = save_to {
let output = toml::to_string_pretty(&config)?;
std::fs::write(save_to, output)?;
}
Ok(PictRsConfiguration { config, operation })
2020-06-07 00:54:06 +00:00
}