stratum-node-core 0.1.0-alpha.3

Zero-I/O core types for Stratum pod nodes: did:nostr principals, typed identifiers, a strict NIP-98 signed-request verifier and bootstrap claim bodies
Documentation
//! The error codes Step 3a adds to the JSON error shape, with their HTTP
//! statuses.

use std::fmt;

/// An error code and the HTTP status it is sent with.
///
/// Every Stratum error response is `{"error":{"code","message"}}`. The codes
/// defined before Step 3a are written by the crate that sends them. This
/// type gives the codes Step 3a adds one definition, shared by the
/// controller and the gate that send them and the client that reads them.
///
/// # Examples
///
/// ```
/// use stratum_node_core::ErrorCode;
///
/// assert_eq!(ErrorCode::SUSPENDED.as_str(), "suspended");
/// assert_eq!(ErrorCode::SUSPENDED.status(), 403);
/// assert_eq!(ErrorCode::from_code("archive_required"), Some(ErrorCode::ARCHIVE_REQUIRED));
/// assert_eq!(ErrorCode::from_code("conflict"), None);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ErrorCode {
    code: &'static str,
    status: u16,
}

impl ErrorCode {
    /// `suspended`, 403: the requester, or the owner of the pod in the path,
    /// is suspended. Also the answer to `redeem` and `confirm` by a
    /// suspended key, decided before any stored response is looked up.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::SUSPENDED.to_string(), "suspended");
    /// ```
    pub const SUSPENDED: Self = Self {
        code: "suspended",
        status: 403,
    };

    /// `archive_required`, 409: `remove` requires an archive, and none was
    /// taken for the member's current generation and suspension number.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::ARCHIVE_REQUIRED.status(), 409);
    /// ```
    pub const ARCHIVE_REQUIRED: Self = Self {
        code: "archive_required",
        status: 409,
    };

    /// `pod_data_present`, 409: an invitation or redemption for a `removed`
    /// or `cancelled` key whose old pod data has not been purged.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::POD_DATA_PRESENT.as_str(), "pod_data_present");
    /// ```
    pub const POD_DATA_PRESENT: Self = Self {
        code: "pod_data_present",
        status: 409,
    };

    /// `app_forbidden`, 403: an application key used on a route, listener or
    /// pod it may not reach, or a member key sent to the gate's proxy
    /// listener.
    pub const APP_FORBIDDEN: Self = Self {
        code: "app_forbidden",
        status: 403,
    };

    /// `rate_limited`, 429: a principal, all application keys together, or
    /// the whole node exceeded its signed-pod-request rate limit.
    pub const RATE_LIMITED: Self = Self {
        code: "rate_limited",
        status: 429,
    };

    /// Every code this type defines.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert!(ErrorCode::ALL.iter().all(|c| ErrorCode::from_code(c.as_str()) == Some(*c)));
    /// ```
    pub const ALL: [Self; 5] = [
        Self::SUSPENDED,
        Self::ARCHIVE_REQUIRED,
        Self::POD_DATA_PRESENT,
        Self::APP_FORBIDDEN,
        Self::RATE_LIMITED,
    ];

    /// Returns the code as it appears in `error.code`.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::POD_DATA_PRESENT.as_str(), "pod_data_present");
    /// ```
    pub const fn as_str(self) -> &'static str {
        self.code
    }

    /// Returns the HTTP status the code is sent with.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::ARCHIVE_REQUIRED.status(), 409);
    /// ```
    pub const fn status(self) -> u16 {
        self.status
    }

    /// Returns the code named `code`, if this type defines it. The match is
    /// exact and case-sensitive.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::from_code("suspended"), Some(ErrorCode::SUSPENDED));
    /// assert_eq!(ErrorCode::from_code("Suspended"), None);
    /// ```
    pub fn from_code(code: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|known| known.code == code)
    }
}

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