Skip to main content

cargo_toml_builder/types/
badges.rs

1use std::fmt;
2use std::convert::TryFrom;
3
4use crate::error::Error;
5
6/// Represents an `appveyor = { ... }` badge
7#[derive(Debug, Clone, PartialEq)]
8pub struct Appveyor {
9    pub(crate) repository: String,
10    pub(crate) branch: Option<String>,
11    pub(crate) service: Option<AppveyorService>,
12}
13
14/// Represents the values that `service` can take in an `appveyor = { ... }` badge
15#[derive(Debug, Clone, Copy, PartialEq)]
16pub enum AppveyorService {
17    /// `appveyor = { service = "github" }`
18    Github,
19    /// `appveyor = { service = "bitbucket" }`
20    Bitbucket,
21    /// `appveyor = { service = "gitlab" }`
22    Gitlab,
23}
24
25impl fmt::Display for AppveyorService {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        use crate::types::badges::AppveyorService::*;
28        write!(f, "{}", match *self {
29            Github => "github",
30            Bitbucket => "bitbucket",
31            Gitlab => "gitlab",
32        })
33    }
34}
35
36impl From<String> for Appveyor {
37    fn from(s: String) -> Appveyor {
38        Appveyor::new(&s)
39    }
40}
41
42impl<'a> From<&'a str> for Appveyor {
43    fn from(s: &'a str) -> Appveyor {
44        Appveyor::new(s)
45    }
46}
47
48impl Appveyor {
49    /// Creates a new, empty `appveyor` section
50    pub fn new(repository: &str) -> Appveyor {
51        Appveyor {
52            repository: repository.to_string(),
53            branch: None,
54            service: None,
55        }
56    }
57
58    /// Sets the branch this badge should be querying for
59    pub fn branch(&mut self, branch: &str) -> &mut Self {
60        self.branch = Some(branch.to_string());
61        self
62    }
63
64    /// Sets the appveyor service this badge should be querying
65    pub fn service(&mut self, service: AppveyorService) -> &mut Self {
66        self.service = Some(service);
67        self
68    }
69
70    /// Takes ownership of this builder
71    pub fn build(&self) -> Self {
72        self.clone()
73    }
74}
75
76/// Represents a `circe-ci = { ... }` badge
77#[derive(Debug, Clone, PartialEq)]
78pub struct CircleCi {
79    pub(crate) repository: String,
80    pub(crate) branch: Option<String>,
81}
82
83impl From<String> for CircleCi {
84    fn from(s: String) -> CircleCi {
85        CircleCi::new(&s)
86    }
87}
88
89impl<'a> From<&'a str> for CircleCi {
90    fn from(s: &'a str) -> CircleCi {
91        CircleCi::new(s)
92    }
93}
94
95impl CircleCi {
96    /// Creates a new, empty `circle-ci` section
97    pub fn new(repository: &str) -> CircleCi {
98        CircleCi {
99            repository: repository.to_string(),
100            branch: None,
101        }
102    }
103
104    /// Sets the branch this badge should be querying for
105    pub fn branch(&mut self, branch: &str) -> &mut Self {
106        self.branch = Some(branch.to_string());
107        self
108    }
109
110    /// Takes ownership of this builder
111    pub fn build(&self) -> Self {
112        self.clone()
113    }
114}
115
116/// Represents a `gitlab = { ... }` badge
117#[derive(Debug, Clone, PartialEq)]
118pub struct Gitlab {
119    pub(crate) repository: String,
120    pub(crate) branch: Option<String>,
121}
122
123impl From<String> for Gitlab {
124    fn from(s: String) -> Gitlab {
125        Gitlab::new(&s)
126    }
127}
128
129impl<'a> From<&'a str> for Gitlab {
130    fn from(s: &'a str) -> Gitlab {
131        Gitlab::new(s)
132    }
133}
134
135impl Gitlab {
136    /// Creates a new empty `gitlab` section
137    pub fn new(repository: &str) -> Gitlab {
138        Gitlab {
139            repository: repository.to_string(),
140            branch: None,
141        }
142    }
143
144    /// Sets the branch this badge should retrieve information for
145    pub fn branch(&mut self, branch: &str) -> &mut Gitlab {
146        self.branch = Some(branch.to_string());
147        self
148    }
149
150    /// Takes ownership of this builder
151    pub fn build(&self) -> Gitlab {
152        self.clone()
153    }
154}
155
156/// Represents a `travis-ci = { ... }` badge
157#[derive(Debug, Clone, PartialEq)]
158pub struct TravisCi {
159    pub(crate) repository: String,
160    pub(crate) branch: Option<String>,
161}
162
163impl From<String> for TravisCi {
164    fn from(s: String) -> TravisCi {
165        TravisCi::new(&s)
166    }
167}
168
169impl<'a> From<&'a str> for TravisCi {
170    fn from(s: &'a str) -> TravisCi {
171        TravisCi::new(s)
172    }
173}
174
175impl TravisCi {
176    /// Creates a new `travis-ci` section
177    pub fn new(repository: &str) -> TravisCi {
178        TravisCi {
179            repository: repository.to_string(),
180            branch: None,
181        }
182    }
183
184    /// Sets the branch that travis should build
185    pub fn branch(&mut self, branch: &str) -> &mut TravisCi {
186        self.branch = Some(branch.to_string());
187        self
188    }
189
190    /// Takes ownership of this builder
191    pub fn build(&self) -> TravisCi {
192        self.clone()
193    }
194}
195
196/// Represents a `codecov = { ... }` badge
197#[derive(Debug, Clone, PartialEq)]
198pub struct Codecov {
199    pub(crate) repository: String,
200    pub(crate) branch: Option<String>,
201    pub(crate) service: Option<CodecovService>,
202}
203
204impl From<String> for Codecov {
205    fn from(s: String) -> Codecov {
206        Codecov::new(&s)
207    }
208}
209
210impl<'a> From<&'a str> for Codecov {
211    fn from(s: &'a str) -> Codecov {
212        Codecov::new(s)
213    }
214}
215
216/// Represents the values `service` can take in a `codecov = { ... }` badge
217#[derive(Debug, Clone, Copy, PartialEq)]
218pub enum CodecovService {
219    /// `codecov = { service = "github" }`
220    Github,
221    /// `codecov = { service = "bitbucket" }`
222    Bitbucket,
223    /// `codecov = { service = "gitlab" }`
224    Gitlab,
225}
226
227impl fmt::Display for CodecovService {
228    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
229        use crate::types::badges::CodecovService::*;
230        write!(f, "{}", match *self {
231            Github => "github",
232            Bitbucket => "bitbucket",
233            Gitlab => "gitlab",
234        })
235    }
236}
237
238impl Codecov {
239    /// Creates a new empty `codecov` section
240    pub fn new(repository: &str) -> Codecov {
241        Codecov {
242            repository: repository.to_string(),
243            branch: None,
244            service: None,
245        }
246    }
247
248    /// Sets the branch that codecov should inspect
249    pub fn branch(&mut self, branch: &str) -> &mut Codecov {
250        self.branch = Some(branch.to_string());
251        self
252    }
253
254    /// Sets the service that codecov will be interacting with
255    pub fn service(&mut self, service: CodecovService) -> &mut Codecov {
256        self.service = Some(service);
257        self
258    }
259
260    /// Takes ownership of this builder
261    pub fn build(&self) -> Codecov {
262        self.clone()
263    }
264}
265
266/// Represents a `coveralls = { ... }` badge
267#[derive(Debug, Clone, PartialEq)]
268pub struct Coveralls {
269    pub(crate) repository: String,
270    pub(crate) branch: Option<String>,
271    pub(crate) service: Option<CoverallsService>,
272}
273
274/// Represents the values `service` can take in a `coveralls = { ... }` badge
275#[derive(Debug, Clone, Copy, PartialEq)]
276pub enum CoverallsService {
277    /// `coveralls = { service = "github" }`
278    Github,
279    /// `coveralls = { service = "bitbucket" }`
280    Bitbucket,
281    /// `coveralls = { service = "gitlab" }`
282    Gitlab,
283}
284
285impl fmt::Display for CoverallsService {
286    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
287        use crate::types::badges::CoverallsService::*;
288        write!(f, "{}", match *self {
289            Github => "github",
290            Bitbucket => "bitbucket",
291            Gitlab => "gitlab",
292        })
293    }
294}
295
296impl From<String> for Coveralls {
297    fn from(s: String) -> Coveralls {
298        Coveralls::new(&s)
299    }
300}
301
302impl<'a> From<&'a str> for Coveralls {
303    fn from(s: &'a str) -> Coveralls {
304        Coveralls::new(s)
305    }
306}
307
308impl Coveralls {
309    /// Creates a new `coveralls = {}` section
310    pub fn new(repository: &str) -> Coveralls {
311        Coveralls {
312            repository: repository.to_string(),
313            branch: None,
314            service: None,
315        }
316    }
317
318    /// Sets the branch that coveralls should inspect
319    pub fn branch(&mut self, branch: &str) -> &mut Coveralls {
320        self.branch = Some(branch.to_string());
321        self
322    }
323
324    /// Sets the coveralls service value
325    pub fn service(&mut self, service: CoverallsService) -> &mut Coveralls {
326        self.service = Some(service);
327        self
328    }
329
330    /// Takes ownership of this builder
331    pub fn build(&self) -> Coveralls {
332        self.clone()
333    }
334}
335
336/// Represents a `maintenance = { status = ... }` badge
337#[derive(Debug, Clone, Copy, PartialEq)]
338pub struct Maintenance {
339    pub(crate) status: MaintenanceStatus,
340}
341
342impl Maintenance {
343    fn new(status: MaintenanceStatus) -> Maintenance {
344        Maintenance { status: status }
345    }
346}
347
348impl TryFrom<String> for Maintenance {
349    type Error = Error;
350    fn try_from(s: String) -> Result<Self, Self::Error> {
351        Ok(Maintenance::new(MaintenanceStatus::try_from(s)?))
352    }
353}
354
355impl<'a> TryFrom<&'a str> for Maintenance {
356    type Error = Error;
357
358    fn try_from(s: &'a str) -> Result<Self, Self::Error> {
359        Ok(Maintenance::new(MaintenanceStatus::try_from(s)?))
360    }
361}
362
363impl From<MaintenanceStatus> for Maintenance {
364    fn from(m: MaintenanceStatus) -> Maintenance {
365        Maintenance {
366            status: m,
367        }
368    }
369}
370
371/// Represents the values `status` can take in a `maintenance = { ... }` badge
372#[derive(Debug, Clone, Copy, PartialEq)]
373pub enum MaintenanceStatus {
374    /// `maintenance = { status = "actively-developed" }`
375    Actively,
376    /// `maintenance = { status = "passively-maintained" }`
377    Passively,
378    /// `maintenance = { status = "as-is" }`
379    AsIs,
380    /// `maintenance = { status = "none" }`
381    None,
382    /// `maintenance = { status = "experimental" }`
383    Experimental,
384    /// `maintenance = { status = "looking-for-maintainer" }`
385    LookingForMaintainer,
386    /// `maintenance = { status = "deprecated" }`
387    Deprecated,
388}
389
390impl TryFrom<String> for MaintenanceStatus {
391    type Error = Error;
392    fn try_from(s: String) -> Result<Self, Self::Error> {
393        TryFrom::try_from(s.as_str())
394    }
395}
396impl<'a> TryFrom<&'a str> for MaintenanceStatus {
397    type Error = Error;
398    fn try_from(s: &'a str) -> Result<Self, Self::Error> {
399        Ok(match s {
400            "actively-developed" => MaintenanceStatus::Actively,
401            "passively-maintained" => MaintenanceStatus::Passively,
402            "as-is" => MaintenanceStatus::AsIs,
403            "none" => MaintenanceStatus::None,
404            "experimental" => MaintenanceStatus::Experimental,
405            "looking-for-maintainer" => MaintenanceStatus::LookingForMaintainer,
406            "deprecated" => MaintenanceStatus::Deprecated,
407            _ => return Err("unrecognized maintenance status string".into()),
408        })
409    }
410}
411
412impl fmt::Display for MaintenanceStatus {
413    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
414        write!(f, "{}", match *self {
415            MaintenanceStatus::Actively => "actively-developed",
416            MaintenanceStatus::Passively => "passively-maintained",
417            MaintenanceStatus::AsIs => "as-is",
418            MaintenanceStatus::None => "none",
419            MaintenanceStatus::Experimental => "experimental",
420            MaintenanceStatus::LookingForMaintainer => "looking-for-maintainer",
421            MaintenanceStatus::Deprecated => "deprecated",
422        })
423    }
424}