rws-http 18.30.0

The HTTP layer of rust-web-server (internal: use the rust-web-server crate).
Documentation
//! `RWS_ENV`: which kind of environment the process runs in — `dev`,
//! `test` or `prod` (the default). Since 18.28.0.
//!
//! Read from the process environment only (no config-file key, no flag):
//! it says how the rest of the configuration should be treated, so it is
//! set where the process is started. Today it decides whether
//! `dev_mcp()` mounts its tools without a token; the profiles of
//! roadmap item #95 build on the same setting.

/// The environment kind named by `RWS_ENV`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RwsEnv {
    /// `RWS_ENV=dev` (or `development`): a developer's machine.
    Dev,
    /// `RWS_ENV=test`: an automated test run.
    Test,
    /// `RWS_ENV=prod` (or `production`), unset, or anything else.
    #[default]
    Prod,
}

impl RwsEnv {
    /// The environment variable read by [`RwsEnv::current`].
    pub const VAR: &'static str = "RWS_ENV";

    /// `RWS_ENV` as it is now; unset or unrecognised is [`RwsEnv::Prod`],
    /// so nothing is opened up by a typo.
    pub fn current() -> RwsEnv {
        std::env::var(Self::VAR).ok().and_then(|v| Self::parse(&v)).unwrap_or_default()
    }

    /// `dev`/`development`, `test`, `prod`/`production` (case-insensitive,
    /// surrounding spaces ignored); `None` for anything else.
    pub fn parse(value: &str) -> Option<RwsEnv> {
        match value.trim().to_ascii_lowercase().as_str() {
            "dev" | "development" => Some(RwsEnv::Dev),
            "test" => Some(RwsEnv::Test),
            "prod" | "production" => Some(RwsEnv::Prod),
            _ => None,
        }
    }

    /// `"dev"`, `"test"` or `"prod"`.
    pub fn as_str(self) -> &'static str {
        match self {
            RwsEnv::Dev => "dev",
            RwsEnv::Test => "test",
            RwsEnv::Prod => "prod",
        }
    }

    /// `true` for [`RwsEnv::Dev`].
    pub fn is_dev(self) -> bool {
        self == RwsEnv::Dev
    }
}

impl std::fmt::Display for RwsEnv {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

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

    #[test]
    fn parses_the_three_names_and_their_long_forms() {
        assert_eq!(RwsEnv::parse("dev"), Some(RwsEnv::Dev));
        assert_eq!(RwsEnv::parse(" Development "), Some(RwsEnv::Dev));
        assert_eq!(RwsEnv::parse("TEST"), Some(RwsEnv::Test));
        assert_eq!(RwsEnv::parse("prod"), Some(RwsEnv::Prod));
        assert_eq!(RwsEnv::parse("production"), Some(RwsEnv::Prod));
    }

    #[test]
    fn anything_else_is_not_an_environment() {
        assert_eq!(RwsEnv::parse(""), None);
        assert_eq!(RwsEnv::parse("staging"), None);
        assert_eq!(RwsEnv::parse("devel"), None);
    }

    #[test]
    fn default_is_prod_and_names_round_trip() {
        assert_eq!(RwsEnv::default(), RwsEnv::Prod);
        for env in [RwsEnv::Dev, RwsEnv::Test, RwsEnv::Prod] {
            assert_eq!(RwsEnv::parse(env.as_str()), Some(env));
            assert_eq!(env.to_string(), env.as_str());
        }
        assert!(RwsEnv::Dev.is_dev());
        assert!(!RwsEnv::Test.is_dev());
    }
}