Skip to main content

midas_cli/commands/
source.rs

1//! Schema source selection shared by CLI commands.
2//!
3//! Precedence: explicit `--schema` flag, `[project].schema`, then
4//! `[project].schema_dir` from `midas.toml`, then the default file. When both
5//! `schema.toml` and `schema.midas` exist and nothing selected one, fail instead
6//! of guessing.
7
8use std::env;
9use std::path::{
10  Path,
11  PathBuf,
12};
13
14use anyhow::{
15  bail,
16  Context as _,
17  Result,
18};
19use midas::config::MidasConfig;
20
21/// Resolve the schema source for the current working directory.
22pub fn resolve(explicit: Option<&str>) -> Result<PathBuf> {
23  let cwd = env::current_dir().context("failed to read current directory")?;
24  resolve_in(&cwd, explicit)
25}
26
27/// Resolve the schema source relative to `dir`.
28pub fn resolve_in(dir: &Path, explicit: Option<&str>) -> Result<PathBuf> {
29  if let Some(value) = explicit.filter(|value| !value.is_empty()) {
30    return Ok(PathBuf::from(value));
31  }
32  let config_path = dir.join("midas.toml");
33  if config_path.is_file() {
34    let config = MidasConfig::from_path(&config_path)
35      .with_context(|| format!("failed to read `{}`", config_path.to_string_lossy()))?;
36    if let Some(value) = config.project.schema.as_deref().filter(|value| !value.is_empty()) {
37      return Ok(dir.join(value));
38    }
39    if let Some(value) = config
40      .project
41      .schema_dir
42      .as_deref()
43      .filter(|value| !value.is_empty())
44    {
45      return Ok(dir.join(value));
46    }
47  }
48  let toml_path = dir.join("schema.toml");
49  let midas_path = dir.join("schema.midas");
50  match (toml_path.is_file(), midas_path.is_file()) {
51    (true, true) => bail!(
52      "both `schema.toml` and `schema.midas` exist in `{}`; select one with `--schema <path>` or `[project].schema` in `midas.toml`",
53      dir.to_string_lossy()
54    ),
55    (false, true) => Ok(midas_path),
56    _ => Ok(PathBuf::from("schema.toml")),
57  }
58}
59
60#[cfg(test)]
61mod tests {
62  use super::*;
63
64  fn write(dir: &Path, name: &str, text: &str) {
65    std::fs::write(dir.join(name), text).expect("write");
66  }
67
68  #[test]
69  fn defaults_to_schema_toml() {
70    let dir = tempfile::tempdir().expect("temp");
71    assert_eq!(
72      resolve_in(dir.path(), None).expect("resolve"),
73      Path::new("schema.toml")
74    );
75  }
76
77  #[test]
78  fn selects_the_only_present_default() {
79    let dir = tempfile::tempdir().expect("temp");
80    write(dir.path(), "schema.midas", "version 1\n");
81    assert_eq!(
82      resolve_in(dir.path(), None).expect("resolve"),
83      dir.path().join("schema.midas")
84    );
85  }
86
87  #[test]
88  fn conflicting_defaults_fail_without_a_selection() {
89    let dir = tempfile::tempdir().expect("temp");
90    write(dir.path(), "schema.toml", "version = 1\n");
91    write(dir.path(), "schema.midas", "version 1\n");
92    let error = resolve_in(dir.path(), None).expect_err("conflict");
93    let message = error.to_string();
94    assert!(
95      message.contains("both `schema.toml` and `schema.midas`"),
96      "{message}"
97    );
98    assert!(message.contains("--schema"), "{message}");
99  }
100
101  #[test]
102  fn explicit_flag_wins() {
103    let dir = tempfile::tempdir().expect("temp");
104    write(dir.path(), "schema.toml", "version = 1\n");
105    write(dir.path(), "custom.midas", "version 1\n");
106    assert_eq!(
107      resolve_in(dir.path(), Some("custom.midas")).expect("resolve"),
108      Path::new("custom.midas")
109    );
110  }
111
112  #[test]
113  fn project_schema_wins_over_defaults() {
114    let dir = tempfile::tempdir().expect("temp");
115    write(
116      dir.path(),
117      "midas.toml",
118      "[project]\nschema = \"db/schema.midas\"\n",
119    );
120    write(dir.path(), "schema.toml", "version = 1\n");
121    assert_eq!(
122      resolve_in(dir.path(), None).expect("resolve"),
123      dir.path().join("db/schema.midas")
124    );
125  }
126
127  #[test]
128  fn project_schema_dir_is_used_when_no_file_is_selected() {
129    let dir = tempfile::tempdir().expect("temp");
130    write(dir.path(), "midas.toml", "[project]\nschema_dir = \"db\"\n");
131    assert_eq!(
132      resolve_in(dir.path(), None).expect("resolve"),
133      dir.path().join("db")
134    );
135  }
136}