activitystreams/primitives/xsd_float.rs
1/*
2 * This file is part of ActivityStreams.
3 *
4 * Copyright © 2020 Riley Trautman
5 *
6 * ActivityStreams is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ActivityStreams is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with ActivityStreams. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/// The type xsd:float represents an IEEE single-precision 32-bit floating-point number.
21///
22/// TODO: handle exponents, infinity, not-a-number
23///
24/// The format of xsd:float values is a mantissa (a number which conforms to the type decimal)
25/// followed, optionally, by the character "E" or "e" followed by an exponent. The exponent must be
26/// an integer. For example, 3E2 represents 3 times 10 to the 2nd power, or 300. The exponent must
27/// be an integer.
28///
29/// In addition, the following values are valid: INF (infinity), -INF (negative infinity), and NaN
30/// (Not a Number). INF is considered to be greater than all other values, while -INF is less than
31/// all other values. The value NaN cannot be compared to any other values, although it equals
32/// itself.
33#[derive(Clone, Debug, Default, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize)]
34#[serde(transparent)]
35pub struct XsdFloat(f64);
36
37/// The error type produced when an XsdFloat cannot be parsed
38#[derive(Clone, Debug, thiserror::Error)]
39#[error("Error parsing Float")]
40pub struct XsdFloatError;
41
42impl XsdFloat {
43 /// Get an f64 from the XsdFloat
44 pub fn to_f64(&self) -> f64 {
45 self.0
46 }
47
48 /// Get an XsdFloat from an f64
49 pub fn from_f64(f: f64) -> Self {
50 f.into()
51 }
52}
53
54impl AsRef<f64> for XsdFloat {
55 fn as_ref(&self) -> &f64 {
56 &self.0
57 }
58}
59
60impl AsMut<f64> for XsdFloat {
61 fn as_mut(&mut self) -> &mut f64 {
62 &mut self.0
63 }
64}
65
66impl From<f64> for XsdFloat {
67 fn from(f: f64) -> Self {
68 XsdFloat(f)
69 }
70}
71
72impl From<XsdFloat> for f64 {
73 fn from(f: XsdFloat) -> Self {
74 f.0
75 }
76}
77
78impl std::convert::TryFrom<String> for XsdFloat {
79 type Error = XsdFloatError;
80
81 fn try_from(s: String) -> Result<Self, Self::Error> {
82 s.parse()
83 }
84}
85
86impl std::convert::TryFrom<&str> for XsdFloat {
87 type Error = XsdFloatError;
88
89 fn try_from(s: &str) -> Result<Self, Self::Error> {
90 s.parse()
91 }
92}
93
94impl std::convert::TryFrom<&mut str> for XsdFloat {
95 type Error = XsdFloatError;
96
97 fn try_from(s: &mut str) -> Result<Self, Self::Error> {
98 s.parse()
99 }
100}
101
102impl std::str::FromStr for XsdFloat {
103 type Err = XsdFloatError;
104
105 fn from_str(s: &str) -> Result<Self, Self::Err> {
106 Ok(XsdFloat(s.parse().map_err(|_| XsdFloatError)?))
107 }
108}
109
110impl std::fmt::Display for XsdFloat {
111 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
112 std::fmt::Display::fmt(&self.0, f)
113 }
114}