diff --git a/Cargo.lock b/Cargo.lock index 7249588..b86af2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1591,7 +1591,7 @@ dependencies = [ [[package]] name = "pict-rs" -version = "0.4.0-beta.8" +version = "0.4.0-beta.9" dependencies = [ "actix-form-data", "actix-rt", @@ -2028,9 +2028,9 @@ checksum = "58bc9567378fc7690d6b2addae4e60ac2eeea07becb2c64b9f218b53865cba2a" [[package]] name = "serde" -version = "1.0.151" +version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97fed41fc1a24994d044e6db6935e69511a1153b52c15eb42493b26fa87feba0" +checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" dependencies = [ "serde_derive", ] @@ -2047,9 +2047,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.151" +version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "255abe9a125a985c05190d687b320c12f9b1f0b99445e608c21ba0782c719ad8" +checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 788a79e..fade6f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "pict-rs" description = "A simple image hosting service" -version = "0.4.0-beta.8" +version = "0.4.0-beta.9" authors = ["asonix "] license = "AGPL-3.0" readme = "README.md" diff --git a/src/config.rs b/src/config.rs index 70021af..99b20a4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,5 @@ use clap::Parser; -use std::path::Path; +use std::path::{Path, PathBuf}; mod commandline; mod defaults; @@ -16,16 +16,53 @@ pub(crate) use primitives::{ AudioCodec, Filesystem, ImageFormat, LogFormat, ObjectStorage, Store, VideoCodec, }; -pub(crate) fn configure_without_clap, Q: AsRef>( - config_file: Option

, +/// Source for pict-rs configuration when embedding as a library +pub enum ConfigSource { + /// 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 ConfigSource +where + T: serde::Serialize, +{ + /// Create a new memory source + pub fn memory(values: T) -> Self { + Self::Memory { values } + } +} + +impl

ConfigSource +where + P: AsRef, +{ + /// Create a new file source + pub fn file(path: P) -> Self { + Self::File { path } + } +} + +impl ConfigSource { + /// Create a new empty source + pub fn empty() -> Self { + Self::Empty + } +} + +pub(crate) fn configure_without_clap, T: serde::Serialize, Q: AsRef>( + source: ConfigSource, save_to: Option, ) -> color_eyre::Result<(Configuration, Operation)> { 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.as_ref())) - } else { - config + 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)?), }; let built = config diff --git a/src/lib.rs b/src/lib.rs index dc8710e..efed8dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,6 +73,8 @@ use self::{ stream::{StreamLimit, StreamTimeout}, }; +pub use self::config::ConfigSource; + const MEGABYTES: usize = 1024 * 1024; const MINUTES: u32 = 60; const HOURS: u32 = 60 * MINUTES; @@ -1162,28 +1164,50 @@ where Ok(()) } -/// Initialize the pict-rs configuration -/// -/// This takes an optional config_file path which is a valid pict-rs configuration file, and an -/// optional save_to path, which the generated configuration will be saved into. Since many -/// parameters have defaults, it can be useful to dump a valid configuration with default values to -/// see what is available for tweaking. -/// -/// This function must be called before `run` or `install_tracing` -/// -/// When running pict-rs as a library, configuration is limited to environment variables and -/// configuration files. Commandline options are not available. -pub fn init_config, Q: AsRef>( - config_file: Option

, - save_to: Option, -) -> color_eyre::Result<()> { - let (config, operation) = config::configure_without_clap(config_file, save_to)?; +impl, T: serde::Serialize> ConfigSource { + /// Initialize the pict-rs configuration + /// + /// This takes an optional config_file path which is a valid pict-rs configuration file, and an + /// optional save_to path, which the generated configuration will be saved into. Since many + /// parameters have defaults, it can be useful to dump a valid configuration with default values to + /// see what is available for tweaking. + /// + /// This function must be called before `run` or `install_tracing` + /// + /// When running pict-rs as a library, configuration is limited to environment variables and + /// configuration files. Commandline options are not available. + /// + /// ```rust + /// fn main() -> Result<(), Box> { + /// pict_rs::ConfigSource::memory(serde_json::json!({ + /// "server": { + /// "address": "127.0.0.1:8080" + /// }, + /// "old_db": { + /// "path": "./old" + /// }, + /// "repo": { + /// "type": "sled", + /// "path": "./sled-repo" + /// }, + /// "store": { + /// "type": "filesystem", + /// "path": "./files" + /// } + /// })).init::<&str>(None)?; + /// + /// Ok(()) + /// } + /// ``` + pub fn init>(self, save_to: Option) -> color_eyre::Result<()> { + let (config, operation) = config::configure_without_clap(self, save_to)?; - DO_CONFIG - .set((config, operation)) - .unwrap_or_else(|_| panic!("CONFIG cannot be initialized more than once")); + DO_CONFIG + .set((config, operation)) + .unwrap_or_else(|_| panic!("CONFIG cannot be initialized more than once")); - Ok(()) + Ok(()) + } } /// Install the default pict-rs tracer