Skip to main content

atfits_rs/
path.rs

1//! Output-path construction helpers.
2use std::path::{Path, PathBuf};
3
4/// Build an output path from `input` with an optional suffix/prefix/outdir.
5///
6/// With `suffix = Some("smooth")` and input `img.fits`, yields `img.smooth.fits`;
7/// `prefix` is prepended to the filename; `outdir` overrides the directory.
8pub fn output_path(
9    input: &Path,
10    suffix: Option<&str>,
11    prefix: Option<&str>,
12    outdir: Option<&Path>,
13) -> PathBuf {
14    let stem = input.file_stem().unwrap_or_default().to_string_lossy();
15    let ext = input.extension().unwrap_or_default().to_string_lossy();
16
17    let filename = match suffix {
18        Some(s) => format!("{stem}.{s}.{ext}"),
19        None => format!("{stem}.{ext}"),
20    };
21    let filename = match prefix {
22        Some(p) => format!("{p}{filename}"),
23        None => filename,
24    };
25
26    let dir = outdir.unwrap_or_else(|| input.parent().unwrap_or(Path::new(".")));
27    dir.join(filename)
28}