cargo_deb/parse/
cargo.rs

1use crate::error::CDResult;
2use crate::CargoDebError;
3use std::path::{Path, PathBuf};
4use std::{env, fs};
5
6pub struct CargoConfig {
7    path: PathBuf,
8    config: toml::Value,
9}
10
11impl CargoConfig {
12    pub fn new<P: AsRef<Path>>(project_path: P) -> CDResult<Option<Self>> {
13        Self::new_(project_path.as_ref())
14    }
15
16    fn new_(project_path: &Path) -> CDResult<Option<Self>> {
17        let mut project_path = project_path;
18        loop {
19            if let Some(conf) = Self::try_parse(&project_path.join(".cargo"))? {
20                return Ok(Some(conf));
21            }
22            if let Some(parent) = project_path.parent() {
23                project_path = parent;
24            } else {
25                break;
26            }
27        }
28        if let Some(home) = env::var_os("CARGO_HOME").map(PathBuf::from) {
29            if let Some(conf) = Self::try_parse(&home)? {
30                return Ok(Some(conf));
31            }
32        }
33        #[allow(deprecated)]
34        if let Some(home) = env::home_dir() {
35            if let Some(conf) = Self::try_parse(&home.join(".cargo"))? {
36                return Ok(Some(conf));
37            }
38        }
39        if let Some(conf) = Self::try_parse(Path::new("/etc/.cargo"))? {
40            return Ok(Some(conf));
41        }
42        Ok(None)
43    }
44
45    fn try_parse(dir_path: &Path) -> CDResult<Option<Self>> {
46        let mut path = dir_path.join("config.toml");
47        if !path.exists() {
48            path.set_file_name("config");
49            if !path.exists() {
50                return Ok(None);
51            }
52        }
53        let config = fs::read_to_string(&path)
54            .map_err(|e| CargoDebError::IoFile("unable to read config file", e, path.clone()))?;
55        Ok(Some(Self::from_str(&config, path)?))
56    }
57
58    fn from_str(input: &str, path: PathBuf) -> CDResult<Self> {
59        let config = toml::from_str(input)?;
60        Ok(Self { path, config })
61    }
62
63    fn target_conf(&self, target_triple: &str) -> Option<&toml::value::Table> {
64        let target = self.config.get("target")?.as_table()?;
65        target.get(target_triple)?.as_table()
66    }
67
68    pub fn explicit_target_specific_command(&self, command_name: &str, target_triple: &str) -> Option<&Path> {
69        let top = self.target_conf(target_triple)?.get(command_name)?;
70        top.as_str().or_else(|| top.get("path")?.as_str()).map(Path::new)
71    }
72
73    pub fn path(&self) -> &Path {
74        &self.path
75    }
76
77    pub fn explicit_linker_command(&self, target_triple: &str) -> Option<&Path> {
78        self.target_conf(target_triple)?.get("linker")?.as_str().map(Path::new)
79    }
80}
81
82#[test]
83fn parse_strip() {
84    let c = CargoConfig::from_str(r#"
85[target.i686-unknown-dragonfly]
86linker = "magic-ld"
87strip = "magic-strip"
88
89[target.'foo']
90strip = { path = "strip2" }
91"#, ".".into()).unwrap();
92
93    assert_eq!("magic-strip", c.explicit_target_specific_command("strip", "i686-unknown-dragonfly").unwrap().as_os_str());
94    assert_eq!("strip2", c.explicit_target_specific_command("strip", "foo").unwrap().as_os_str());
95    assert_eq!(None, c.explicit_target_specific_command("strip", "bar"));
96}
97
98#[test]
99fn parse_objcopy() {
100    let c = CargoConfig::from_str(r#"
101[target.i686-unknown-dragonfly]
102linker = "magic-ld"
103objcopy = "magic-objcopy"
104
105[target.'foo']
106objcopy = { path = "objcopy2" }
107"#, ".".into()).unwrap();
108
109    assert_eq!("magic-objcopy", c.explicit_target_specific_command("objcopy", "i686-unknown-dragonfly").unwrap().as_os_str());
110    assert_eq!("objcopy2", c.explicit_target_specific_command("objcopy", "foo").unwrap().as_os_str());
111    assert_eq!(None, c.explicit_target_specific_command("objcopy", "bar"));
112}