build_target/
profile.rs

1use std::fmt;
2
3use crate::utils::{build_env, define_target_enum};
4
5define_target_enum! {
6    /// Profile of the current build.
7    #[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
8    #[non_exhaustive]
9    pub enum Profile {
10        /// The dev profile is used for normal development and debugging.
11        /// It is the default for build commands like `cargo build`.
12        Dev => "dev",
13
14        /// The release profile is intended for optimized artifacts used for releases and in production.
15        /// This profile is used when the `--release` flag is used, and is the default for `cargo install`.
16        Release => "release",
17
18        /// The test profile is used for building tests, or when benchmarks are built in debug mode with `cargo build`.
19        Test => "test",
20
21        /// The bench profile is used for building benchmarks, or when tests are built with the `--release` flag.
22        Bench => "bench",
23    }
24
25    as_str_doc = "String representing this target profile.",
26    from_str_doc = "Tries to parse the given string as an [`Profile`] falling back to [`Profile::Other`] for unknown values.",
27}
28
29impl Profile {
30    /// Gets the current [`Profile`].
31    #[must_use]
32    pub fn current() -> Self {
33        Self::from_str(build_env("PROFILE"))
34    }
35}
36
37impl fmt::Display for Profile {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        f.write_str(self.as_str())
40    }
41}