|
| 1 | +// Copyright 2017-2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! Error types |
| 12 | +
|
| 13 | +use core::fmt; |
| 14 | + |
| 15 | +#[cfg(feature="std")] |
| 16 | +use std::error::Error as stdError; |
| 17 | + |
| 18 | +/// Error kind which can be matched over. |
| 19 | +#[derive(PartialEq, Eq, Debug, Copy, Clone)] |
| 20 | +pub enum ErrorKind { |
| 21 | + /// Permanent failure: likely not recoverable without user action. |
| 22 | + Unavailable, |
| 23 | + /// Temporary failure: recommended to retry a few times, but may also be |
| 24 | + /// irrecoverable. |
| 25 | + Transient, |
| 26 | + /// Not ready yet: recommended to try again a little later. |
| 27 | + NotReady, |
| 28 | + /// Uncategorised error |
| 29 | + Other, |
| 30 | + #[doc(hidden)] |
| 31 | + __Nonexhaustive, |
| 32 | +} |
| 33 | + |
| 34 | +impl ErrorKind { |
| 35 | + /// True if this kind of error may resolve itself on retry. |
| 36 | + /// |
| 37 | + /// See also `should_wait()`. |
| 38 | + pub fn should_retry(self) -> bool { |
| 39 | + match self { |
| 40 | + ErrorKind::Transient | ErrorKind::NotReady => true, |
| 41 | + _ => false, |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /// True if we should retry but wait before retrying |
| 46 | + /// |
| 47 | + /// This implies `should_retry()` is true. |
| 48 | + pub fn should_wait(self) -> bool { |
| 49 | + self == ErrorKind::NotReady |
| 50 | + } |
| 51 | + |
| 52 | + /// A description of this error kind |
| 53 | + pub fn description(self) -> &'static str { |
| 54 | + match self { |
| 55 | + ErrorKind::Unavailable => "permanent failure or unavailable", |
| 56 | + ErrorKind::Transient => "transient failure", |
| 57 | + ErrorKind::NotReady => "not ready yet", |
| 58 | + ErrorKind::Other => "uncategorised", |
| 59 | + ErrorKind::__Nonexhaustive => unreachable!(), |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// Error type of random number generators |
| 65 | +/// |
| 66 | +/// This is a relatively simple error type, designed for compatibility with and |
| 67 | +/// without the Rust `std` library. It embeds a "kind" code, a message (static |
| 68 | +/// string only), and an optional chained cause (`std` only). |
| 69 | +#[derive(Debug)] |
| 70 | +pub struct Error { |
| 71 | + kind: ErrorKind, |
| 72 | + msg: &'static str, |
| 73 | + #[cfg(feature="std")] |
| 74 | + cause: Option<Box<stdError + Send + Sync>>, |
| 75 | +} |
| 76 | + |
| 77 | +impl Error { |
| 78 | + /// Create a new instance, with specified kind and a message. |
| 79 | + pub fn new(kind: ErrorKind, msg: &'static str) -> Self { |
| 80 | + #[cfg(feature="std")] { |
| 81 | + Error { kind: kind, msg: msg, cause: None } |
| 82 | + } |
| 83 | + #[cfg(not(feature="std"))] { |
| 84 | + Error { kind: kind, msg: msg } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// Create a new instance, with specified kind, message, and a |
| 89 | + /// chained cause. |
| 90 | + /// |
| 91 | + /// Note: `stdError` is an alias for `std::error::Error`. |
| 92 | + /// |
| 93 | + /// If not targetting `std` (i.e. `no_std`), this function is replaced by |
| 94 | + /// another with the same prototype, except that there are no bounds on the |
| 95 | + /// type `E` (because both `Box` and `stdError` are unavailable), and the |
| 96 | + /// `cause` is ignored. |
| 97 | + #[cfg(feature="std")] |
| 98 | + pub fn with_cause<E>(kind: ErrorKind, msg: &'static str, cause: E) -> Self |
| 99 | + where E: Into<Box<stdError + Send + Sync>> |
| 100 | + { |
| 101 | + Error { kind: kind, msg: msg, cause: Some(cause.into()) } |
| 102 | + } |
| 103 | + |
| 104 | + /// Create a new instance, with specified kind, message, and a |
| 105 | + /// chained cause. |
| 106 | + /// |
| 107 | + /// In `no_std` mode the *cause* is ignored. |
| 108 | + #[cfg(not(feature="std"))] |
| 109 | + pub fn with_cause<E>(kind: ErrorKind, msg: &'static str, _cause: E) -> Self { |
| 110 | + Error { kind: kind, msg: msg } |
| 111 | + } |
| 112 | + |
| 113 | + /// Get the error kind |
| 114 | + pub fn kind(&self) -> ErrorKind { |
| 115 | + self.kind |
| 116 | + } |
| 117 | + |
| 118 | + /// Get the error message |
| 119 | + pub fn msg(&self) -> &'static str { |
| 120 | + self.msg |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +impl fmt::Display for Error { |
| 125 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 126 | + write!(f, "RNG error [{}]: {}", self.kind.description(), self.msg()) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +#[cfg(feature="std")] |
| 131 | +impl stdError for Error { |
| 132 | + fn description(&self) -> &str { |
| 133 | + self.msg |
| 134 | + } |
| 135 | + |
| 136 | + fn cause(&self) -> Option<&stdError> { |
| 137 | + self.cause.as_ref().map(|e| e.as_ref() as &stdError) |
| 138 | + } |
| 139 | +} |
0 commit comments