licers 1.0.1

Licers license-key verification: Ed25519-signed responses, floating seats, usage credits, remote variables, offline licenses.
Documentation
use std::fmt;

/// Every error the SDK returns.
#[derive(Debug, Clone)]
pub enum Error {
    /// The license server could not be reached.
    Network(String),
    /// The server answered with a failure of its own (HTTP 5xx) — an outage,
    /// not a verdict on the key. Handle it like [`Error::Network`].
    Server { status: u16, message: String },
    /// The server refused the key: invalid, expired, revoked, blocked...
    InvalidLicense { status: u16, message: String },
    /// Correctly signed, but for a different product.
    ProductMismatch { expected: String, actual: String },
    /// A usage-based key doesn't have enough credits left. Nothing was spent.
    /// `usage` is what the server reported (unsigned: for messages only).
    UsageExceeded { message: String, usage: serde_json::Value },
    /// Signature, nonce or timestamp check failed: forged or replayed.
    Signature(String),
    /// The reply wasn't a usable response body.
    Protocol(String),
    /// A floating license has no free seat, or a held seat was lost.
    SeatUnavailable(String),
    /// Bad arguments (e.g. a malformed public key).
    Config(String),
}

impl Error {
    /// True for problems reaching the server (offline, 5xx) — the cases where
    /// an app usually retries or applies a grace period.
    pub fn is_network(&self) -> bool {
        matches!(self, Error::Network(_) | Error::Server { .. })
    }
    /// True when the server (or the signature check) says the key may not be used.
    pub fn is_refusal(&self) -> bool {
        matches!(self, Error::InvalidLicense { .. } | Error::ProductMismatch { .. } | Error::UsageExceeded { .. })
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Network(m) | Error::Signature(m) | Error::Protocol(m) | Error::SeatUnavailable(m) | Error::Config(m) => f.write_str(m),
            Error::Server { message, .. } | Error::InvalidLicense { message, .. } | Error::UsageExceeded { message, .. } => f.write_str(message),
            Error::ProductMismatch { expected, actual } => write!(
                f, "This license is for a different product. Expected {expected}, got {}.",
                if actual.is_empty() { "none" } else { actual }
            ),
        }
    }
}

impl std::error::Error for Error {}

pub type Result<T> = std::result::Result<T, Error>;