Skip to content

Commit e36cc96

Browse files
nyonsontcharding
andcommitted
consensus_encoding: rename UnexptectedEof
It is policy that all error types are suffixed with `Error` and live at the bottom of the file. Co-authored-by: Tobin C. Harding <me@tobin.cc>
1 parent 947f490 commit e36cc96

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

consensus_encoding/src/decode/decoders.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@
44
55
use super::Decoder;
66

7-
/// Not enough bytes given to decoder.
8-
#[derive(Debug, Clone, PartialEq, Eq)]
9-
pub struct UnexpectedEof {
10-
/// Number of bytes missing to complete decoder.
11-
missing: usize,
12-
}
13-
14-
impl core::fmt::Display for UnexpectedEof {
15-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
16-
write!(f, "not enough bytes for decoder, {} more bytes required", self.missing)
17-
}
18-
}
19-
20-
#[cfg(feature = "std")]
21-
impl std::error::Error for UnexpectedEof {}
22-
237
/// A decoder that expects exactly N bytes and returns them as an array.
248
pub struct ArrayDecoder<const N: usize> {
259
buffer: [u8; N],
@@ -37,7 +21,7 @@ impl<const N: usize> Default for ArrayDecoder<N> {
3721

3822
impl<const N: usize> Decoder for ArrayDecoder<N> {
3923
type Output = [u8; N];
40-
type Error = UnexpectedEof;
24+
type Error = UnexpectedEofError;
4125

4226
fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bool, Self::Error> {
4327
let remaining_space = N - self.bytes_written;
@@ -59,7 +43,7 @@ impl<const N: usize> Decoder for ArrayDecoder<N> {
5943
if self.bytes_written == N {
6044
Ok(self.buffer)
6145
} else {
62-
Err(UnexpectedEof { missing: N - self.bytes_written })
46+
Err(UnexpectedEofError { missing: N - self.bytes_written })
6347
}
6448
}
6549
}
@@ -355,3 +339,19 @@ where
355339
Ok((first, second, third, fourth, fifth, sixth))
356340
}
357341
}
342+
343+
/// Not enough bytes given to decoder.
344+
#[derive(Debug, Clone, PartialEq, Eq)]
345+
pub struct UnexpectedEofError {
346+
/// Number of bytes missing to complete decoder.
347+
missing: usize,
348+
}
349+
350+
impl core::fmt::Display for UnexpectedEofError {
351+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
352+
write!(f, "not enough bytes for decoder, {} more bytes required", self.missing)
353+
}
354+
}
355+
356+
#[cfg(feature = "std")]
357+
impl std::error::Error for UnexpectedEofError {}

consensus_encoding/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ mod decode;
2323
mod encode;
2424

2525
pub use self::decode::decoders::{
26-
ArrayDecoder, Decoder2, Decoder3, Decoder4, Decoder6, UnexpectedEof,
26+
ArrayDecoder, Decoder2, Decoder3, Decoder4, Decoder6, UnexpectedEofError,
2727
};
2828
pub use self::decode::{Decodable, Decoder};
2929
#[cfg(feature = "alloc")]

consensus_encoding/tests/composition.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use consensus_encoding::{
66
ArrayDecoder, ArrayEncoder, Decodable, Decoder, Decoder2, Decoder6, Encodable, Encoder,
7-
Encoder2, Encoder6, UnexpectedEof,
7+
Encoder2, Encoder6, UnexpectedEofError,
88
};
99

1010
const EMPTY: &[u8] = &[];
@@ -30,11 +30,11 @@ impl Encodable for CompositeData {
3030
/// A unified error type for [`CompositeDataDecoder`].
3131
#[derive(Debug, Clone, PartialEq, Eq)]
3232
enum CompositeError {
33-
Eof(UnexpectedEof),
33+
Eof(UnexpectedEofError),
3434
}
3535

36-
impl From<UnexpectedEof> for CompositeError {
37-
fn from(eof: UnexpectedEof) -> Self { CompositeError::Eof(eof) }
36+
impl From<UnexpectedEofError> for CompositeError {
37+
fn from(eof: UnexpectedEofError) -> Self { CompositeError::Eof(eof) }
3838
}
3939

4040
impl core::fmt::Display for CompositeError {
@@ -115,7 +115,7 @@ fn composition_nested() {
115115
}
116116
assert_eq!(encoded_bytes, data);
117117

118-
let mut decoder6: Decoder6<_, _, _, _, _, _, UnexpectedEof> = Decoder6::new(
118+
let mut decoder6: Decoder6<_, _, _, _, _, _, UnexpectedEofError> = Decoder6::new(
119119
ArrayDecoder::<1>::new(),
120120
ArrayDecoder::<1>::new(),
121121
ArrayDecoder::<1>::new(),
@@ -139,7 +139,7 @@ fn composition_nested() {
139139
#[test]
140140
fn composition_extra_bytes() {
141141
// Test that Decoder2 consumes exactly what it needs and leaves extra bytes unconsumed.
142-
let mut decoder2: Decoder2<_, _, UnexpectedEof> =
142+
let mut decoder2: Decoder2<_, _, UnexpectedEofError> =
143143
Decoder2::new(ArrayDecoder::<2>::new(), ArrayDecoder::<3>::new());
144144
let mut bytes = &[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08][..];
145145
let original_len = bytes.len();
@@ -167,22 +167,22 @@ fn composition_error_unification() {
167167
#[derive(Debug, Clone, PartialEq, Eq)]
168168
enum NestedError {
169169
BadChecksum,
170-
UnexpectedEof(UnexpectedEof),
170+
UnexpectedEof(UnexpectedEofError),
171171
}
172172

173-
impl From<UnexpectedEof> for NestedError {
174-
fn from(eof: UnexpectedEof) -> Self { NestedError::UnexpectedEof(eof) }
173+
impl From<UnexpectedEofError> for NestedError {
174+
fn from(eof: UnexpectedEofError) -> Self { NestedError::UnexpectedEof(eof) }
175175
}
176176

177177
/// Error for top level encoder.
178178
#[derive(Debug, Clone, PartialEq, Eq)]
179179
enum TopLevelError {
180-
UnexpectedEof(UnexpectedEof),
180+
UnexpectedEof(UnexpectedEofError),
181181
Validation(NestedError),
182182
}
183183

184-
impl From<UnexpectedEof> for TopLevelError {
185-
fn from(eof: UnexpectedEof) -> Self { TopLevelError::UnexpectedEof(eof) }
184+
impl From<UnexpectedEofError> for TopLevelError {
185+
fn from(eof: UnexpectedEofError) -> Self { TopLevelError::UnexpectedEof(eof) }
186186
}
187187

188188
impl From<NestedError> for TopLevelError {

consensus_encoding/tests/decode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//! Integration tests for decode module.
44
5-
use consensus_encoding::{ArrayDecoder, Decoder, UnexpectedEof};
5+
use consensus_encoding::{ArrayDecoder, Decoder, UnexpectedEofError};
66

77
const EMPTY: &[u8] = &[];
88

@@ -50,5 +50,5 @@ fn decode_array_insufficient_data_error() {
5050
assert_eq!(data, EMPTY);
5151

5252
let err = decoder.end().unwrap_err();
53-
assert!(matches!(err, UnexpectedEof { .. }));
53+
assert!(matches!(err, UnexpectedEofError { .. }));
5454
}

0 commit comments

Comments
 (0)