fix: config empty values unset, drop adapter from_vars

This commit is contained in:
2026-10-25 21:18:16 +01:00
parent 9bf9342fc0
commit a00a5dc655
6 changed files with 83 additions and 88 deletions

View File

@@ -19,24 +19,19 @@ pub struct ConversionConfig {
impl ConversionConfig {
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>> {
if enabled != Some("true") {
if !config.enabled {
return Ok(None);
}
let format_str = format.ok_or_else(|| {
anyhow::anyhow!("IMAGE_CONVERSION_FORMAT required when IMAGE_CONVERSION_ENABLED=true")
})?;
let format = match format_str {
"avif" => Format::Avif,
"webp" => Format::Webp,
other => anyhow::bail!(
let format = match config.format.as_deref() {
Some("avif") => Format::Avif,
Some("webp") => Format::Webp,
Some(other) => anyhow::bail!(
"Unknown IMAGE_CONVERSION_FORMAT: {other:?}. Valid values: avif, webp"
),
None => {
anyhow::bail!("IMAGE_CONVERSION_FORMAT required when IMAGE_CONVERSION_ENABLED=true")
}
};
Ok(Some(Self { format }))