mln-rs-plugin 0.0.23

Safe Rust SDK for writing MapLibre layer, source, and expression plugins
Documentation
//! Status codes and diagnostic errors returned by plugin host calls.

use core::fmt;

/// Status code returned by host calls.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Status {
    InvalidArgument,
    InvalidState,
    WrongThread,
    Unsupported,
    NativeError,
    Cancelled,
    Busy,
    TargetLost,
    NotFound,
    AlreadyExists,
    Other(i32),
}

/// Error returned by plugin host calls, with an optional diagnostic message.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error {
    pub status: Status,
    pub message: String,
}

impl Error {
    #[must_use]
    pub fn new(status: Status, message: impl Into<String>) -> Self {
        Self { status, message: message.into() }
    }

    #[must_use]
    pub fn invalid_argument(message: impl Into<String>) -> Self {
        Self::new(Status::InvalidArgument, message)
    }

    #[must_use]
    pub fn invalid_state(message: impl Into<String>) -> Self {
        Self::new(Status::InvalidState, message)
    }

    #[must_use]
    pub fn not_found(message: impl Into<String>) -> Self {
        Self::new(Status::NotFound, message)
    }

    #[must_use]
    pub fn already_exists(message: impl Into<String>) -> Self {
        Self::new(Status::AlreadyExists, message)
    }

    #[must_use]
    pub fn native(message: impl Into<String>) -> Self {
        Self::new(Status::NativeError, message)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.message.is_empty() {
            write!(f, "{:?}", self.status)
        } else {
            write!(f, "{:?}: {}", self.status, self.message)
        }
    }
}

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

/// Result alias using [`Error`].
pub type Result<T> = std::result::Result<T, Error>;