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
/// Cargo build profile
/// https://doc.rust-lang.org/cargo/reference/profiles.html
#[derive(Debug, PartialEq)]
pub enum CargoProfile {
    /// cargo build --release
    Release,

    /// cargo build
    Dev,

    /// cargo test
    Test,

    // Bench,
    // Doc,
}

/// Returns whether debug information is included in the build
pub fn debug_mode() -> bool {
    #[cfg(debug_assertions)]
    return true;

    #[cfg(not(debug_assertions))]
    return false;
}

/// Returns whether the build profile is test
pub fn is_profile_test() -> bool {
    get_profile() == CargoProfile::Test
}

/// Returns a `BuildProfile` value according to the cargo command
pub fn get_profile() -> CargoProfile {
    if cfg!(test) {
        return CargoProfile::Test;
    }
    if debug_mode() {
        return CargoProfile::Dev;
    }

    CargoProfile::Release
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn get_profile_test() {
        assert_eq!(get_profile(), CargoProfile::Test);
    }

    #[test]
    fn is_cargo_profile_test() {
        assert!(is_profile_test());
    }

    #[test]
    fn debug_mode_test() {
        assert!(debug_mode());
    }
}