1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//  Copyright (C) 2018  The Durs Project Developers.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Provide base convertion tools

/// Base16 conversion tools
pub mod b16;

/// Base58 conversion tools
pub mod b58;

/// Base64 conversion tools
pub mod b64;

/// Errors enumeration for Base58/64 strings convertion.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Fail)]
pub enum BaseConvertionError {
    #[fail(
        display = "Data have invalid key length : expected {}, found {}.",
        expected, found
    )]
    /// Data have invalid length.
    InvalidLength {
        /// Expected length
        expected: usize,
        /// Actual length
        found: usize,
    },
    #[fail(display = "Invalid character '{}' at offset {}.", character, offset)]
    /// Base58/64 have an invalid character.
    InvalidCharacter {
        /// Character
        character: char,
        /// Offset (=position)
        offset: usize,
    },
    #[fail(display = "Invalid base converter length.")]
    /// Base58/64 have invalid lendth
    InvalidBaseConverterLength,
    #[fail(display = "Invalid last symbol '{}' at offset {}.", symbol, offset)]
    /// Base64 have invalid last symbol (symbol, offset)
    InvalidLastSymbol {
        /// Symbol
        symbol: u8,
        /// Offset (=position)
        offset: usize,
    },
}

impl From<base64::DecodeError> for BaseConvertionError {
    fn from(err: base64::DecodeError) -> Self {
        match err {
            base64::DecodeError::InvalidByte(offset, byte) => {
                BaseConvertionError::InvalidCharacter {
                    character: byte as char,
                    offset,
                }
            }
            base64::DecodeError::InvalidLength => BaseConvertionError::InvalidBaseConverterLength,
            base64::DecodeError::InvalidLastSymbol(offset, symbol) => {
                BaseConvertionError::InvalidLastSymbol { symbol, offset }
            }
        }
    }
}