build_script/
prefix.rs

1//! This contains the [`Prefix`](Prefix) enum.
2use std::fmt;
3
4/// The prefix. Usually [`Cargo`](Self::Cargo).
5#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
6pub enum Prefix {
7    /// The cargo prefix. 99% of the time this is used.
8    Cargo,
9
10    /// Other, custom prefixes.
11    Custom(String),
12}
13
14impl Default for Prefix {
15    /// The default prefix is [`Cargo`](Self::Cargo).
16    fn default() -> Self {
17        Self::Cargo
18    }
19}
20
21impl fmt::Display for Prefix {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        let prefix = match self {
24            Self::Cargo => "cargo",
25            Self::Custom(prefix) => prefix,
26        };
27
28        prefix.fmt(f)
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use crate::Prefix;
35
36    #[test]
37    fn test_default() {
38        let prefix = Prefix::default();
39        assert!(matches!(prefix, Prefix::Cargo))
40    }
41
42    #[test]
43    fn test_display_cargo() {
44        let prefix = Prefix::Cargo;
45        let string = format!("{}", prefix);
46        assert_eq!(string, "cargo")
47    }
48
49    #[test]
50    fn test_display_custom() {
51        let prefix = Prefix::Custom("custom".into());
52        let string = format!("{}", prefix);
53        assert_eq!(string, "custom")
54    }
55}