moxy_token/lit/float/
f32.rs1use 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 LitF32 {
8 value: f32,
9 suffixed: bool,
10 repr: Box<str>,
11 span: Span,
12}
13
14impl LitF32 {
15 #[inline]
16 pub fn new(value: f32, suffixed: bool, span: Span) -> Self {
17 let repr = if suffixed {
18 format!("{value}f32").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: f32, 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) -> f32 {
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 LitF32 {
68 fn eq(&self, other: &Self) -> bool {
69 self.value == other.value && self.suffixed == other.suffixed
70 }
71}
72
73impl Eq for LitF32 {}
74
75impl std::hash::Hash for LitF32 {
76 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
77 self.repr.hash(state);
78 }
79}
80
81impl std::fmt::Display for LitF32 {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 f.write_str(&self.repr)
84 }
85}
86
87impl Spanner for LitF32 {
88 fn span(&self) -> Span {
89 self.span
90 }
91}
92
93impl From<LitF32> for LitFloat {
94 fn from(value: LitF32) -> Self {
95 Self::F32(value)
96 }
97}
98
99impl From<LitF32> for Lit {
100 fn from(value: LitF32) -> Self {
101 Self::Float(LitFloat::F32(value))
102 }
103}
104
105#[cfg(feature = "serde")]
106impl From<LitF32> for String {
107 fn from(value: LitF32) -> Self {
108 value.repr.into_string()
109 }
110}