muse2/output.rs
//! The module responsible for writing output data to disk.
use anyhow::{Context, Result};
use std::fs;
use std::path::{Path, PathBuf};
/// The root folder in which model-specific output folders will be created
const OUTPUT_DIRECTORY_ROOT: &str = "muse2_results";
/// Create a new output directory for the model specified at `model_dir`.
pub fn create_output_directory(model_dir: &Path) -> Result<PathBuf> {
// Get the model name from the dir path. This ends up being convoluted because we need to check
// for all possible errors. Ugh.
let model_dir = model_dir
.canonicalize() // canonicalise in case the user has specified "."
.context("Could not resolve path to model")?;
let model_name = model_dir
.file_name()
.context("Model cannot be in root folder")?
.to_str()
.context("Invalid chars in model dir name")?;
// Construct path
let path: PathBuf = [OUTPUT_DIRECTORY_ROOT, model_name].iter().collect();
if path.is_dir() {
// already exists
return Ok(path);
}
// Try to create the directory, with parents
fs::create_dir_all(&path)?;
Ok(path)
}