use fluxframe_core::paths::ConfigBase;
use fluxframe_core::traits::VideoEffect;
use fluxframe_core::{FluxConfig, FluxError, Preset};
use fluxframe_effects::{EffectChain, PassthroughEffect};
use tracing::info;
pub const DEFAULT_PRESET_NAME: &str = "default";
pub fn resolve<'a>(
cfg: &'a FluxConfig,
requested: Option<&str>,
) -> Result<(&'a str, &'a Preset), FluxError> {
let name = requested.unwrap_or(DEFAULT_PRESET_NAME);
if let Some((key, preset)) = cfg.presets.get_key_value(name) {
return Ok((key.as_str(), preset));
}
Err(resolution_error(cfg, requested))
}
fn resolution_error(cfg: &FluxConfig, requested: Option<&str>) -> FluxError {
let available: Vec<&str> = cfg.presets.keys().map(String::as_str).collect();
let reason = match requested {
Some(name) => format!("preset '{name}' is not defined in the config"),
None => format!("no --preset given and no '{DEFAULT_PRESET_NAME}' preset in the config"),
};
let hint = if available.is_empty() {
format!("add a [presets.{DEFAULT_PRESET_NAME}] section to your fluxframe.toml")
} else {
format!("available presets: {}", available.join(", "))
};
FluxError::Config {
reason,
hint: Some(hint),
}
}
pub fn build_chain(
name: &str,
preset: &Preset,
config_base: &ConfigBase,
) -> Result<EffectChain, FluxError> {
reject_planes_without_mask(name, preset)?;
if preset.mask.is_some() {
return build_composite_chain(name, preset, config_base);
}
Ok(build_passthrough_chain(name))
}
fn reject_planes_without_mask(name: &str, preset: &Preset) -> Result<(), FluxError> {
if preset.mask.is_some() {
return Ok(());
}
if preset.background.is_none() && preset.foreground.is_none() {
return Ok(());
}
let mut sections: Vec<&'static str> = Vec::new();
if preset.background.is_some() {
sections.push("background");
}
if preset.foreground.is_some() {
sections.push("foreground");
}
Err(FluxError::Config {
reason: format!(
"preset '{name}' declares [{}] but no [mask] section; \
plane sub-pipelines are only applied through the composite \
(which requires [mask])",
sections.join("] / ["),
),
hint: Some(
"add a [mask] section with a segmentation `model = \"...\"` path, \
or remove the background/foreground sections"
.into(),
),
})
}
fn build_passthrough_chain(name: &str) -> EffectChain {
info!(
preset = %name,
"preset has no [mask] section — running passthrough chain",
);
let effects: Vec<Box<dyn VideoEffect>> = vec![Box::new(PassthroughEffect::new())];
EffectChain::new(effects)
}
#[cfg(feature = "ml")]
fn build_composite_chain(
name: &str,
preset: &Preset,
config_base: &ConfigBase,
) -> Result<EffectChain, FluxError> {
use fluxframe_effects::composite::CompositeBuilder;
use fluxframe_effects::{mask_effects, plane_effects, post_effects};
let mask_section = preset
.mask
.as_ref()
.expect("build_composite_chain called without a [mask] section");
let mask_registry = mask_effects::default_registry();
let plane_registry = plane_effects::default_registry();
let post_registry = post_effects::default_registry();
let composite = CompositeBuilder::new(&mask_registry, &plane_registry, &post_registry)
.build(
mask_section,
preset.background.as_ref(),
preset.foreground.as_ref(),
preset.post.as_ref(),
config_base,
)
.map_err(FluxError::from)?;
info!(
preset = %name,
"preset has a [mask] section — running composite chain",
);
let effects: Vec<Box<dyn VideoEffect>> = vec![Box::new(composite)];
Ok(EffectChain::new(effects))
}
#[cfg(not(feature = "ml"))]
fn build_composite_chain(
name: &str,
_preset: &Preset,
_config_base: &ConfigBase,
) -> Result<EffectChain, FluxError> {
Err(FluxError::Config {
reason: format!(
"preset '{name}' has a [mask] sub-section but the binary was built \
without the `ml` feature"
),
hint: Some(
"rebuild with `--features fluxframe-effects/ml` or remove the mask \
section from the preset"
.into(),
),
})
}
#[cfg(test)]
mod tests {
use super::*;
use fluxframe_core::PipelineSection;
fn cfg_with_preset(name: &str, body: &str) -> FluxConfig {
let text = format!("[presets.{name}]\n{body}");
FluxConfig::from_toml_str(&text).expect("preset toml parses")
}
#[test]
fn resolve_default_when_none_given() {
let cfg = cfg_with_preset(DEFAULT_PRESET_NAME, "");
let (name, _preset) = resolve(&cfg, None).expect("default resolves");
assert_eq!(name, DEFAULT_PRESET_NAME);
}
#[test]
fn resolve_fails_when_no_default_and_no_flag() {
let cfg = cfg_with_preset("custom", "");
let err = resolve(&cfg, None).expect_err("missing default must surface an error");
let msg = format!("{err}");
assert!(
msg.contains(DEFAULT_PRESET_NAME),
"expected '{DEFAULT_PRESET_NAME}' mention: {msg}",
);
}
#[test]
fn resolve_fails_when_named_preset_missing() {
let cfg = cfg_with_preset(DEFAULT_PRESET_NAME, "");
let err = resolve(&cfg, Some("nope")).expect_err("missing requested preset must error");
let msg = format!("{err}");
assert!(msg.contains("'nope'"), "expected requested name: {msg}");
}
#[test]
fn resolve_picks_explicit_flag() {
let cfg = cfg_with_preset("custom", "");
let (name, _preset) = resolve(&cfg, Some("custom")).expect("custom resolves");
assert_eq!(name, "custom");
}
#[test]
fn build_chain_falls_back_to_passthrough_when_empty() {
let preset = Preset::default();
let chain = build_chain("raw", &preset, &ConfigBase::default())
.expect("empty preset → passthrough");
assert_eq!(chain.len(), 1);
assert_eq!(chain.names(), vec![PassthroughEffect::NAME]);
}
#[test]
fn build_chain_rejects_bg_without_mask() {
let preset = Preset {
background: Some(PipelineSection::default()),
..Preset::default()
};
let Err(err) = build_chain("bg-only", &preset, &ConfigBase::default()) else {
panic!("bg without mask must surface as a config error");
};
let msg = format!("{err}");
assert!(msg.contains("background"), "got: {msg}");
assert!(msg.contains("mask"), "got: {msg}");
}
#[test]
fn build_chain_rejects_fg_without_mask() {
let preset = Preset {
foreground: Some(PipelineSection::default()),
..Preset::default()
};
let Err(err) = build_chain("fg-only", &preset, &ConfigBase::default()) else {
panic!("fg without mask must surface as a config error");
};
let msg = format!("{err}");
assert!(msg.contains("foreground"), "got: {msg}");
assert!(msg.contains("mask"), "got: {msg}");
}
#[cfg(feature = "ml")]
fn segmentation_model_path() -> std::path::PathBuf {
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../../models/selfie_segmentation.onnx")
}
#[cfg(feature = "ml")]
#[test]
fn build_chain_with_mask_returns_composite_chain() {
use fluxframe_effects::CompositeEffect;
let mask = PipelineSection {
model: Some(segmentation_model_path()),
..PipelineSection::default()
};
let preset = Preset {
mask: Some(mask),
..Preset::default()
};
let Ok(chain) = build_chain("composite", &preset, &ConfigBase::default()) else {
panic!("mask present → composite chain should build");
};
assert_eq!(chain.len(), 1);
assert_eq!(chain.names(), vec![CompositeEffect::NAME]);
}
#[cfg(not(feature = "ml"))]
#[test]
fn build_chain_rejects_mask_without_ml_feature() {
let mask = PipelineSection {
model: Some(std::path::PathBuf::from("/tmp/dummy.onnx")),
..PipelineSection::default()
};
let preset = Preset {
mask: Some(mask),
..Preset::default()
};
let Err(err) = build_chain("composite", &preset, &ConfigBase::default()) else {
panic!("mask without ml feature must surface as a config error");
};
let msg = format!("{err}");
assert!(msg.contains("ml"), "expected 'ml' mention: {msg}");
}
}