Skip to main content

moxy_token/lit/float/
f64.rs

1use super::LitFloat;
2use crate::lit::Lit;
3use crate::{Span, Spanner};
4
5#[derive(Debug, Clone)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(into = "String"))]
7pub struct LitF64 {
8    value: f64,
9    suffixed: bool,
10    repr: Box<str>,
11    span: Span,
12}
13
14impl LitF64 {
15    #[inline]
16    pub fn new(value: f64, suffixed: bool, span: Span) -> Self {
17        let repr = if suffixed {
18            format!("{value}f64").into_boxed_str()
19        } else {
20            format!("{value}").into_boxed_str()
21        };
22
23        Self {
24            value,
25            suffixed,
26            repr,
27            span,
28        }
29    }
30
31    #[inline]
32    pub(crate) fn from_parts(value: f64, suffixed: bool, repr: &str, span: Span) -> Self {
33        Self {
34            value,
35            suffixed,
36            repr: repr.into(),
37            span,
38        }
39    }
40
41    #[inline]
42    pub fn value(&self) -> f64 {
43        self.value
44    }
45
46    #[inline]
47    pub fn suffixed(&self) -> bool {
48        self.suffixed
49    }
50
51    #[inline]
52    pub fn repr(&self) -> &str {
53        &self.repr
54    }
55
56    #[inline]
57    pub fn span(&self) -> Span {
58        self.span
59    }
60
61    #[inline]
62    pub fn set_span(&mut self, span: Span) {
63        self.span = span;
64    }
65}
66
67impl PartialEq for LitF64 {
68    fn eq(&self, other: &Self) -> bool {
69        self.value == other.value && self.suffixed == other.suffixed
70    }
71}
72
73impl Eq for LitF64 {}
74
75impl std::hash::Hash for LitF64 {
76    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
77        self.repr.hash(state);
78    }
79}
80
81impl std::fmt::Display for LitF64 {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        f.write_str(&self.repr)
84    }
85}
86
87impl Spanner for LitF64 {
88    fn span(&self) -> Span {
89        self.span
90    }
91}
92
93impl From<LitF64> for LitFloat {
94    fn from(value: LitF64) -> Self {
95        Self::F64(value)
96    }
97}
98
99impl From<LitF64> for Lit {
100    fn from(value: LitF64) -> Self {
101        Self::Float(LitFloat::F64(value))
102    }
103}
104
105#[cfg(feature = "serde")]
106impl From<LitF64> for String {
107    fn from(value: LitF64) -> Self {
108        value.repr.into_string()
109    }
110}