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
148
149
150
151
/*
==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--

Binn-IR

Copyright (C) 2018-2023  Anonymous

There are several releases over multiple years,
they are listed as ranges, such as: "2018-2023".

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
*/

//! # Numbers

use crate::{Error, Value};

macro_rules! impl_from_numbers_for_value { ($($number: ty, $variant: tt,)+) => {
    $(
        impl From<&$number> for Value {

            fn from(n: &$number) -> Self {
                Value::$variant(*n)
            }

        }

        impl From<$number> for Value {

            fn from(n: $number) -> Self {
                Self::from(&n)
            }

        }
    )+
}}

impl_from_numbers_for_value!{
    i8, I8, i16, I16, i32, I32, i64, I64,
    u8, U8, u16, U16, u32, U32, u64, U64,
    f32, Float, f64, Double,
}

macro_rules! impl_try_from_value_for_integers { ($($ty: ty,)+) => {
    $(
        impl TryFrom<&Value> for $ty {

            type Error = Error;

            fn try_from(v: &Value) -> core::result::Result<Self, Self::Error> {
                match v {
                    Value::I8(i) => Self::try_from(*i).map_err(|e| err!("{}", e)),
                    Value::U8(u) => Self::try_from(*u).map_err(|e| err!("{}", e)),
                    Value::I16(i) => Self::try_from(*i).map_err(|e| err!("{}", e)),
                    Value::U16(u) => Self::try_from(*u).map_err(|e| err!("{}", e)),
                    Value::I32(i) => Self::try_from(*i).map_err(|e| err!("{}", e)),
                    Value::U32(u) => Self::try_from(*u).map_err(|e| err!("{}", e)),
                    Value::I64(i) => Self::try_from(*i).map_err(|e| err!("{}", e)),
                    Value::U64(u) => Self::try_from(*u).map_err(|e| err!("{}", e)),
                    _ => Err(err!("Value is not an integer")),
                }
            }

        }

        impl TryFrom<Value> for $ty {

            type Error = Error;

            fn try_from(v: Value) -> core::result::Result<Self, Self::Error> {
                Self::try_from(&v)
            }

        }
    )+
}}

impl_try_from_value_for_integers! {
    i8, i16, i32, i64,
    u8, u16, u32, u64,
}

impl TryFrom<&Value> for f32 {

    type Error = Error;

    fn try_from(v: &Value) -> core::result::Result<Self, Self::Error> {
        match v {
            Value::I8(i) => Ok(Self::from(*i)),
            Value::U8(u) => Ok(Self::from(*u)),
            Value::I16(i) => Ok(Self::from(*i)),
            Value::U16(u) => Ok(Self::from(*u)),
            Value::Float(f) => Ok(*f),
            _ => Err(err!("Cannot convert this value to f32")),
        }
    }

}

impl TryFrom<Value> for f32 {

    type Error = Error;

    fn try_from(v: Value) -> core::result::Result<Self, Self::Error> {
        Self::try_from(&v)
    }

}

impl TryFrom<&Value> for f64 {

    type Error = Error;

    fn try_from(v: &Value) -> core::result::Result<Self, Self::Error> {
        match v {
            Value::I8(i) => Ok(Self::from(*i)),
            Value::U8(u) => Ok(Self::from(*u)),
            Value::I16(i) => Ok(Self::from(*i)),
            Value::U16(u) => Ok(Self::from(*u)),
            Value::I32(i) => Ok(Self::from(*i)),
            Value::U32(u) => Ok(Self::from(*u)),
            Value::Float(f) => Ok(Self::from(*f)),
            Value::Double(d) => Ok(*d),
            _ => Err(err!("Cannot convert this value to f64")),
        }
    }

}

impl TryFrom<Value> for f64 {

    type Error = Error;

    fn try_from(v: Value) -> core::result::Result<Self, Self::Error> {
        Self::try_from(&v)
    }

}