refactor: adapters configured from config crate
This commit is contained in:
@@ -18,11 +18,8 @@ pub struct ConversionConfig {
|
||||
}
|
||||
|
||||
impl ConversionConfig {
|
||||
pub fn from_env() -> anyhow::Result<Option<Self>> {
|
||||
Self::from_vars(
|
||||
std::env::var("IMAGE_CONVERSION_ENABLED").ok().as_deref(),
|
||||
std::env::var("IMAGE_CONVERSION_FORMAT").ok().as_deref(),
|
||||
)
|
||||
pub fn from_config(config: &::config::ImageConversionConfig) -> anyhow::Result<Option<Self>> {
|
||||
Self::from_vars(config.enabled.then_some("true"), config.format.as_deref())
|
||||
}
|
||||
|
||||
fn from_vars(enabled: Option<&str>, format: Option<&str>) -> anyhow::Result<Option<Self>> {
|
||||
|
||||
@@ -14,12 +14,13 @@ use std::sync::Arc;
|
||||
type ConversionPair = (Arc<dyn EventHandler>, Arc<dyn PeriodicJob>);
|
||||
|
||||
pub fn build(
|
||||
config: &::config::ImageConversionConfig,
|
||||
object_storage: Arc<dyn ObjectStorage>,
|
||||
image_ref_command: Arc<dyn ImageRefCommand>,
|
||||
image_ref_query: Arc<dyn ImageRefQuery>,
|
||||
event_publisher: Arc<dyn EventPublisher>,
|
||||
) -> anyhow::Result<Option<ConversionPair>> {
|
||||
let config = match ConversionConfig::from_env()? {
|
||||
let config = match ConversionConfig::from_config(config)? {
|
||||
Some(c) => c,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
@@ -43,3 +43,15 @@ fn avif_extension() {
|
||||
fn webp_extension() {
|
||||
assert_eq!(Format::Webp.extension(), ".webp");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_config_follows_section() {
|
||||
let disabled = ::config::ImageConversionConfig::default();
|
||||
assert!(ConversionConfig::from_config(&disabled).unwrap().is_none());
|
||||
let enabled = ::config::ImageConversionConfig {
|
||||
enabled: true,
|
||||
format: Some("webp".into()),
|
||||
};
|
||||
let cfg = ConversionConfig::from_config(&enabled).unwrap().unwrap();
|
||||
assert_eq!(cfg.format, Format::Webp);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user