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
95
96
// Copyright 2021 Axiom-Team
//
// This file is part of Duniter-v2S.
//
// Duniter-v2S 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, version 3 of the License.
//
// Duniter-v2S 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 Duniter-v2S. If not, see <https://www.gnu.org/licenses/>.

use codec::{Decode, Encode, Error, Input, MaxEncodedLen, Output};
use core::num::NonZeroU16;
use sp_runtime::RuntimeDebug;
use sp_std::vec::Vec;

pub type UdIndex = u16;

#[derive(
    Clone, Copy, Default, Eq, PartialEq, RuntimeDebug, serde::Deserialize, serde::Serialize,
)]
pub struct FirstEligibleUd(pub Option<NonZeroU16>);

impl FirstEligibleUd {
    pub fn min() -> Self {
        Self(Some(NonZeroU16::new(1).expect("unreachable")))
    }
}

impl From<UdIndex> for FirstEligibleUd {
    fn from(ud_index: UdIndex) -> Self {
        FirstEligibleUd(NonZeroU16::new(ud_index))
    }
}

impl From<FirstEligibleUd> for Option<UdIndex> {
    fn from(first_eligible_ud: FirstEligibleUd) -> Self {
        first_eligible_ud.0.map(|ud_index| ud_index.get())
    }
}

impl Encode for FirstEligibleUd {
    fn size_hint(&self) -> usize {
        self.as_u16().size_hint()
    }

    fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
        self.as_u16().encode_to(dest)
    }

    fn encode(&self) -> Vec<u8> {
        self.as_u16().encode()
    }

    fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
        self.as_u16().using_encoded(f)
    }
}

impl codec::EncodeLike for FirstEligibleUd {}

impl Decode for FirstEligibleUd {
    fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
        Ok(match NonZeroU16::new(Decode::decode(input)?) {
            Some(non_zero_u16) => Self(Some(non_zero_u16)),
            None => Self(None),
        })
    }
}

impl MaxEncodedLen for FirstEligibleUd {
    fn max_encoded_len() -> usize {
        u16::max_encoded_len()
    }
}

impl scale_info::TypeInfo for FirstEligibleUd {
    type Identity = UdIndex;

    fn type_info() -> scale_info::Type {
        Self::Identity::type_info()
    }
}

impl FirstEligibleUd {
    // private
    #[inline(always)]
    fn as_u16(&self) -> UdIndex {
        self.0.map(|ud_index| ud_index.get()).unwrap_or_default()
    }
}