cargo_codesign/config/
resolve.rs1use super::SignConfig;
2use std::path::{Path, PathBuf};
3
4#[derive(Debug, thiserror::Error)]
5pub enum ResolveError {
6 #[error("no sign.toml found (searched ./sign.toml and ./.cargo/sign.toml)")]
7 NotFound,
8 #[error("failed to read {path}: {source}")]
9 ReadError {
10 path: PathBuf,
11 source: std::io::Error,
12 },
13 #[error("failed to parse {path}: {source}")]
14 ParseError {
15 path: PathBuf,
16 source: Box<toml::de::Error>,
17 },
18}
19
20pub fn resolve_config_from_path(
21 path: &Path,
22) -> Result<(SignConfig, PathBuf, Vec<String>), ResolveError> {
23 let content = std::fs::read_to_string(path).map_err(|e| ResolveError::ReadError {
24 path: path.to_path_buf(),
25 source: e,
26 })?;
27 let config: SignConfig = toml::from_str(&content).map_err(|e| ResolveError::ParseError {
28 path: path.to_path_buf(),
29 source: Box::new(e),
30 })?;
31 Ok((config, path.to_path_buf(), Vec::new()))
32}
33
34pub fn resolve_config(
35 working_dir: Option<&Path>,
36) -> Result<(SignConfig, PathBuf, Vec<String>), ResolveError> {
37 let cwd = match working_dir {
38 Some(p) => p.to_path_buf(),
39 None => std::env::current_dir().expect("cannot determine current directory"),
40 };
41
42 let root_path = cwd.join("sign.toml");
43 let cargo_path = cwd.join(".cargo").join("sign.toml");
44
45 let root_exists = root_path.exists();
46 let cargo_exists = cargo_path.exists();
47
48 match (root_exists, cargo_exists) {
49 (true, true) => {
50 let (config, path, _) = resolve_config_from_path(&root_path)?;
51 let warnings = vec![
52 "warning: both ./sign.toml and ./.cargo/sign.toml exist, using ./sign.toml"
53 .to_string(),
54 ];
55 Ok((config, path, warnings))
56 }
57 (true, false) => resolve_config_from_path(&root_path),
58 (false, true) => resolve_config_from_path(&cargo_path),
59 (false, false) => Err(ResolveError::NotFound),
60 }
61}