error.rs 2.49 KB
Newer Older
1
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
Hero Bird's avatar
Hero Bird committed
2
//
3
4
5
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Hero Bird's avatar
Hero Bird committed
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
Hero Bird's avatar
Hero Bird committed
8
//
9
10
11
12
13
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Hero Bird's avatar
Hero Bird committed
14

15
16
17
18
/// A dispatch result.
pub type DispatchResult = core::result::Result<(), DispatchError>;

/// A dispatch error.
19
#[derive(Debug, Copy, Clone)]
20
21
22
23
24
25
26
27
28
29
pub enum DispatchError {
    UnknownSelector,
    UnknownInstantiateSelector,
    UnknownCallSelector,

    InvalidParameters,
    InvalidInstantiateParameters,
    InvalidCallParameters,

    CouldNotReadInput,
30
    PaidUnpayableMessage,
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
}

impl DispatchError {
    /// Converts `self` into an associated `u32` that SRML contracts can handle.
    #[inline]
    pub fn to_u32(self) -> u32 {
        DispatchRetCode::from(self).to_u32()
    }
}

/// A return code indicating success or error in a compact form.
#[derive(Copy, Clone)]
pub struct DispatchRetCode(u32);

impl DispatchRetCode {
    /// Creates a return code indicating success.
    #[inline]
    pub fn success() -> Self {
        Self(0)
    }

    /// Returns the `u32` representation of `self`.
    ///
    /// # Note
    ///
    /// This is useful to communicate back to SRML contracts.
    #[inline]
    pub fn to_u32(self) -> u32 {
        self.0
    }
}

impl From<DispatchError> for DispatchRetCode {
    #[inline]
    fn from(err: DispatchError) -> Self {
        match err {
            DispatchError::UnknownSelector => Self(0x01),
            DispatchError::UnknownInstantiateSelector => Self(0x02),
            DispatchError::UnknownCallSelector => Self(0x03),
            DispatchError::InvalidParameters => Self(0x04),
            DispatchError::InvalidInstantiateParameters => Self(0x05),
            DispatchError::InvalidCallParameters => Self(0x06),
            DispatchError::CouldNotReadInput => Self(0x07),
74
            DispatchError::PaidUnpayableMessage => Self(0x08),
75
        }
Hero Bird's avatar
Hero Bird committed
76
77
78
    }
}

79
80
81
82
83
84
impl From<DispatchResult> for DispatchRetCode {
    #[inline]
    fn from(res: DispatchResult) -> Self {
        match res {
            Ok(_) => Self::success(),
            Err(err) => Self::from(err),
Hero Bird's avatar
Hero Bird committed
85
86
87
        }
    }
}