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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::{error::ConvertError, value::number::Number};

pub trait FromNumber<I, F>: Sized
where
    I: num::ToPrimitive,
    F: num::ToPrimitive,
{
    type Err;
    fn from_number(number: &Number<I, F>) -> Result<Self, Self::Err>;
}

pub enum IntegerConverter {}
pub enum FloatConverter {}

pub trait Converter<I, F>
where
    I: num::ToPrimitive,
    F: num::ToPrimitive,
{
    type Err;
    fn convert<N: Converted<I, F>>(n: &Number<I, F>) -> Result<N, Self::Err>;
}

impl<I, F> Converter<I, F> for IntegerConverter
where
    I: num::ToPrimitive,
    F: num::ToPrimitive,
{
    type Err = crate::Error;
    fn convert<N: Converted<I, F>>(n: &Number<I, F>) -> Result<N, Self::Err> {
        match n {
            Number::Integer(i) => Ok(Converted::<I, F>::to_self(i).ok_or(ConvertError::InvalidIntegerConvert)?),
            Number::Float(_) => Err(ConvertError::CannotConvertFloatToInteger)?,
        }
    }
}

impl<I, F> Converter<I, F> for FloatConverter
where
    I: num::ToPrimitive,
    F: num::ToPrimitive,
{
    type Err = crate::Error;
    fn convert<N: Converted<I, F>>(n: &Number<I, F>) -> Result<N, Self::Err> {
        match n {
            Number::Integer(_) => Err(ConvertError::CannotConvertIntegerToFloat)?,
            Number::Float(f) => Ok(Converted::<I, F>::to_self(f).ok_or(ConvertError::InvalidIntegerConvert)?),
        }
    }
}

pub trait Converted<I, F>: Sized
where
    I: num::ToPrimitive,
    F: num::ToPrimitive,
{
    type Converter: Converter<I, F>;
    fn to_self<P: num::ToPrimitive>(p: &P) -> Option<Self>;
    fn converted(n: &Number<I, F>) -> Result<Self, <Self::Converter as Converter<I, F>>::Err> {
        Self::Converter::convert(n)
    }
}

impl<N, I, F> FromNumber<I, F> for N
where
    N: Converted<I, F>,
    I: num::ToPrimitive,
    F: num::ToPrimitive,
{
    type Err = <<N as Converted<I, F>>::Converter as Converter<I, F>>::Err;
    fn from_number(number: &Number<I, F>) -> Result<Self, Self::Err> {
        Self::converted(number)
    }
}

impl<I: num::ToPrimitive, F: num::ToPrimitive> Converted<I, F> for u8 {
    type Converter = IntegerConverter;
    fn to_self<P: num::ToPrimitive>(p: &P) -> Option<Self> {
        p.to_u8()
    }
}
impl<I: num::ToPrimitive, F: num::ToPrimitive> Converted<I, F> for u16 {
    type Converter = IntegerConverter;
    fn to_self<P: num::ToPrimitive>(p: &P) -> Option<Self> {
        p.to_u16()
    }
}
impl<I: num::ToPrimitive, F: num::ToPrimitive> Converted<I, F> for u32 {
    type Converter = IntegerConverter;
    fn to_self<P: num::ToPrimitive>(p: &P) -> Option<Self> {
        p.to_u32()
    }
}
impl<I: num::ToPrimitive, F: num::ToPrimitive> Converted<I, F> for u64 {
    type Converter = IntegerConverter;
    fn to_self<P: num::ToPrimitive>(p: &P) -> Option<Self> {
        p.to_u64()
    }
}
impl<I: num::ToPrimitive, F: num::ToPrimitive> Converted<I, F> for u128 {
    type Converter = IntegerConverter;
    fn to_self<P: num::ToPrimitive>(p: &P) -> Option<Self> {
        p.to_u128()
    }
}
impl<I: num::ToPrimitive, F: num::ToPrimitive> Converted<I, F> for i8 {
    type Converter = IntegerConverter;
    fn to_self<P: num::ToPrimitive>(p: &P) -> Option<Self> {
        p.to_i8()
    }
}
impl<I: num::ToPrimitive, F: num::ToPrimitive> Converted<I, F> for i16 {
    type Converter = IntegerConverter;
    fn to_self<P: num::ToPrimitive>(p: &P) -> Option<Self> {
        p.to_i16()
    }
}
impl<I: num::ToPrimitive, F: num::ToPrimitive> Converted<I, F> for i32 {
    type Converter = IntegerConverter;
    fn to_self<P: num::ToPrimitive>(p: &P) -> Option<Self> {
        p.to_i32()
    }
}
impl<I: num::ToPrimitive, F: num::ToPrimitive> Converted<I, F> for i64 {
    type Converter = IntegerConverter;
    fn to_self<P: num::ToPrimitive>(p: &P) -> Option<Self> {
        p.to_i64()
    }
}
impl<I: num::ToPrimitive, F: num::ToPrimitive> Converted<I, F> for i128 {
    type Converter = IntegerConverter;
    fn to_self<P: num::ToPrimitive>(p: &P) -> Option<Self> {
        p.to_i128()
    }
}
impl<I: num::ToPrimitive, F: num::ToPrimitive> Converted<I, F> for f32 {
    type Converter = FloatConverter;
    fn to_self<P: num::ToPrimitive>(p: &P) -> Option<Self> {
        p.to_f32()
    }
}
impl<I: num::ToPrimitive, F: num::ToPrimitive> Converted<I, F> for f64 {
    type Converter = FloatConverter;
    fn to_self<P: num::ToPrimitive>(p: &P) -> Option<Self> {
        p.to_f64()
    }
}