use std::fs;
use std::io::Write;
use std::path::Path;
use fluxframe_core::{FluxError, Preset};
use toml_edit::{DocumentMut, Item, Table};
pub fn save_preset(file: &Path, preset_name: &str, preset: &Preset) -> Result<(), FluxError> {
validate_preset_name(preset_name)?;
let existing_text = if file.exists() {
fs::read_to_string(file).map_err(|e| FluxError::io(file.to_path_buf(), e))?
} else {
"# fluxframe configuration\n# Generated on first Save from the GUI. \
Edits made here are honoured by the daemon on reload.\n"
.to_string()
};
let stripped = strip_preset_sections(&existing_text, preset_name);
let preset_text = serialise_preset_block(preset_name, preset)?;
let mut combined = stripped;
if !combined.is_empty() && !combined.ends_with('\n') {
combined.push('\n');
}
if !combined.is_empty() && !combined.ends_with("\n\n") {
combined.push('\n');
}
combined.push_str(&preset_text);
if !combined.ends_with('\n') {
combined.push('\n');
}
let _check: DocumentMut =
combined
.parse()
.map_err(|e: toml_edit::TomlError| FluxError::Config {
reason: format!("internal: produced TOML did not re-parse: {e}"),
hint: None,
})?;
atomic_write(file, combined.as_bytes())
}
fn strip_preset_sections(text: &str, preset_name: &str) -> String {
let exact = format!("[presets.{preset_name}]");
let dotted = format!("[presets.{preset_name}.");
let mut blocks: Vec<&str> = Vec::new();
let mut start = 0usize;
for (idx, line) in text.match_indices('\n') {
let after_newline = idx + 1;
if after_newline >= text.len() {
continue;
}
let rest = &text[after_newline..];
if rest.starts_with('[') {
let _ = line;
blocks.push(&text[start..after_newline]);
start = after_newline;
}
}
if start < text.len() {
blocks.push(&text[start..]);
}
let mut kept: Vec<&str> = Vec::with_capacity(blocks.len());
for block in blocks {
let header_line = block
.lines()
.find(|l| {
let t = l.trim_start();
!t.is_empty() && !t.starts_with('#')
})
.unwrap_or("");
let stripped_header = header_line.trim_start();
let drop = stripped_header == exact || stripped_header.starts_with(&dotted);
if !drop {
kept.push(block);
}
}
kept.concat()
}
fn serialise_preset_block(preset_name: &str, preset: &Preset) -> Result<String, FluxError> {
let mut doc = DocumentMut::new();
let mut presets_tbl = Table::new();
presets_tbl.set_implicit(true);
presets_tbl.insert(preset_name, preset_to_item(preset)?);
doc.insert("presets", Item::Table(presets_tbl));
Ok(doc.to_string())
}
pub fn save_preset_as(file: &Path, new_name: &str, preset: &Preset) -> Result<(), FluxError> {
validate_preset_name(new_name)?;
if file.exists() {
let doc = read_document(file)?;
if let Some(table) = doc.get("presets").and_then(Item::as_table)
&& table.contains_key(new_name)
{
return Err(FluxError::Config {
reason: format!("preset '{new_name}' already exists"),
hint: Some(
"pick a different name, or use the regular Save button to overwrite the active preset"
.into(),
),
});
}
}
save_preset(file, new_name, preset)
}
fn validate_preset_name(name: &str) -> Result<(), FluxError> {
if name.is_empty() {
return Err(FluxError::Config {
reason: "preset name is empty".into(),
hint: Some(
"preset names must be non-empty identifiers (letters, digits, _ or -)".into(),
),
});
}
if !name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
{
return Err(FluxError::Config {
reason: format!("preset name '{name}' contains invalid characters"),
hint: Some("use letters, digits, underscores or dashes only".into()),
});
}
Ok(())
}
fn read_document(file: &Path) -> Result<DocumentMut, FluxError> {
let text = fs::read_to_string(file).map_err(|e| FluxError::io(file.to_path_buf(), e))?;
text.parse::<DocumentMut>().map_err(|e| FluxError::Config {
reason: format!("config file is not valid TOML: {e}"),
hint: Some(format!("path: {}", file.display())),
})
}
fn preset_to_item(preset: &Preset) -> Result<Item, FluxError> {
let normalised = normalise_preset(preset);
let text = toml::to_string(&normalised).map_err(|e| FluxError::Config {
reason: format!("serialise preset: {e}"),
hint: None,
})?;
let parsed: DocumentMut = text.parse().map_err(|e| FluxError::Config {
reason: format!("re-parse serialised preset: {e}"),
hint: None,
})?;
let mut table = parsed.as_table().clone();
table.set_implicit(false);
Ok(Item::Table(table))
}
fn atomic_write(path: &Path, content: &[u8]) -> Result<(), FluxError> {
let parent = path.parent().ok_or_else(|| FluxError::Config {
reason: format!("config path {} has no parent directory", path.display()),
hint: None,
})?;
if !parent.as_os_str().is_empty() {
fs::create_dir_all(parent).map_err(|e| FluxError::io(parent.to_path_buf(), e))?;
}
let tmp = staging_path(path);
{
let mut f = fs::File::create(&tmp).map_err(|e| FluxError::io(tmp.clone(), e))?;
f.write_all(content)
.map_err(|e| FluxError::io(tmp.clone(), e))?;
f.sync_all().map_err(|e| FluxError::io(tmp.clone(), e))?;
}
if let Err(e) = fs::rename(&tmp, path) {
let _ = fs::remove_file(&tmp);
return Err(FluxError::io(path.to_path_buf(), e));
}
Ok(())
}
fn normalise_preset(preset: &Preset) -> Preset {
let mut out = preset.clone();
for slot in [
out.mask.as_mut(),
out.background.as_mut(),
out.foreground.as_mut(),
out.post.as_mut(),
]
.into_iter()
.flatten()
{
slot.per_effect.retain(|key, _| {
slot.chain.iter().any(|n| n == key)
|| fluxframe_core::PipelineSection::is_reserved_key(key)
});
}
out
}
fn staging_path(path: &Path) -> std::path::PathBuf {
let mut name = path
.file_name()
.map(std::ffi::OsString::from)
.unwrap_or_default();
name.push(format!(".tmp.{}", std::process::id()));
match path.parent() {
Some(p) => p.join(name),
None => std::path::PathBuf::from(name),
}
}
#[cfg(test)]
mod tests {
use super::*;
use fluxframe_core::PipelineSection;
use std::collections::BTreeMap;
use tempfile::TempDir;
fn sample_preset() -> Preset {
let mut per_effect: BTreeMap<String, toml::Value> = BTreeMap::new();
let mut blur_table = toml::Table::new();
blur_table.insert("radius".into(), toml::Value::Integer(40));
per_effect.insert("blur".into(), toml::Value::Table(blur_table));
Preset {
fallback_preset: None,
mask: None,
background: Some(PipelineSection {
chain: vec!["blur".into()],
per_effect,
..PipelineSection::default()
}),
foreground: None,
post: None,
}
}
#[test]
fn save_preset_into_missing_file_creates_parent_dirs() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("nested").join("fluxframe.toml");
save_preset(&path, "default", &sample_preset()).expect("save into nested path");
assert!(path.exists(), "config file was not created");
let text = fs::read_to_string(&path).unwrap();
assert!(
text.contains("[presets.default"),
"preset header missing in:\n{text}"
);
}
#[test]
fn save_preset_preserves_unrelated_sections_and_comments() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
let initial = "\
# Top-level comment kept across saves.
[input]
device = \"/dev/video2\" # inline comment
[presets.default]
[presets.default.background]
chain = [\"vignette\"]
[presets.default.background.vignette]
strength = 0.3
";
fs::write(&path, initial).unwrap();
save_preset(&path, "default", &sample_preset()).expect("overwrite default preset");
let after = fs::read_to_string(&path).unwrap();
assert!(
after.contains("# Top-level comment kept across saves."),
"leading comment lost:\n{after}"
);
assert!(
after.contains("device = \"/dev/video2\""),
"[input] table lost:\n{after}"
);
assert!(
after.contains("# inline comment"),
"inline comment lost:\n{after}"
);
assert!(after.contains("radius = 40"), "new value missing:\n{after}");
assert!(
!after.contains("strength = 0.3"),
"stale preset data not replaced:\n{after}"
);
}
#[test]
fn save_preset_round_trips_through_load() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
save_preset(&path, "demo", &sample_preset()).unwrap();
let cfg = fluxframe_core::FluxConfig::from_toml_str(&fs::read_to_string(&path).unwrap())
.expect("re-parse saved file");
let demo = cfg.presets.get("demo").expect("demo preset present");
let bg = demo.background.as_ref().expect("background present");
assert_eq!(bg.chain, vec!["blur".to_string()]);
let blur_cfg = bg
.per_effect
.get("blur")
.and_then(toml::Value::as_table)
.unwrap();
assert_eq!(
blur_cfg.get("radius").and_then(toml::Value::as_integer),
Some(40)
);
}
#[test]
fn fallback_preset_round_trips_through_save() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
save_preset(&path, "lite", &sample_preset()).unwrap();
let demo = Preset {
fallback_preset: Some("lite".into()),
..sample_preset()
};
save_preset(&path, "demo", &demo).unwrap();
let cfg = fluxframe_core::FluxConfig::from_toml_str(&fs::read_to_string(&path).unwrap())
.expect("re-parse saved file");
assert_eq!(cfg.presets.get("demo"), Some(&demo));
}
#[test]
fn disabled_flag_round_trips_through_save() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
let mut preset = sample_preset();
preset
.background
.as_mut()
.and_then(|bg| bg.per_effect.get_mut("blur"))
.and_then(toml::Value::as_table_mut)
.expect("sample preset has [background.blur]")
.insert("enabled".into(), toml::Value::Boolean(false));
save_preset(&path, "demo", &preset).unwrap();
let cfg = fluxframe_core::FluxConfig::from_toml_str(&fs::read_to_string(&path).unwrap())
.expect("re-parse saved file");
let blur = cfg.presets["demo"].background.as_ref().unwrap().per_effect["blur"]
.as_table()
.expect("blur table");
assert_eq!(blur.get("enabled"), Some(&toml::Value::Boolean(false)));
assert_eq!(
blur.get("radius").and_then(toml::Value::as_integer),
Some(40)
);
}
#[test]
fn save_preset_as_rejects_existing_name() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
save_preset(&path, "existing", &sample_preset()).unwrap();
let err = save_preset_as(&path, "existing", &sample_preset())
.expect_err("duplicate name should fail");
match err {
FluxError::Config { reason, .. } => assert!(
reason.contains("existing"),
"error should mention the conflicting name: {reason}"
),
other => panic!("expected Config error, got {other}"),
}
}
#[test]
fn save_preset_as_appends_a_second_preset() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
save_preset(&path, "first", &sample_preset()).unwrap();
save_preset_as(&path, "second", &sample_preset()).unwrap();
let after = fs::read_to_string(&path).unwrap();
assert!(after.contains("[presets.first"), "first preset gone");
assert!(after.contains("[presets.second"), "second preset missing");
}
#[test]
fn save_preset_rejects_invalid_name() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
let err =
save_preset(&path, "has spaces", &sample_preset()).expect_err("space should fail");
assert!(matches!(err, FluxError::Config { .. }));
let err = save_preset(&path, "", &sample_preset()).expect_err("empty should fail");
assert!(matches!(err, FluxError::Config { .. }));
}
fn realistic_main_preset() -> Preset {
let mut mask_per_effect: BTreeMap<String, toml::Value> = BTreeMap::new();
let mut feather = toml::Table::new();
feather.insert("radius".into(), toml::Value::Integer(2));
mask_per_effect.insert("feather".into(), toml::Value::Table(feather));
let mut smooth = toml::Table::new();
smooth.insert("factor".into(), toml::Value::Float(0.85));
mask_per_effect.insert("smooth_temporal".into(), toml::Value::Table(smooth));
let mut threshold = toml::Table::new();
threshold.insert("level".into(), toml::Value::Float(0.8));
mask_per_effect.insert("threshold".into(), toml::Value::Table(threshold));
let mut bg_per_effect: BTreeMap<String, toml::Value> = BTreeMap::new();
let mut blur = toml::Table::new();
blur.insert("passes".into(), toml::Value::Integer(2));
blur.insert("radius".into(), toml::Value::Integer(10));
bg_per_effect.insert("blur".into(), toml::Value::Table(blur));
let mut vignette = toml::Table::new();
vignette.insert("strength".into(), toml::Value::Float(0.71));
vignette.insert("inner_radius".into(), toml::Value::Float(0.4));
bg_per_effect.insert("vignette".into(), toml::Value::Table(vignette));
let mut post_per_effect: BTreeMap<String, toml::Value> = BTreeMap::new();
let mut auto_frame = toml::Table::new();
auto_frame.insert("threshold".into(), toml::Value::Float(0.43));
auto_frame.insert("smoothing".into(), toml::Value::Float(0.97));
auto_frame.insert("zoom_max".into(), toml::Value::Float(2.41));
post_per_effect.insert("auto_frame".into(), toml::Value::Table(auto_frame));
Preset {
fallback_preset: None,
mask: Some(PipelineSection {
chain: vec![
"smooth_temporal".into(),
"threshold".into(),
"largest_blob".into(),
"feather".into(),
],
model: Some("./models/selfie_segmentation.onnx".into()),
fallback_threshold: None,
per_effect: mask_per_effect,
}),
background: Some(PipelineSection {
chain: vec!["blur".into(), "vignette".into()],
per_effect: bg_per_effect,
..PipelineSection::default()
}),
foreground: None,
post: Some(PipelineSection {
chain: vec!["auto_frame".into(), "mirror".into()],
per_effect: post_per_effect,
..PipelineSection::default()
}),
}
}
#[test]
fn realistic_preset_full_round_trip() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
let original = realistic_main_preset();
save_preset(&path, "main", &original).expect("save realistic preset");
let text = fs::read_to_string(&path).unwrap();
for header in [
"[presets.main.mask]",
"[presets.main.background]",
"[presets.main.post]",
] {
assert!(
text.contains(header),
"missing top section header {header} in:\n{text}"
);
}
let cfg = fluxframe_core::FluxConfig::from_toml_str(&text)
.expect("saved file parses as FluxConfig");
let reloaded = cfg.presets.get("main").expect("main preset present");
assert_eq!(
reloaded, &original,
"round-trip changed the preset:\nbefore: {original:?}\nafter: {reloaded:?}"
);
}
#[test]
fn second_save_does_not_duplicate_sections() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
save_preset(&path, "main", &realistic_main_preset()).unwrap();
let mut p = realistic_main_preset();
if let Some(bg) = p.background.as_mut() {
let mut blur = toml::Table::new();
blur.insert("passes".into(), toml::Value::Integer(4));
blur.insert("radius".into(), toml::Value::Integer(20));
bg.per_effect
.insert("blur".into(), toml::Value::Table(blur));
}
save_preset(&path, "main", &p).unwrap();
let text = fs::read_to_string(&path).unwrap();
let mask_headers = text.matches("[presets.main.mask]").count();
assert_eq!(
mask_headers, 1,
"duplicated `[presets.main.mask]` header:\n{text}"
);
let cfg = fluxframe_core::FluxConfig::from_toml_str(&text).unwrap();
let bg = cfg
.presets
.get("main")
.and_then(|p| p.background.as_ref())
.expect("background present after second save");
let blur = bg
.per_effect
.get("blur")
.and_then(toml::Value::as_table)
.unwrap();
assert_eq!(
blur.get("passes").and_then(toml::Value::as_integer),
Some(4)
);
}
#[test]
fn save_groups_preset_subsections_together() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
let initial = "\
[control]
enabled = true
[presets.main.mask]
chain = [\"smooth_temporal\"]
[presets.main.mask.smooth_temporal]
factor = 0.5
[input]
device = \"/dev/video0\"
[presets.main.background]
chain = [\"blur\"]
[presets.main.background.blur]
radius = 30
[output]
device = \"/dev/video10\"
";
fs::write(&path, initial).unwrap();
save_preset(&path, "main", &realistic_main_preset()).unwrap();
let after = fs::read_to_string(&path).unwrap();
let positions = |needle: &str| -> Vec<usize> {
after.match_indices(needle).map(|(idx, _)| idx).collect()
};
let input_at = *positions("[input]").first().expect("[input] still present");
let output_at = *positions("[output]")
.first()
.expect("[output] still present");
let main_positions = positions("[presets.main");
assert!(!main_positions.is_empty(), "main preset headers gone");
let all_before_input = main_positions.iter().all(|&p| p < input_at);
let all_after_output = main_positions.iter().all(|&p| p > output_at);
let all_between = main_positions
.iter()
.all(|&p| p > input_at && p < output_at)
|| main_positions.iter().all(|&p| {
p < input_at.min(output_at) || (p > input_at.max(output_at))
});
assert!(
all_before_input || all_after_output || all_between,
"preset.main sub-sections are interleaved with [input]/[output]:\n{after}"
);
let cfg = fluxframe_core::FluxConfig::from_toml_str(&after).unwrap();
assert_eq!(cfg.presets.get("main"), Some(&realistic_main_preset()));
}
#[test]
fn save_prunes_orphan_per_effect_entries() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
let mut p = sample_preset();
if let Some(bg) = p.background.as_mut() {
let mut pixelate = toml::Table::new();
pixelate.insert("block_size".into(), toml::Value::Integer(16));
bg.per_effect
.insert("pixelate".into(), toml::Value::Table(pixelate));
}
save_preset(&path, "main", &p).unwrap();
let text = fs::read_to_string(&path).unwrap();
assert!(
!text.contains("[presets.main.background.pixelate]"),
"orphan sub-table reached disk:\n{text}"
);
let cfg = fluxframe_core::FluxConfig::from_toml_str(&text).unwrap();
let bg = cfg
.presets
.get("main")
.and_then(|x| x.background.as_ref())
.expect("bg present");
assert!(
!bg.per_effect.contains_key("pixelate"),
"orphan reached the loaded Preset"
);
}
#[test]
fn empty_section_round_trips() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
let p = Preset {
background: Some(PipelineSection::default()),
..Preset::default()
};
save_preset(&path, "empty", &p).unwrap();
let cfg =
fluxframe_core::FluxConfig::from_toml_str(&fs::read_to_string(&path).unwrap()).unwrap();
let reloaded = cfg.presets.get("empty").unwrap();
if let Some(bg) = &reloaded.background {
assert_eq!(bg.chain, Vec::<String>::new());
assert!(bg.per_effect.is_empty());
}
}
#[test]
fn mask_model_path_survives_round_trip() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
let mut mask_per_effect: BTreeMap<String, toml::Value> = BTreeMap::new();
let mut threshold = toml::Table::new();
threshold.insert("level".into(), toml::Value::Float(0.7));
mask_per_effect.insert("threshold".into(), toml::Value::Table(threshold));
let preset = Preset {
mask: Some(PipelineSection {
chain: vec!["threshold".into()],
model: Some("./models/selfie.onnx".into()),
fallback_threshold: Some(3),
per_effect: mask_per_effect,
}),
..Preset::default()
};
save_preset(&path, "demo", &preset).unwrap();
let cfg =
fluxframe_core::FluxConfig::from_toml_str(&fs::read_to_string(&path).unwrap()).unwrap();
let reloaded = cfg.presets.get("demo").unwrap();
let mask = reloaded.mask.as_ref().expect("mask present");
assert_eq!(
mask.model.as_deref().and_then(std::path::Path::to_str),
Some("./models/selfie.onnx")
);
assert_eq!(mask.fallback_threshold, Some(3));
}
#[test]
fn strip_leaves_other_presets_intact() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
let initial = "\
[presets.main]
[presets.main.background]
chain = [\"blur\"]
[presets.main.background.blur]
radius = 5
[presets.other]
[presets.other.background]
chain = [\"vignette\"]
[presets.other.background.vignette]
strength = 0.2
";
fs::write(&path, initial).unwrap();
save_preset(&path, "main", &sample_preset()).unwrap();
let after = fs::read_to_string(&path).unwrap();
assert!(after.contains("[presets.other.background]"));
assert!(after.contains("strength = 0.2"));
let cfg = fluxframe_core::FluxConfig::from_toml_str(&after).unwrap();
assert_eq!(
cfg.presets
.get("main")
.and_then(|p| p.background.as_ref())
.map(|s| s.chain.clone()),
Some(vec!["blur".to_string()])
);
assert_eq!(
cfg.presets
.get("other")
.and_then(|p| p.background.as_ref())
.map(|s| s.chain.clone()),
Some(vec!["vignette".to_string()])
);
}
#[test]
fn strip_does_not_match_prefix_names() {
let initial = "\
[presets.main]
[presets.main.background]
chain = [\"blur\"]
[presets.mainline]
[presets.mainline.background]
chain = [\"vignette\"]
";
let stripped = strip_preset_sections(initial, "main");
assert!(
!stripped.contains("[presets.main.background]"),
"main not stripped:\n{stripped}"
);
assert!(
!stripped.contains("[presets.main]\n"),
"main parent not stripped:\n{stripped}"
);
assert!(
stripped.contains("[presets.mainline]"),
"mainline lost:\n{stripped}"
);
assert!(
stripped.contains("[presets.mainline.background]"),
"mainline.background lost:\n{stripped}"
);
}
#[test]
fn atomic_write_leaves_no_tmp_file_on_success() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("cfg.toml");
save_preset(&path, "default", &sample_preset()).unwrap();
let entries: Vec<_> = fs::read_dir(tmp.path())
.unwrap()
.filter_map(Result::ok)
.map(|e| e.file_name().to_string_lossy().into_owned())
.collect();
for name in &entries {
assert!(
!name.contains(".tmp."),
"found leftover staging file {name} in {entries:?}"
);
}
}
}