stylex_types/structures/
meta_data.rs1use std::hash::{Hash, Hasher};
2
3use serde::{Deserialize, Serialize, Serializer};
4
5use crate::{
6 enums::data_structures::injectable_style::{InjectableStyleBaseKind, InjectableStyleKind},
7 traits::InjectableStylesMap,
8};
9use stylex_utils::hash::hash_f64;
10
11fn f64_to_int<S>(priority: &f64, serializer: S) -> Result<S::Ok, S::Error>
12where
13 S: Serializer,
14{
15 if priority.fract() == 0.0 {
16 return serializer.serialize_i32(*priority as i32);
17 }
18
19 serializer.serialize_f64(*priority)
20}
21
22#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
23
24pub struct MetaData {
25 class_name: String,
26 style: InjectableStyleBaseKind,
27 #[serde(serialize_with = "f64_to_int")]
28 priority: f64,
29}
30
31impl Hash for MetaData {
32 fn hash<H: Hasher>(&self, state: &mut H) {
33 self.class_name.hash(state);
34 self.style.hash(state);
35 hash_f64(self.priority);
36 }
37}
38
39impl Eq for MetaData {}
40
41impl MetaData {
42 pub fn new(class_name: String, injectable_style: InjectableStyleKind) -> Self {
43 Self {
44 class_name,
45 priority: match &injectable_style {
46 InjectableStyleKind::Regular(style) => style.priority.unwrap_or(0.0),
47 InjectableStyleKind::Const(style) => style.priority.unwrap_or(0.0),
48 },
49 style: InjectableStyleBaseKind::from(injectable_style),
50 }
51 }
52 pub fn get_style(&self) -> &InjectableStyleBaseKind {
53 &self.style
54 }
55
56 pub fn get_css(&self) -> &str {
57 match &self.style {
58 InjectableStyleBaseKind::Regular(style) => style.ltr.as_str(),
59 InjectableStyleBaseKind::Const(style) => style.ltr.as_str(),
60 }
61 }
62
63 pub fn get_const_key(&self) -> Option<&str> {
64 match &self.style {
65 InjectableStyleBaseKind::Const(style) => Some(style.const_key.as_str()),
66 _ => None,
67 }
68 }
69 pub fn get_const_value(&self) -> Option<&str> {
70 match &self.style {
71 InjectableStyleBaseKind::Const(style) => Some(style.const_value.as_str()),
72 _ => None,
73 }
74 }
75
76 pub fn get_css_rtl(&self) -> Option<&str> {
77 match &self.style {
78 InjectableStyleBaseKind::Regular(style) => style.rtl.as_deref(),
79 InjectableStyleBaseKind::Const(style) => style.rtl.as_deref(),
80 }
81 }
82
83 pub fn get_class_name(&self) -> &str {
84 self.class_name.as_str()
85 }
86
87 pub fn get_priority(&self) -> &f64 {
88 &self.priority
89 }
90
91 pub fn convert_from_injected_styles_map(
92 injected_styles_map: &InjectableStylesMap,
93 ) -> Vec<MetaData> {
94 injected_styles_map
95 .iter()
96 .map(|(class_name, injectable_style)| {
97 MetaData::new(class_name.clone(), injectable_style.as_ref().clone())
98 })
99 .collect()
100 }
101}