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