cargo_subcommand/
artifact.rs

1use std::path::{Path, PathBuf};
2
3use crate::manifest::CrateType;
4
5#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
6pub enum ArtifactType {
7    Lib,
8    Bin,
9    Example,
10    // Bench,
11    // Test,
12}
13
14#[derive(Clone, Debug, Eq, Hash, PartialEq)]
15pub struct Artifact {
16    pub name: String,
17    pub path: PathBuf,
18    pub r#type: ArtifactType,
19}
20
21impl Artifact {
22    pub fn build_dir(&self) -> &'static Path {
23        Path::new(match self.r#type {
24            ArtifactType::Lib | ArtifactType::Bin => "",
25            ArtifactType::Example => "examples",
26        })
27    }
28
29    // TODO: CrateType should be read from the manifest' crate-type array,
30    // and validated that the requested format is in that array
31    pub fn file_name(&self, ty: CrateType, target: &str) -> String {
32        match (self.r#type, ty) {
33            (ArtifactType::Bin | ArtifactType::Example, CrateType::Bin) => {
34                if target.contains("windows") {
35                    format!("{}.exe", self.name)
36                } else if target.contains("wasm") {
37                    format!("{}.wasm", self.name)
38                } else {
39                    self.name.to_string()
40                }
41            }
42            (ArtifactType::Lib | ArtifactType::Example, CrateType::Lib) => {
43                format!("lib{}.rlib", self.name.replace('-', "_"))
44            }
45            (ArtifactType::Lib | ArtifactType::Example, CrateType::Staticlib) => {
46                format!("lib{}.a", self.name.replace('-', "_"))
47            }
48            (ArtifactType::Lib | ArtifactType::Example, CrateType::Cdylib) => {
49                format!("lib{}.so", self.name.replace('-', "_"))
50            }
51            (a, c) => panic!("{a:?} is not compatible with {c:?}"),
52        }
53    }
54}