cargo_deb/parse/
cargo.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use crate::error::CDResult;
use std::path::{Path, PathBuf};
use std::{env, fs};

pub struct CargoConfig {
    path: PathBuf,
    config: toml::Value,
}

impl CargoConfig {
    pub fn new<P: AsRef<Path>>(project_path: P) -> CDResult<Option<Self>> {
        Self::new_(project_path.as_ref())
    }

    fn new_(project_path: &Path) -> CDResult<Option<Self>> {
        let mut project_path = project_path;
        loop {
            if let Some(conf) = Self::try_parse(&project_path.join(".cargo"))? {
                return Ok(Some(conf));
            }
            if let Some(parent) = project_path.parent() {
                project_path = parent;
            } else {
                break;
            }
        }
        if let Some(home) = env::var_os("CARGO_HOME").map(PathBuf::from) {
            if let Some(conf) = Self::try_parse(&home)? {
                return Ok(Some(conf));
            }
        }
        #[allow(deprecated)]
        if let Some(home) = env::home_dir() {
            if let Some(conf) = Self::try_parse(&home.join(".cargo"))? {
                return Ok(Some(conf));
            }
        }
        if let Some(conf) = Self::try_parse(Path::new("/etc/.cargo"))? {
            return Ok(Some(conf));
        }
        Ok(None)
    }

    fn try_parse(dir_path: &Path) -> CDResult<Option<Self>> {
        let mut path = dir_path.join("config.toml");
        if !path.exists() {
            path = dir_path.join("config");
            if !path.exists() {
                return Ok(None);
            }
        }
        Ok(Some(Self::from_str(&fs::read_to_string(&path)?, path)?))
    }

    fn from_str(input: &str, path: PathBuf) -> CDResult<Self> {
        let config = toml::from_str(input)?;
        Ok(Self { path, config })
    }

    fn target_conf(&self, target_triple: &str) -> Option<&toml::value::Table> {
        let target = self.config.get("target")?.as_table()?;
        target.get(target_triple)?.as_table()
    }

    pub fn explicit_target_specific_command(&self, command_name: &str, target_triple: &str) -> Option<&Path> {
        let top = self.target_conf(target_triple)?.get(command_name)?;
        top.as_str().or_else(|| top.get("path")?.as_str()).map(Path::new)
    }

    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn explicit_linker_command(&self, target_triple: &str) -> Option<&Path> {
        self.target_conf(target_triple)?.get("linker")?.as_str().map(Path::new)
    }
}

#[test]
fn parse_strip() {
    let c = CargoConfig::from_str(r#"
[target.i686-unknown-dragonfly]
linker = "magic-ld"
strip = "magic-strip"

[target.'foo']
strip = { path = "strip2" }
"#, ".".into()).unwrap();

    assert_eq!("magic-strip", c.explicit_target_specific_command("strip", "i686-unknown-dragonfly").unwrap().as_os_str());
    assert_eq!("strip2", c.explicit_target_specific_command("strip", "foo").unwrap().as_os_str());
    assert_eq!(None, c.explicit_target_specific_command("strip", "bar"));
}

#[test]
fn parse_objcopy() {
    let c = CargoConfig::from_str(r#"
[target.i686-unknown-dragonfly]
linker = "magic-ld"
objcopy = "magic-objcopy"

[target.'foo']
objcopy = { path = "objcopy2" }
"#, ".".into()).unwrap();

    assert_eq!("magic-objcopy", c.explicit_target_specific_command("objcopy", "i686-unknown-dragonfly").unwrap().as_os_str());
    assert_eq!("objcopy2", c.explicit_target_specific_command("objcopy", "foo").unwrap().as_os_str());
    assert_eq!(None, c.explicit_target_specific_command("objcopy", "bar"));
}