multiversx_sc/types/managed/basic/
big_int_sign.rs

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use crate::codec::{
    DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedDecodeInput, NestedEncode,
    NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput,
};

use crate::abi::{TypeAbi, TypeAbiFrom, TypeName};

// BigInt sign.
#[allow(clippy::enum_variant_names)]
#[derive(PartialEq, Eq, Debug)]
pub enum Sign {
    Minus,
    NoSign,
    Plus,
}

impl Sign {
    #[inline]
    pub fn is_minus(&self) -> bool {
        matches!(self, Sign::Minus)
    }

    fn to_int(&self) -> i8 {
        match self {
            Sign::Plus => 1,
            Sign::NoSign => 0,
            Sign::Minus => -1,
        }
    }

    fn from_int(value: i8) -> Self {
        match value.cmp(&0) {
            core::cmp::Ordering::Greater => Sign::Plus,
            core::cmp::Ordering::Equal => Sign::NoSign,
            core::cmp::Ordering::Less => Sign::Minus,
        }
    }
}

impl TopEncode for Sign {
    #[inline]
    fn top_encode_or_handle_err<O, H>(&self, output: O, h: H) -> Result<(), H::HandledErr>
    where
        O: TopEncodeOutput,
        H: EncodeErrorHandler,
    {
        self.to_int().top_encode_or_handle_err(output, h)
    }
}

impl NestedEncode for Sign {
    fn dep_encode_or_handle_err<O, H>(&self, dest: &mut O, h: H) -> Result<(), H::HandledErr>
    where
        O: NestedEncodeOutput,
        H: EncodeErrorHandler,
    {
        self.to_int().dep_encode_or_handle_err(dest, h)
    }
}

impl NestedDecode for Sign {
    fn dep_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
    where
        I: NestedDecodeInput,
        H: DecodeErrorHandler,
    {
        Ok(Sign::from_int(i8::dep_decode_or_handle_err(input, h)?))
    }
}

impl TopDecode for Sign {
    #[inline]
    fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
    where
        I: TopDecodeInput,
        H: DecodeErrorHandler,
    {
        Ok(Sign::from_int(i8::top_decode_or_handle_err(input, h)?))
    }
}

impl TypeAbiFrom<Self> for Sign {}

impl TypeAbi for Sign {
    type Unmanaged = Self;

    fn type_name() -> TypeName {
        TypeName::from("Sign")
    }

    fn type_name_rust() -> TypeName {
        TypeName::from("multiversx_sc::types::Sign")
    }
}