1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2023 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

//! # pw_status
//!
//! Rust error types using error codes compatible with Pigweed's
//! [pw_status](https://pigweed.dev/pw_status).  In order to keep the interface
//! idiomatic for Rust, `PW_STATUS_OK` is omitted from the Error enum and a
//! `StatusCode` trait is provided to turn a `Result` into a canonical
//! status code.
//!
//! For an in depth explanation of the values of the `Error` enum, see
//! the [Pigweed status codes documentation](https://pigweed.dev/pw_status/#status-codes).
//!
//! # Example
//!
//! ```
//! use pw_status::{Error, Result};
//!
//! fn div(numerator: u32, denominator: u32) -> Result<u32> {
//!     if denominator == 0 {
//!         Err(Error::FailedPrecondition)
//!     } else {
//!         Ok(numerator / denominator)
//!     }
//! }
//!
//! assert_eq!(div(4, 2), Ok(2));
//! assert_eq!(div(4, 0), Err(Error::FailedPrecondition));
//! ```

#![cfg_attr(not(feature = "std"), no_std)]

/// Status code for no error.
pub const OK: u32 = 0;

#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, Copy, Eq, PartialEq)]
/// Error type compatible with Pigweed's [pw_status](https://pigweed.dev/pw_status).
///
/// For an in depth explanation of the values of the `Error` enum, see
/// the [Pigweed status codes documentation](https://pigweed.dev/pw_status/#status-codes).
pub enum Error {
    Cancelled = 1,
    Unknown = 2,
    InvalidArgument = 3,
    DeadlineExceeded = 4,
    NotFound = 5,
    AlreadyExists = 6,
    PermissionDenied = 7,
    ResourceExhausted = 8,
    FailedPrecondition = 9,
    Aborted = 10,
    OutOfRange = 11,
    Unimplemented = 12,
    Internal = 13,
    Unavailable = 14,
    DataLoss = 15,
    Unauthenticated = 16,
}

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

/// Convert a Result into an status code.
pub trait StatusCode {
    /// Return a pigweed compatible status code.
    fn status_code(self) -> u32;
}

impl<T> StatusCode for Result<T> {
    fn status_code(self) -> u32 {
        match self {
            Ok(_) => OK,
            Err(e) => e as u32,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_status_code() {
        assert_eq!(Result::Ok(()).status_code(), 0);
        assert_eq!(Result::<()>::Err(Error::Cancelled).status_code(), 1);
        assert_eq!(Result::<()>::Err(Error::Unknown).status_code(), 2);
        assert_eq!(Result::<()>::Err(Error::InvalidArgument).status_code(), 3);
        assert_eq!(Result::<()>::Err(Error::DeadlineExceeded).status_code(), 4);
        assert_eq!(Result::<()>::Err(Error::NotFound).status_code(), 5);
        assert_eq!(Result::<()>::Err(Error::AlreadyExists).status_code(), 6);
        assert_eq!(Result::<()>::Err(Error::PermissionDenied).status_code(), 7);
        assert_eq!(Result::<()>::Err(Error::ResourceExhausted).status_code(), 8);
        assert_eq!(
            Result::<()>::Err(Error::FailedPrecondition).status_code(),
            9
        );
        assert_eq!(Result::<()>::Err(Error::Aborted).status_code(), 10);
        assert_eq!(Result::<()>::Err(Error::OutOfRange).status_code(), 11);
        assert_eq!(Result::<()>::Err(Error::Unimplemented).status_code(), 12);
        assert_eq!(Result::<()>::Err(Error::Internal).status_code(), 13);
        assert_eq!(Result::<()>::Err(Error::Unavailable).status_code(), 14);
        assert_eq!(Result::<()>::Err(Error::DataLoss).status_code(), 15);
        assert_eq!(Result::<()>::Err(Error::Unauthenticated).status_code(), 16);
    }
}