phoenix_core/
error.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
7use aes_gcm::Error as AesError;
8use core::fmt;
9use dusk_bytes::{BadLength, Error as DuskBytesError, InvalidChar};
10
11/// All possible errors for Phoenix's Core
12#[allow(missing_docs)]
13#[derive(Debug, Clone)]
14pub enum Error {
15    /// Invalid u8 as Note Type (expected `0` or `1`, found {0})
16    InvalidNoteType(u8),
17    /// ViewKey required for decrypt data from Obfuscated Note
18    MissingViewKey,
19    /// Failure to encrypt / decrypt
20    InvalidEncryption,
21    /// Dusk-bytes InvalidData error
22    InvalidData,
23    /// Dusk-bytes BadLength error
24    BadLength(usize, usize),
25    /// Dusk-bytes InvalidChar error
26    InvalidChar(char, usize),
27}
28
29impl fmt::Display for Error {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        write!(f, "Phoenix-Core Error: {:?}", &self)
32    }
33}
34
35impl From<AesError> for Error {
36    fn from(aes_error: AesError) -> Self {
37        match aes_error {
38            AesError => Self::InvalidEncryption,
39        }
40    }
41}
42
43impl From<DuskBytesError> for Error {
44    fn from(err: DuskBytesError) -> Self {
45        match err {
46            DuskBytesError::InvalidData => Error::InvalidData,
47            DuskBytesError::BadLength { found, expected } => {
48                Error::BadLength(found, expected)
49            }
50            DuskBytesError::InvalidChar { ch, index } => {
51                Error::InvalidChar(ch, index)
52            }
53        }
54    }
55}
56
57impl From<Error> for DuskBytesError {
58    fn from(err: Error) -> Self {
59        match err {
60            Error::InvalidData => DuskBytesError::InvalidData,
61            Error::BadLength(found, expected) => {
62                DuskBytesError::BadLength { found, expected }
63            }
64            Error::InvalidChar(ch, index) => {
65                DuskBytesError::InvalidChar { ch, index }
66            }
67            _ => unreachable!(),
68        }
69    }
70}
71
72impl BadLength for Error {
73    fn bad_length(found: usize, expected: usize) -> Self {
74        Error::BadLength(found, expected)
75    }
76}
77
78impl InvalidChar for Error {
79    fn invalid_char(ch: char, index: usize) -> Self {
80        Error::InvalidChar(ch, index)
81    }
82}