use std::env;
use std::path::{
Path,
PathBuf,
};
use anyhow::{
bail,
Context as _,
Result,
};
use midas::config::MidasConfig;
pub fn resolve(explicit: Option<&str>) -> Result<PathBuf> {
let cwd = env::current_dir().context("failed to read current directory")?;
resolve_in(&cwd, explicit)
}
pub fn resolve_in(dir: &Path, explicit: Option<&str>) -> Result<PathBuf> {
if let Some(value) = explicit.filter(|value| !value.is_empty()) {
return Ok(PathBuf::from(value));
}
let config_path = dir.join("midas.toml");
if config_path.is_file() {
let config = MidasConfig::from_path(&config_path)
.with_context(|| format!("failed to read `{}`", config_path.to_string_lossy()))?;
if let Some(value) = config.project.schema.as_deref().filter(|value| !value.is_empty()) {
return Ok(dir.join(value));
}
if let Some(value) = config
.project
.schema_dir
.as_deref()
.filter(|value| !value.is_empty())
{
return Ok(dir.join(value));
}
}
let toml_path = dir.join("schema.toml");
let midas_path = dir.join("schema.midas");
match (toml_path.is_file(), midas_path.is_file()) {
(true, true) => bail!(
"both `schema.toml` and `schema.midas` exist in `{}`; select one with `--schema <path>` or `[project].schema` in `midas.toml`",
dir.to_string_lossy()
),
(false, true) => Ok(midas_path),
_ => Ok(PathBuf::from("schema.toml")),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn write(dir: &Path, name: &str, text: &str) {
std::fs::write(dir.join(name), text).expect("write");
}
#[test]
fn defaults_to_schema_toml() {
let dir = tempfile::tempdir().expect("temp");
assert_eq!(
resolve_in(dir.path(), None).expect("resolve"),
Path::new("schema.toml")
);
}
#[test]
fn selects_the_only_present_default() {
let dir = tempfile::tempdir().expect("temp");
write(dir.path(), "schema.midas", "version 1\n");
assert_eq!(
resolve_in(dir.path(), None).expect("resolve"),
dir.path().join("schema.midas")
);
}
#[test]
fn conflicting_defaults_fail_without_a_selection() {
let dir = tempfile::tempdir().expect("temp");
write(dir.path(), "schema.toml", "version = 1\n");
write(dir.path(), "schema.midas", "version 1\n");
let error = resolve_in(dir.path(), None).expect_err("conflict");
let message = error.to_string();
assert!(
message.contains("both `schema.toml` and `schema.midas`"),
"{message}"
);
assert!(message.contains("--schema"), "{message}");
}
#[test]
fn explicit_flag_wins() {
let dir = tempfile::tempdir().expect("temp");
write(dir.path(), "schema.toml", "version = 1\n");
write(dir.path(), "custom.midas", "version 1\n");
assert_eq!(
resolve_in(dir.path(), Some("custom.midas")).expect("resolve"),
Path::new("custom.midas")
);
}
#[test]
fn project_schema_wins_over_defaults() {
let dir = tempfile::tempdir().expect("temp");
write(
dir.path(),
"midas.toml",
"[project]\nschema = \"db/schema.midas\"\n",
);
write(dir.path(), "schema.toml", "version = 1\n");
assert_eq!(
resolve_in(dir.path(), None).expect("resolve"),
dir.path().join("db/schema.midas")
);
}
#[test]
fn project_schema_dir_is_used_when_no_file_is_selected() {
let dir = tempfile::tempdir().expect("temp");
write(dir.path(), "midas.toml", "[project]\nschema_dir = \"db\"\n");
assert_eq!(
resolve_in(dir.path(), None).expect("resolve"),
dir.path().join("db")
);
}
}