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
110
111
use cargo_metadata::{Package, Target};
use eyre::eyre;

use crate::Result;

/// Configures and constructs [`DistTargetConfig`].
///
/// This struct is build from
/// [`DistPackageConfigBuilder`](super::DistPackageConfigBuilder).
#[derive(Debug)]
pub struct DistTargetConfigBuilder<'a> {
    name: String,
    metadata: &'a Target,
    command: Option<clap::Command>,
    #[cfg(feature = "subcommand-dist-build-bin")]
    cargo_build_options: Vec<String>,
}

impl<'a> DistTargetConfigBuilder<'a> {
    pub(crate) fn from_metadata(target: &'a Target) -> Self {
        Self {
            name: target.name.clone(),
            metadata: target,
            command: None,
            #[cfg(feature = "subcommand-dist-build-bin")]
            cargo_build_options: vec![],
        }
    }

    pub(crate) fn target_by_name(package: &'a Package, name: &str, kind: &str) -> Result<Self> {
        let target = package
            .targets
            .iter()
            .find(|t| t.name == name && t.kind.iter().any(|k| k == kind))
            .ok_or_else(|| eyre!("command not found: {name}, {kind}"))?;
        Ok(Self {
            name: name.to_string(),
            metadata: target,
            command: None,
            #[cfg(feature = "subcommand-dist-build-bin")]
            cargo_build_options: vec![],
        })
    }

    /// Set the command line interface definition for the target.
    pub fn command(mut self, command: clap::Command) -> Self {
        self.command = Some(command);
        self
    }

    /// Adds cargo build options to be used when building the target.
    #[cfg(feature = "subcommand-dist-build-bin")]
    #[cfg_attr(docsrs, doc(cfg(feature = "subcommand-dist-build-bin")))]
    pub fn cargo_build_options(
        mut self,
        options: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.cargo_build_options
            .extend(options.into_iter().map(Into::into));
        self
    }

    /// Builds a [`DistTargetConfig`] from the current configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the [`DistTargetConfig`] cannot be built.
    pub fn build(self) -> Result<DistTargetConfig<'a>> {
        Ok(DistTargetConfig {
            name: self.name,
            metadata: self.metadata,
            command: self.command,
            #[cfg(feature = "subcommand-dist-build-bin")]
            cargo_build_options: self.cargo_build_options,
        })
    }
}

/// Configuration for the distribution of the target.
#[derive(Debug)]
pub struct DistTargetConfig<'a> {
    name: String,
    metadata: &'a Target,
    command: Option<clap::Command>,
    #[cfg(feature = "subcommand-dist-build-bin")]
    cargo_build_options: Vec<String>,
}

impl<'a> DistTargetConfig<'a> {
    /// Return the name of the target.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the metadata of the target.
    pub fn metadata(&self) -> &Target {
        self.metadata
    }

    /// Returns the command line interface definition for the target.
    pub fn command(&self) -> Option<&clap::Command> {
        self.command.as_ref()
    }

    /// Returns the cargo build options to be used when building the target.
    #[cfg(feature = "subcommand-dist-build-bin")]
    #[cfg_attr(docsrs, doc(cfg(feature = "subcommand-dist-build-bin")))]
    pub fn cargo_build_options(&self) -> &[String] {
        &self.cargo_build_options
    }
}