ugi 0.2.1

Runtime-agnostic Rust request client with HTTP/1.1, HTTP/2, HTTP/3, H2C, WebSocket, SSE, and gRPC support
Documentation
use std::time::Duration;

use crate::response::Version;

/// Per-request timing and connection diagnostics.
///
/// Populated by the transport layer and accessible via
/// [`Response::metrics`](crate::Response::metrics).  Fields are `None` when
/// the corresponding phase was skipped (e.g. `dns_duration` is `None` for
/// connections reused from the pool).
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Metrics {
    dns_duration: Option<Duration>,
    connect_duration: Option<Duration>,
    tls_duration: Option<Duration>,
    request_write_duration: Option<Duration>,
    ttfb: Option<Duration>,
    download_duration: Option<Duration>,
    reused_connection: bool,
    protocol: Option<Version>,
}

impl Metrics {
    /// Time spent resolving the hostname via DNS.
    pub fn dns_duration(&self) -> Option<Duration> {
        self.dns_duration
    }

    /// Time spent establishing the TCP connection.
    pub fn connect_duration(&self) -> Option<Duration> {
        self.connect_duration
    }

    /// Time spent completing the TLS handshake.
    pub fn tls_duration(&self) -> Option<Duration> {
        self.tls_duration
    }

    /// Time spent writing the request headers and body.
    pub fn request_write_duration(&self) -> Option<Duration> {
        self.request_write_duration
    }

    /// Time to first byte — from request flush to first byte of the response.
    pub fn ttfb(&self) -> Option<Duration> {
        self.ttfb
    }

    /// Time spent reading the response body.
    pub fn download_duration(&self) -> Option<Duration> {
        self.download_duration
    }

    /// Returns `true` if this request reused an existing pooled connection.
    pub fn reused_connection(&self) -> bool {
        self.reused_connection
    }

    /// The HTTP protocol version used for this request.
    pub fn protocol(&self) -> Option<Version> {
        self.protocol
    }

    pub(crate) fn with_dns_duration(mut self, value: Option<Duration>) -> Self {
        self.dns_duration = value;
        self
    }

    pub(crate) fn with_connect_duration(mut self, value: Option<Duration>) -> Self {
        self.connect_duration = value;
        self
    }

    pub(crate) fn with_tls_duration(mut self, value: Option<Duration>) -> Self {
        self.tls_duration = value;
        self
    }

    pub(crate) fn with_request_write_duration(mut self, value: Option<Duration>) -> Self {
        self.request_write_duration = value;
        self
    }

    pub(crate) fn with_ttfb(mut self, value: Option<Duration>) -> Self {
        self.ttfb = value;
        self
    }

    pub(crate) fn with_download_duration(mut self, value: Option<Duration>) -> Self {
        self.download_duration = value;
        self
    }

    pub(crate) fn with_reused_connection(mut self, value: bool) -> Self {
        self.reused_connection = value;
        self
    }

    pub(crate) fn with_protocol(mut self, value: Version) -> Self {
        self.protocol = Some(value);
        self
    }
}