dusk_bytes/errors.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7/// Trait implemented by error types used by
8/// [`DeserializableSlice::from_slice`](crate::DeserializableSlice::from_slice).
9/// The method is called when the given slice is shorter than the required size.
10pub trait BadLength {
11 /// Invoked when a buffer of bad length is given to
12 /// [`DeserializableSlice::from_slice`](crate::DeserializableSlice::from_slice).
13 fn bad_length(found: usize, expected: usize) -> Self;
14}
15
16/// Trait implemented by error types used by
17/// [`ParseHexStr::from_hex_str`](crate::ParseHexStr::from_hex_str).
18/// The method is called when an invalid character is found in the string slice.
19pub trait InvalidChar {
20 /// Invoked when a string slice with a non-hex character is given to
21 /// [`ParseHexStr::from_hex_str`](crate::ParseHexStr::from_hex_str).
22 fn invalid_char(ch: char, index: usize) -> Self;
23}
24
25/// Dusk Bytes operation error variants
26#[derive(Copy, Debug, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
27pub enum Error {
28 /// Generic error that can be returned in a
29 /// [`Serializable::from_bytes`](crate::Serializable::from_bytes)
30 /// implementation.
31 InvalidData,
32 /// Automatically returned from the default implementation of
33 /// [`DeserializableSlice::from_slice`](crate::DeserializableSlice::from_slice)
34 /// if the slice given is smaller than the mandatory size for the struct.
35 BadLength {
36 /// The slice's length
37 found: usize,
38 /// The expected slice's length
39 expected: usize,
40 },
41 /// Automatically returned from the default implementation of
42 /// [`ParseHexStr::from_hex_str`](crate::ParseHexStr::from_hex_str) if an
43 /// invalid character is found in the string slice.
44 InvalidChar {
45 /// The invalid character found
46 ch: char,
47 /// The character's index
48 index: usize,
49 },
50}
51
52impl BadLength for Error {
53 fn bad_length(found: usize, expected: usize) -> Self {
54 Self::BadLength { found, expected }
55 }
56}
57
58impl InvalidChar for Error {
59 fn invalid_char(ch: char, index: usize) -> Self {
60 Self::InvalidChar { ch, index }
61 }
62}