Skip to content

Commit ae64bb1

Browse files
author
Andreas Auernhammer
committed
implement Debug trait explicitly for NotAuthentic and Exceeded
This commit implements the `Debug` trait for `NotAuthentic` and `Exceeded` explicitly such that the printed output is guaranteed to be equal. Further, it adds some `#[inline]` directives.
1 parent a7a640c commit ae64bb1

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/error.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
1-
use std::error::Error;
2-
use std::{fmt, io};
1+
use std::{error::Error, fmt, io};
32

4-
#[derive(Clone, Copy, Debug, PartialEq)]
3+
/// An error indicating that the encrypted data is not authentic - e.g.
4+
/// malisously modified.
5+
///
6+
/// It happens whenever the decryption of some ciphertext fails.
7+
#[derive(Clone, Copy, PartialEq)]
58
pub struct NotAuthentic;
69

710
impl NotAuthentic {
811
const fn description() -> &'static str {
9-
"not authentic"
12+
"data is not authentic"
1013
}
1114
}
1215

1316
impl Error for NotAuthentic {
17+
#[inline]
1418
fn description(&self) -> &str {
1519
Self::description()
1620
}
1721
}
1822

23+
impl fmt::Debug for NotAuthentic {
24+
#[inline]
25+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26+
write!(f, "{}", Self::description())
27+
}
28+
}
29+
1930
impl fmt::Display for NotAuthentic {
31+
#[inline]
2032
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2133
write!(f, "{}", Self::description())
2234
}
2335
}
2436

2537
impl From<NotAuthentic> for io::Error {
38+
#[inline]
2639
fn from(_: NotAuthentic) -> Self {
2740
io::Error::new(io::ErrorKind::InvalidData, NotAuthentic)
2841
}
2942
}
3043

31-
#[derive(Clone, Copy, Debug, PartialEq)]
44+
#[derive(Clone, Copy, PartialEq)]
3245
pub struct Exceeded;
3346

3447
impl Exceeded {
@@ -38,18 +51,28 @@ impl Exceeded {
3851
}
3952

4053
impl Error for Exceeded {
54+
#[inline]
4155
fn description(&self) -> &str {
4256
Self::description()
4357
}
4458
}
4559

4660
impl fmt::Display for Exceeded {
61+
#[inline]
62+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63+
write!(f, "{}", Self::description())
64+
}
65+
}
66+
67+
impl fmt::Debug for Exceeded {
68+
#[inline]
4769
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4870
write!(f, "{}", Self::description())
4971
}
5072
}
5173

5274
impl From<Exceeded> for io::Error {
75+
#[inline]
5376
fn from(_: Exceeded) -> Self {
5477
io::Error::new(io::ErrorKind::InvalidData, Exceeded)
5578
}

0 commit comments

Comments
 (0)