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
use core::fmt;
use core::str::FromStr;

use super::IntegerOrFloat::{self, *};
use crate::{f_iof, i_iof};

#[cfg(std)]
impl From<IntegerOrFloat> for String {
    fn from(iof: IntegerOrFloat) -> Self {
        match iof {
            IntegerOrFloat::Float(f) => f.to_string(),
            IntegerOrFloat::Integer(i) => i.to_string(),
        }
    }
}

// this implements ToString iff feature(alloc), which has:
// impl<T> ToString for T where T: std::fmt::Display, T: ?Sized { … };
impl fmt::Display for IntegerOrFloat {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            IntegerOrFloat::Float(f) => f.fmt(formatter),
            IntegerOrFloat::Integer(i) => i.fmt(formatter),
        }
    }
}

impl fmt::Debug for IntegerOrFloat {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            IntegerOrFloat::Float(f) => formatter.write_fmt(format_args!("Float({})", f)),
            IntegerOrFloat::Integer(i) => formatter.write_fmt(format_args!("Integer({})", i)),
        }
    }
}

#[cfg(std)]
use std::error::Error;
#[cfg(no_std)]
trait Error {}

#[derive(Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ConversionError {
    IntegerConversionError,
    FloatConversionError,
}

use ConversionError::*;
impl fmt::Display for ConversionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IntegerConversionError => write!(f, "String not an integer"),
            FloatConversionError => write!(f, "String not a floating point number"),
        }
    }
}

impl Error for ConversionError {}

use core::convert::TryFrom;
impl TryFrom<&str> for IntegerOrFloat {
    type Error = ConversionError;
    fn try_from(s: &str) -> Result<Self, Self::Error> {
        if let Ok(i) = s.parse::<i_iof>() {
            Ok(Integer(i))
        } else {
            if let Ok(f) = s.parse::<f_iof>() {
                Ok(Float(f))
            } else {
                if !s.contains('.') {
                    Err(IntegerConversionError)
                } else {
                    Err(FloatConversionError)
                }
            }
        }
    }
}

#[cfg(feature = "num-traits")]
impl From<num_traits::ParseFloatError> for ConversionError {
    fn from(_: num_traits::ParseFloatError) -> Self {
        ConversionError::FloatConversionError
    }
}

impl From<core::num::ParseIntError> for ConversionError {
    fn from(_: core::num::ParseIntError) -> Self {
        ConversionError::IntegerConversionError
    }
}

#[cfg(feature = "num-traits")]
impl num_traits::Num for IntegerOrFloat {
    type FromStrRadixErr = ConversionError;
    fn from_str_radix(s: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
        if s.contains('.') {
            Ok(Integer(i_iof::from_str_radix(s, radix)?))
        } else {
            Ok(Float(f_iof::from_str_radix(s, radix)?))
        }
    }
}

impl FromStr for IntegerOrFloat {
    type Err = ConversionError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::try_from(s)
    }
}