Skip to main content

cargo_codesign/
manifest.rs

1#[derive(Debug, thiserror::Error)]
2pub enum ManifestError {
3    #[error("failed to parse dist-manifest.json: {0}")]
4    ParseError(#[from] serde_json::Error),
5}
6
7#[derive(Debug)]
8pub struct DistArtifact {
9    pub name: String,
10    pub kind: String,
11    pub target_triples: Vec<String>,
12    pub asset_paths: Vec<String>,
13}
14
15impl DistArtifact {
16    pub fn is_macos(&self) -> bool {
17        self.target_triples
18            .iter()
19            .any(|t| t.contains("apple-darwin"))
20    }
21
22    pub fn is_windows(&self) -> bool {
23        self.target_triples.iter().any(|t| t.contains("windows"))
24    }
25
26    pub fn is_linux(&self) -> bool {
27        self.target_triples.iter().any(|t| t.contains("linux"))
28    }
29}
30
31/// Parse cargo-dist's `dist-manifest.json` and extract all artifacts.
32pub fn parse_dist_manifest(json: &str) -> Result<Vec<DistArtifact>, ManifestError> {
33    let manifest: serde_json::Value = serde_json::from_str(json)?;
34
35    let Some(artifacts) = manifest["artifacts"].as_object() else {
36        return Ok(Vec::new());
37    };
38
39    let mut result = Vec::new();
40
41    for (_key, artifact) in artifacts {
42        let name = artifact["name"].as_str().unwrap_or_default().to_string();
43        let kind = artifact["kind"].as_str().unwrap_or_default().to_string();
44
45        let target_triples = artifact["target_triples"]
46            .as_array()
47            .map(|arr| {
48                arr.iter()
49                    .filter_map(|v| v.as_str().map(String::from))
50                    .collect()
51            })
52            .unwrap_or_default();
53
54        let asset_paths = artifact["assets"]
55            .as_array()
56            .map(|arr| {
57                arr.iter()
58                    .filter_map(|v| v["path"].as_str().map(String::from))
59                    .collect()
60            })
61            .unwrap_or_default();
62
63        result.push(DistArtifact {
64            name,
65            kind,
66            target_triples,
67            asset_paths,
68        });
69    }
70
71    Ok(result)
72}