binn_ir/value_enum/impls/
numbers.rs

1/*
2==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
3
4Binn-IR
5
6Copyright (C) 2018-2023  Anonymous
7
8There are several releases over multiple years,
9they are listed as ranges, such as: "2018-2023".
10
11This program is free software: you can redistribute it and/or modify
12it under the terms of the GNU Lesser General Public License as published by
13the Free Software Foundation, either version 3 of the License, or
14(at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19GNU Lesser General Public License for more details.
20
21You should have received a copy of the GNU Lesser General Public License
22along with this program.  If not, see <https://www.gnu.org/licenses/>.
23
24::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
25*/
26
27//! # Numbers
28
29use crate::{Error, Value};
30
31macro_rules! impl_from_numbers_for_value { ($($number: ty, $variant: tt,)+) => {
32    $(
33        impl From<&$number> for Value {
34
35            fn from(n: &$number) -> Self {
36                Value::$variant(*n)
37            }
38
39        }
40
41        impl From<$number> for Value {
42
43            fn from(n: $number) -> Self {
44                Self::from(&n)
45            }
46
47        }
48    )+
49}}
50
51impl_from_numbers_for_value!{
52    i8, I8, i16, I16, i32, I32, i64, I64,
53    u8, U8, u16, U16, u32, U32, u64, U64,
54    f32, Float, f64, Double,
55}
56
57macro_rules! impl_try_from_value_for_integers { ($($ty: ty,)+) => {
58    $(
59        impl TryFrom<&Value> for $ty {
60
61            type Error = Error;
62
63            fn try_from(v: &Value) -> core::result::Result<Self, Self::Error> {
64                match v {
65                    Value::I8(i) => Self::try_from(*i).map_err(|e| err!("{}", e)),
66                    Value::U8(u) => Self::try_from(*u).map_err(|e| err!("{}", e)),
67                    Value::I16(i) => Self::try_from(*i).map_err(|e| err!("{}", e)),
68                    Value::U16(u) => Self::try_from(*u).map_err(|e| err!("{}", e)),
69                    Value::I32(i) => Self::try_from(*i).map_err(|e| err!("{}", e)),
70                    Value::U32(u) => Self::try_from(*u).map_err(|e| err!("{}", e)),
71                    Value::I64(i) => Self::try_from(*i).map_err(|e| err!("{}", e)),
72                    Value::U64(u) => Self::try_from(*u).map_err(|e| err!("{}", e)),
73                    _ => Err(err!("Value is not an integer")),
74                }
75            }
76
77        }
78
79        impl TryFrom<Value> for $ty {
80
81            type Error = Error;
82
83            fn try_from(v: Value) -> core::result::Result<Self, Self::Error> {
84                Self::try_from(&v)
85            }
86
87        }
88    )+
89}}
90
91impl_try_from_value_for_integers! {
92    i8, i16, i32, i64,
93    u8, u16, u32, u64,
94}
95
96impl TryFrom<&Value> for f32 {
97
98    type Error = Error;
99
100    fn try_from(v: &Value) -> core::result::Result<Self, Self::Error> {
101        match v {
102            Value::I8(i) => Ok(Self::from(*i)),
103            Value::U8(u) => Ok(Self::from(*u)),
104            Value::I16(i) => Ok(Self::from(*i)),
105            Value::U16(u) => Ok(Self::from(*u)),
106            Value::Float(f) => Ok(*f),
107            _ => Err(err!("Cannot convert this value to f32")),
108        }
109    }
110
111}
112
113impl TryFrom<Value> for f32 {
114
115    type Error = Error;
116
117    fn try_from(v: Value) -> core::result::Result<Self, Self::Error> {
118        Self::try_from(&v)
119    }
120
121}
122
123impl TryFrom<&Value> for f64 {
124
125    type Error = Error;
126
127    fn try_from(v: &Value) -> core::result::Result<Self, Self::Error> {
128        match v {
129            Value::I8(i) => Ok(Self::from(*i)),
130            Value::U8(u) => Ok(Self::from(*u)),
131            Value::I16(i) => Ok(Self::from(*i)),
132            Value::U16(u) => Ok(Self::from(*u)),
133            Value::I32(i) => Ok(Self::from(*i)),
134            Value::U32(u) => Ok(Self::from(*u)),
135            Value::Float(f) => Ok(Self::from(*f)),
136            Value::Double(d) => Ok(*d),
137            _ => Err(err!("Cannot convert this value to f64")),
138        }
139    }
140
141}
142
143impl TryFrom<Value> for f64 {
144
145    type Error = Error;
146
147    fn try_from(v: Value) -> core::result::Result<Self, Self::Error> {
148        Self::try_from(&v)
149    }
150
151}