servlet 1.0.0

A small application runtime with an explicit operational contract
Documentation
//! A handler observes the same absolute cutoff that owns its admitted exchange.

use axum::{
    extract::FromRequestParts,
    http::{StatusCode, request::Parts},
};
use std::time::Duration;
use tokio::time::Instant;

/// Extract the absolute request deadline chosen by Servlet at admission.
///
/// Middleware, body extraction, handlers, and response transfer share this budget.
/// Passing [`Self::instant`] to dependency work preserves time already consumed;
/// extracting or copying this value never renews the request allowance. Incoming
/// headers cannot change it. This extractor fails with 500 outside Servlet's owned
/// request service or if application middleware removes the extension.
#[derive(Clone, Copy, Debug)]
pub struct RequestDeadline(pub(super) Instant);

impl RequestDeadline {
    /// The exchange's original absolute cutoff, using the current Tokio clock.
    pub fn instant(self) -> Instant {
        self.0
    }

    /// Time left at the instant of this call, saturating at zero after expiry.
    pub fn remaining(self) -> Duration {
        self.0.saturating_duration_since(Instant::now())
    }
}

impl<S: Send + Sync> FromRequestParts<S> for RequestDeadline {
    type Rejection = StatusCode;

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        parts
            .extensions
            .get::<Self>()
            .copied()
            .ok_or(StatusCode::INTERNAL_SERVER_ERROR)
    }
}

#[cfg(test)]
#[path = "deadline_test.rs"]
mod deadline_test;