2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 11:21:24 +00:00

Extract preprocess steps as urlencoded in config deserialization

This commit is contained in:
asonix 2023-07-22 11:38:09 -05:00
parent ac48003f45
commit e1262a5fda

View file

@ -3,7 +3,6 @@ use crate::{
formats::{AnimationFormat, AudioCodec, ImageFormat, VideoCodec}, formats::{AnimationFormat, AudioCodec, ImageFormat, VideoCodec},
serde_str::Serde, serde_str::Serde,
}; };
use once_cell::sync::OnceCell;
use std::{collections::BTreeSet, net::SocketAddr, path::PathBuf}; use std::{collections::BTreeSet, net::SocketAddr, path::PathBuf};
use url::Url; use url::Url;
@ -160,7 +159,7 @@ pub(crate) struct Media {
pub(crate) max_file_size: usize, pub(crate) max_file_size: usize,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub(crate) preprocess_steps: Option<String>, preprocess_steps: Option<PreprocessSteps>,
pub(crate) filters: BTreeSet<String>, pub(crate) filters: BTreeSet<String>,
@ -351,22 +350,45 @@ pub(crate) struct VideoQuality {
crf_max: u8, crf_max: u8,
} }
#[derive(Clone, Debug)]
struct PreprocessSteps {
inner: Vec<(String, String)>,
}
impl<'de> serde::Deserialize<'de> for PreprocessSteps {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::de::Error;
let s = String::deserialize(deserializer)?;
let inner: Vec<(String, String)> =
serde_urlencoded::from_str(&s).map_err(D::Error::custom)?;
Ok(PreprocessSteps { inner })
}
}
impl serde::Serialize for PreprocessSteps {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::Error;
let s = serde_urlencoded::to_string(&self.inner).map_err(S::Error::custom)?;
s.serialize(serializer)
}
}
impl Media { impl Media {
pub(crate) fn preprocess_steps(&self) -> Option<&[(String, String)]> { pub(crate) fn preprocess_steps(&self) -> Option<&[(String, String)]> {
static PREPROCESS_STEPS: OnceCell<Vec<(String, String)>> = OnceCell::new(); self.preprocess_steps
.as_ref()
if let Some(steps) = &self.preprocess_steps { .map(|steps| steps.inner.as_slice())
let steps = PREPROCESS_STEPS
.get_or_try_init(|| {
serde_urlencoded::from_str(steps) as Result<Vec<(String, String)>, _>
})
.expect("Invalid preprocess_steps configuration")
.as_slice();
Some(steps)
} else {
None
}
} }
} }