ConfigProfile

Trait ConfigProfile 

Source
pub trait ConfigProfile: FromStr<Err: Display + Debug + Send + Sync + 'static> + AsRef<str> {
    // Provided method
    fn load() -> Result<Self, ConfigProfileLoadError> { ... }
}
Expand description

Configuration profiles are used by Pavex applications to determine which configuration file to load.

They are usually modeled as an enum with unit variants, one for each profile.

§Deriving an implementation

You can automatically derive an implementation of ConfigProfile using the #[derive(ConfigProfile)] attribute.

use pavex::config::ConfigProfile;

// Equivalent to the manual implementation below!
#[derive(ConfigProfile, Debug, Clone, Copy, PartialEq, Eq)]
pub enum Profile {
    #[px(profile = "dev")]
    Development,
    #[px(profile = "prod")]
    Production,
}

Check out the macro documentation for more details.

§Manual implementation

If you need more flexibility, you can implement ConfigProfile manually:

use pavex::config::ConfigProfile;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Profile {
    Development,
    Production,
}

impl std::str::FromStr for Profile {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "dev" => Ok(Profile::Development),
            "prod" => Ok(Profile::Production),
            _ => anyhow::bail!("Unknown profile: {}", s),
        }
    }
}

impl AsRef<str> for Profile {
    fn as_ref(&self) -> &str {
        match self {
            Profile::Development => "dev",
            Profile::Production => "prod",
        }
    }
}

impl ConfigProfile for Profile {}

The value returned by as_ref() is used as the name of the profile-specific configuration file.

Provided Methods§

Source

fn load() -> Result<Self, ConfigProfileLoadError>

Load and parse the configuration profile out of the PX_PROFILE environment variable.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§