Skip to main content

atfits_rs/
path.rs

1//! Output path construction.
2use std::path::{Path, PathBuf};
3
4use crate::error::{AtfitsError, Result};
5
6/// Build an output path from `input`.
7///
8/// `img.fits` with suffix `smooth` gives `img.smooth.fits`; `prefix` is
9/// prepended to the file name and `outdir` replaces the directory.
10pub fn output_path(
11    input: &Path,
12    suffix: Option<&str>,
13    prefix: Option<&str>,
14    outdir: Option<&Path>,
15) -> PathBuf {
16    let stem = input.file_stem().unwrap_or_default().to_string_lossy();
17    let mut name = format!("{}{stem}", prefix.unwrap_or_default());
18    if let Some(s) = suffix {
19        name = format!("{name}.{s}");
20    }
21    if let Some(ext) = input.extension() {
22        name = format!("{name}.{}", ext.to_string_lossy());
23    }
24    let dir = outdir.unwrap_or_else(|| input.parent().unwrap_or(Path::new(".")));
25    dir.join(name)
26}
27
28/// `path` as UTF-8, which `fitsio` requires (it panics otherwise).
29pub(crate) fn utf8(path: &Path) -> Result<&str> {
30    path.to_str()
31        .ok_or_else(|| AtfitsError::Other(format!("path is not UTF-8: {}", path.display())))
32}