soaprs-http 0.2.0

Transport-neutral HTTP metadata for soaprs
Documentation
//! Framework-independent endpoint policies.

use std::{num::NonZeroU32, time::Duration};

use soaprs_core::{SoapError, SoapResult};

/// Declarative authorization requirement for an endpoint.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum AuthorizationPolicy {
    /// No authenticated identity is required.
    #[default]
    Public,
    /// Any authenticated identity is allowed.
    Authenticated,
    /// The identity must have at least one listed role.
    AnyRole(Vec<String>),
    /// The identity must have every listed role.
    AllRoles(Vec<String>),
}

/// Declarative per-key rate limit translated by an HTTP adapter.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RateLimitPolicy {
    /// Maximum number of requests in the period.
    pub requests: NonZeroU32,
    /// Length of the rate-limit period.
    pub period: Duration,
}

impl RateLimitPolicy {
    /// Creates a rate-limit policy with a non-zero period.
    pub fn new(requests: NonZeroU32, period: Duration) -> SoapResult<Self> {
        if period.is_zero() {
            return Err(SoapError::validation(
                "rate-limit period must be greater than zero",
            ));
        }
        Ok(Self { requests, period })
    }
}