error.rs 1.84 KB
Newer Older
1
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
2
// This file is part of ink!.
3
//
4
// ink! is free software: you can redistribute it and/or modify
5
6
7
8
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// ink! is distributed in the hope that it will be useful,
10
11
12
13
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
15
// along with ink!.  If not, see <http://www.gnu.org/licenses/>.
16

17
18
use std::{
    io::Error as IoError,
Robin Freyler's avatar
Robin Freyler committed
19
    result::Result as StdResult,
20
};
21
use zip::result::ZipError;
22
23
24
25
26
27
28

/// The kinds of command errors.
#[derive(Debug)]
pub enum CommandErrorKind {
    Io(IoError),
    UnimplementedCommand,
    UnimplementedAbstractionLayer,
29
    ZipError(ZipError),
30
    Other(String),
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
}

/// An error that can be encountered while executing commands.
#[derive(Debug)]
pub struct CommandError {
    kind: CommandErrorKind,
}

impl From<IoError> for CommandError {
    fn from(error: IoError) -> Self {
        Self {
            kind: CommandErrorKind::Io(error),
        }
    }
}

47
48
49
50
51
52
53
54
impl From<ZipError> for CommandError {
    fn from(error: ZipError) -> Self {
        Self {
            kind: CommandErrorKind::ZipError(error),
        }
    }
}

55
56
57
58
59
60
61
62
impl From<&str> for CommandError {
    fn from(error: &str) -> Self {
        Self {
            kind: CommandErrorKind::Other(error.to_string()),
        }
    }
}

63
64
65
66
67
68
69
70
71
impl CommandError {
    /// Creates a new command error from the given kind.
    pub fn new(kind: CommandErrorKind) -> Self {
        Self { kind }
    }
}

/// Result type that has a `CommandError`.
pub type Result<T> = StdResult<T, CommandError>;