Skip to main content

foch_cli/cli/handler/
mod.rs

1pub mod cache;
2pub mod check;
3pub mod config;
4pub mod data;
5pub mod graph;
6pub mod input;
7pub mod merge;
8pub mod plugin;
9pub mod simplify;
10pub mod testing;
11
12pub type HandlerResult = Result<i32, Box<dyn std::error::Error>>;
13
14use foch::input::{Config, InputSource};
15use std::path::{Path, PathBuf};
16
17/// Resolve the `<PLAYSET_PATH>` argument shared by `check`, `merge`, `graph`,
18/// and `simplify`. When the user supplies an explicit path
19/// it is used verbatim; otherwise `<paradox_data_path>/dlc_load.json` (the
20/// launcher's currently-active playset) is the implicit default. Returns a
21/// human-readable error string when neither source produces a usable path.
22pub fn resolve_input_source(
23	explicit: Option<&Path>,
24	config: &Config,
25) -> Result<InputSource, String> {
26	if let Some(path) = explicit {
27		return Ok(InputSource::from_path(path.to_path_buf()));
28	}
29	let paradox = config.paradox_data_path.as_ref().ok_or_else(|| {
30		"no <PLAYSET_PATH> given and `paradox_data_path` is not configured; either pass a path \
31		 explicitly or run `foch config set --paradox-data-path <dir>` so foch can default to the \
32		 launcher's `dlc_load.json` in that directory"
33			.to_string()
34	})?;
35	let candidate = paradox.join("dlc_load.json");
36	if !candidate.is_file() {
37		return Err(format!(
38			"no <PLAYSET_PATH> given and `{}` does not exist; pass an explicit playset path or \
39			 launch the Paradox launcher once to write `dlc_load.json`",
40			candidate.display()
41		));
42	}
43	Ok(InputSource::DlcLoad(candidate))
44}
45
46pub fn resolve_playset_path(explicit: Option<&Path>, config: &Config) -> Result<PathBuf, String> {
47	Ok(resolve_input_source(explicit, config)?.path().to_path_buf())
48}