grpc 0.9.0

The official Rust implementation of gRPC: a high performance, open source, universal RPC framework.
Documentation
/*
 *
 * Copyright 2025 gRPC authors.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */

mod server_status;
mod status_code;

pub use status_code::StatusCodeError;

/// Represents either a failing gRPC status or a successful result containing
/// `T`.
pub type StatusOr<T> = Result<T, StatusError>;

/// The representation of a gRPC status.  OK statuses may not contain a status
/// message, while error values may.
pub type Status = StatusOr<()>;

/// Represents a gRPC status.
#[derive(Debug, Clone)]
pub struct StatusError {
    code: StatusCodeError,
    message: String,
}

impl StatusError {
    /// Create a new [`StatusError`] with the given code and message.
    pub fn new(code: StatusCodeError, message: impl Into<String>) -> Self {
        StatusError {
            code,
            message: message.into(),
        }
    }

    /// Get the [`StatusCodeError`] of this [`StatusError`].
    pub fn code(&self) -> StatusCodeError {
        self.code
    }

    /// Get the message of this [`StatusError`].
    pub fn message(&self) -> &str {
        &self.message
    }

    /// Returns whether the status includes a code restricted for control
    /// plane usage as defined by gRFC A54.
    pub(crate) fn is_restricted_control_plane_code(&self) -> bool {
        matches!(
            self.code,
            StatusCodeError::InvalidArgument
                | StatusCodeError::NotFound
                | StatusCodeError::AlreadyExists
                | StatusCodeError::FailedPrecondition
                | StatusCodeError::Aborted
                | StatusCodeError::OutOfRange
                | StatusCodeError::DataLoss
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_status_new() {
        let status = StatusError::new(StatusCodeError::NotFound, "not ok");
        assert_eq!(status.code(), StatusCodeError::NotFound);
        assert_eq!(status.message(), "not ok");
    }

    #[test]
    fn test_status_debug() {
        let status = StatusError::new(StatusCodeError::Cancelled, "not ok");
        let debug = format!("{:?}", status);
        assert!(debug.contains("Status"));
        assert!(debug.contains("Cancelled"));
        assert!(debug.contains("not ok"));
    }
}