cargo_packager/config/
builder.rs

1use std::path::PathBuf;
2
3use crate::{Config, PackageFormat};
4
5use super::{
6    AppImageConfig, Binary, DebianConfig, FileAssociation, HookCommand, LogLevel, MacOsConfig,
7    NsisConfig, PacmanConfig, Resource, WindowsConfig, WixConfig,
8};
9
10/// A builder type for [`Config`].
11#[derive(Default)]
12pub struct ConfigBuilder(Config);
13
14impl ConfigBuilder {
15    /// Creates a new config builder.
16    pub fn new() -> Self {
17        Self::default()
18    }
19
20    /// Returns a reference to the config used by this builder.
21    pub fn config(&self) -> &Config {
22        &self.0
23    }
24
25    /// Sets [`Config::product_name`].
26    pub fn product_name<S: Into<String>>(mut self, product_name: S) -> Self {
27        self.0.product_name = product_name.into();
28        self
29    }
30
31    /// Sets [`Config::version`].
32    pub fn version<S: Into<String>>(mut self, version: S) -> Self {
33        self.0.version = version.into();
34        self
35    }
36
37    /// Sets [`Config::binaries`].
38    pub fn binaries<I: IntoIterator<Item = Binary>>(mut self, binaries: I) -> Self {
39        self.0.binaries = binaries.into_iter().collect();
40        self
41    }
42
43    /// Sets [`Config::identifier`].
44    pub fn identifier<S: Into<String>>(mut self, identifier: S) -> Self {
45        self.0.identifier.replace(identifier.into());
46        self
47    }
48
49    /// Sets [`Config::before_packaging_command`].
50    pub fn before_packaging_command(mut self, command: HookCommand) -> Self {
51        self.0.before_packaging_command.replace(command);
52        self
53    }
54
55    /// Sets [`Config::before_each_package_command`].
56    pub fn before_each_package_command(mut self, command: HookCommand) -> Self {
57        self.0.before_each_package_command.replace(command);
58        self
59    }
60
61    /// Sets [`Config::log_level`].
62    pub fn log_level(mut self, level: LogLevel) -> Self {
63        self.0.log_level.replace(level);
64        self
65    }
66
67    /// Sets [`Config::formats`].
68    pub fn formats<I: IntoIterator<Item = PackageFormat>>(mut self, formats: I) -> Self {
69        self.0.formats = Some(formats.into_iter().collect());
70        self
71    }
72
73    /// Sets [`Config::out_dir`].
74    pub fn out_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
75        self.0.out_dir = path.into();
76        self
77    }
78
79    /// Sets [`Config::target_triple`].
80    pub fn target_triple<S: Into<String>>(mut self, target_triple: S) -> Self {
81        self.0.target_triple.replace(target_triple.into());
82        self
83    }
84
85    /// Sets [`Config::description`].
86    pub fn description<S: Into<String>>(mut self, description: S) -> Self {
87        self.0.description.replace(description.into());
88        self
89    }
90
91    /// Sets [`Config::long_description`].
92    pub fn long_description<S: Into<String>>(mut self, long_description: S) -> Self {
93        self.0.long_description.replace(long_description.into());
94        self
95    }
96
97    /// Sets [`Config::homepage`].
98    pub fn homepage<S: Into<String>>(mut self, homepage: S) -> Self {
99        self.0.homepage.replace(homepage.into());
100        self
101    }
102
103    /// Sets [`Config::authors`].
104    pub fn authors<I, S>(mut self, authors: I) -> Self
105    where
106        I: IntoIterator<Item = S>,
107        S: Into<String>,
108    {
109        self.0
110            .authors
111            .replace(authors.into_iter().map(Into::into).collect());
112        self
113    }
114
115    /// Sets [`Config::publisher`].
116    pub fn publisher<S: Into<String>>(mut self, publisher: S) -> Self {
117        self.0.publisher.replace(publisher.into());
118        self
119    }
120
121    /// Sets [`Config::license_file`].
122    pub fn license_file<P: Into<PathBuf>>(mut self, license_file: P) -> Self {
123        self.0.license_file.replace(license_file.into());
124        self
125    }
126
127    /// Sets [`Config::copyright`].
128    pub fn copyright<S: Into<String>>(mut self, copyright: S) -> Self {
129        self.0.copyright.replace(copyright.into());
130        self
131    }
132
133    /// Sets [`Config::icons`].
134    pub fn icons<I, S>(mut self, icons: I) -> Self
135    where
136        I: IntoIterator<Item = S>,
137        S: Into<String>,
138    {
139        self.0
140            .icons
141            .replace(icons.into_iter().map(Into::into).collect());
142        self
143    }
144
145    /// Sets [`Config::file_associations`].
146    pub fn file_associations<I: IntoIterator<Item = FileAssociation>>(
147        mut self,
148        file_associations: I,
149    ) -> Self {
150        self.0
151            .file_associations
152            .replace(file_associations.into_iter().collect());
153        self
154    }
155
156    /// Sets [`Config::resources`].
157    pub fn resources<I: IntoIterator<Item = Resource>>(mut self, resources: I) -> Self {
158        self.0.resources.replace(resources.into_iter().collect());
159        self
160    }
161
162    /// Sets [`Config::external_binaries`].
163    pub fn external_binaries<I, P>(mut self, external_binaries: I) -> Self
164    where
165        I: IntoIterator<Item = P>,
166        P: Into<PathBuf>,
167    {
168        self.0
169            .external_binaries
170            .replace(external_binaries.into_iter().map(Into::into).collect());
171        self
172    }
173
174    /// Set the [Windows](Config::windows) specific configuration.
175    pub fn windows(mut self, windows: WindowsConfig) -> Self {
176        self.0.windows.replace(windows);
177        self
178    }
179
180    /// Set the [MacOS](Config::macos) specific configuration.
181    pub fn macos(mut self, macos: MacOsConfig) -> Self {
182        self.0.macos.replace(macos);
183        self
184    }
185
186    /// Set the [WiX](Config::wix) specific configuration.
187    pub fn wix(mut self, wix: WixConfig) -> Self {
188        self.0.wix.replace(wix);
189        self
190    }
191
192    /// Set the [Nsis](Config::nsis) specific configuration.
193    pub fn nsis(mut self, nsis: NsisConfig) -> Self {
194        self.0.nsis.replace(nsis);
195        self
196    }
197
198    /// Set the [Debian](Config::deb) specific configuration.
199    pub fn deb(mut self, deb: DebianConfig) -> Self {
200        self.0.deb.replace(deb);
201        self
202    }
203
204    /// Set the [Appimage](Config::appimage) specific configuration.
205    pub fn appimage(mut self, appimage: AppImageConfig) -> Self {
206        self.0.appimage.replace(appimage);
207        self
208    }
209
210    /// Set the [Pacman](Config::pacman) specific configuration.
211    pub fn pacman(mut self, pacman: PacmanConfig) -> Self {
212        self.0.pacman.replace(pacman);
213        self
214    }
215}