Skip to main content

moxy_token/lit/float/
f32.rs

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