servlet 1.1.0

A small application runtime with an explicit operational contract
Documentation
//! The application work selected for one process.

use crate::{Error, Result};
use std::str::FromStr;

/// Select which application routes and workers run.
///
/// Registered [services](crate::App::service) run in every role. The configured
/// admin listener serves health and metrics for every role.
///
/// Parse a role supplied by the application's CLI or configuration.
///
/// ```
/// use servlet::Role;
/// assert_eq!("all".parse::<Role>()?, Role::All);
/// assert_eq!("api".parse::<Role>()?, Role::Api);
/// assert_eq!("worker".parse::<Role>()?, Role::Worker);
/// assert!("WORKER".parse::<Role>().is_err());
/// # Ok::<(), servlet::Error>(())
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Role {
    /// Run the registered application routes and workers.
    #[default]
    All,
    /// Serve application routes.
    Api,
    /// Run background workers.
    Worker,
}

impl Role {
    /// Whether the role serves registered application routes.
    pub const fn includes_api(self) -> bool {
        matches!(self, Self::All | Self::Api)
    }

    /// Whether the role runs tasks registered with [`crate::App::worker`].
    pub const fn includes_workers(self) -> bool {
        matches!(self, Self::All | Self::Worker)
    }
}

impl FromStr for Role {
    type Err = Error;

    fn from_str(value: &str) -> Result<Self> {
        match value {
            "all" => Ok(Self::All),
            "api" => Ok(Self::Api),
            "worker" => Ok(Self::Worker),
            _ => Err(Error::Registration(
                "role must be all, api, or worker".into(),
            )),
        }
    }
}