dangerous/error/
fatal.rs

1use crate::fmt;
2use crate::input::Input;
3
4use super::{
5    Context, ExpectedLength, ExpectedValid, ExpectedValue, RetryRequirement, ToRetryRequirement,
6    WithContext,
7};
8
9/// An error that has no details around what went wrong and cannot be retried.
10///
11/// This is the most performant and simplistic catch-all error, but it doesn't
12/// provide any context to debug problems well and cannot be used in streaming
13/// contexts.
14///
15/// See [`crate::error`] for additional documentation around the error system.
16///
17/// # Example
18///
19/// ```
20/// use dangerous::{Input, Fatal};
21///
22/// let error: Fatal = dangerous::input(b"").read_all(|r| {
23///     r.read_u8()
24/// }).unwrap_err();
25///
26/// assert_eq!(
27///     error.to_string(),
28///     "invalid input",
29/// );
30/// ```
31#[derive(Debug, PartialEq)]
32#[must_use = "error must be handled"]
33pub struct Fatal;
34
35impl fmt::DisplayBase for Fatal {
36    fn fmt(&self, w: &mut dyn fmt::Write) -> fmt::Result {
37        w.write_str("invalid input")
38    }
39}
40
41impl fmt::Display for Fatal {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        fmt::DisplayBase::fmt(self, f)
44    }
45}
46
47impl<'i> WithContext<'i> for Fatal {
48    const PASSTHROUGH: bool = true;
49
50    #[inline(always)]
51    fn with_input(self, _input: impl Input<'i>) -> Self {
52        self
53    }
54
55    #[inline(always)]
56    fn with_context(self, _context: impl Context) -> Self {
57        self
58    }
59}
60
61impl ToRetryRequirement for Fatal {
62    fn to_retry_requirement(&self) -> Option<RetryRequirement> {
63        None
64    }
65
66    fn is_fatal(&self) -> bool {
67        true
68    }
69}
70
71impl<'i> From<ExpectedValue<'i>> for Fatal {
72    fn from(_: ExpectedValue<'i>) -> Self {
73        Self
74    }
75}
76
77impl<'i> From<ExpectedLength<'i>> for Fatal {
78    fn from(_: ExpectedLength<'i>) -> Self {
79        Self
80    }
81}
82
83impl<'i> From<ExpectedValid<'i>> for Fatal {
84    fn from(_: ExpectedValid<'i>) -> Self {
85        Self
86    }
87}