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 to be implemented for the associated Error used in
8/// [`DeserializableSlice::from_slice`]. The function is called if the slice
9/// given is smaller than the mandatory size for the struct.
10pub trait BadLength {
11 /// Invoked when a buffer of bad length is given to [`from_slice`]
12 fn bad_length(found: usize, expected: usize) -> Self;
13}
14
15/// Trait to be implemented for the associated Error used in
16/// [`ParseHexStr::from_hex_str`].
17/// The function is called if an invalid character is found in the string
18/// slice.
19pub trait InvalidChar {
20 /// Invoked when a string slice with a non hex character is is give to
21 /// [`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 [`Deserializable::from_bytes`]
29 /// implementation
30 InvalidData,
31 /// Automatically returned from the default implementation of
32 /// [`DeserializableSlice::from_slice`] if the slice given is smaller than
33 /// the mandatory size for the struct.
34 BadLength {
35 /// The slice's length
36 found: usize,
37 /// The expected slice's length
38 expected: usize,
39 },
40 /// Automatically returned from the default implementation of
41 /// [`ParseHexStr::from_hex_str`] if an invalid character is found in the
42 /// string slice.
43 InvalidChar {
44 /// The invalid character found
45 ch: char,
46 /// The character's index
47 index: usize,
48 },
49}
50
51impl BadLength for Error {
52 fn bad_length(found: usize, expected: usize) -> Self {
53 Self::BadLength { found, expected }
54 }
55}
56
57impl InvalidChar for Error {
58 fn invalid_char(ch: char, index: usize) -> Self {
59 Self::InvalidChar { ch, index }
60 }
61}