Skip to main content

stylex_types/structures/
injectable_style.rs

1use std::{hash::Hash, rc::Rc};
2
3use serde::{Deserialize, Serialize};
4
5use crate::enums::data_structures::injectable_style::InjectableStyleKind;
6use stylex_utils::hash::hash_f64;
7
8#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Hash, Eq)]
9pub struct InjectableStyleBase {
10  pub rtl: Option<String>,
11  pub ltr: String,
12}
13
14#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Hash, Eq)]
15pub struct InjectableStyleConstBase {
16  pub rtl: Option<String>,
17  pub ltr: String,
18  pub const_key: String,
19  pub const_value: String,
20}
21
22#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
23pub struct InjectableStyle {
24  pub ltr: String,
25  pub rtl: Option<String>,
26  pub priority: Option<f64>,
27}
28
29#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
30pub struct InjectableConstStyle {
31  pub ltr: String,
32  pub rtl: Option<String>,
33  pub priority: Option<f64>,
34  pub const_key: String,
35  pub const_value: String,
36}
37
38impl Hash for InjectableStyle {
39  fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
40    self.ltr.hash(state);
41    self.rtl.hash(state);
42    hash_f64(self.priority.unwrap_or(0.0));
43  }
44}
45
46impl From<InjectableStyle> for InjectableStyleBase {
47  fn from(style: InjectableStyle) -> Self {
48    // Assuming InjectableStyleBase and InjectableStyle have similar fields
49    InjectableStyleBase {
50      ltr: style.ltr,
51      rtl: style.rtl,
52    }
53  }
54}
55
56impl InjectableStyle {
57  /// Creates a new InjectableStyle wrapped in Rc<InjectableStyleKind> with only LTR content.
58  ///
59  /// # Example
60  /// ```ignore
61  /// let style = InjectableStyle::regular(css_string, Some(0.5));
62  /// ```
63  #[inline]
64  pub fn regular(ltr: String, priority: Option<f64>) -> Rc<InjectableStyleKind> {
65    Rc::new(InjectableStyleKind::Regular(InjectableStyle {
66      ltr,
67      rtl: None,
68      priority,
69    }))
70  }
71
72  /// Creates a new InjectableStyle wrapped in Rc<InjectableStyleKind> with both LTR and RTL content.
73  ///
74  /// # Example
75  /// ```ignore
76  /// let style = InjectableStyle::with_rtl(ltr_css, rtl_css, Some(0.5));
77  /// ```
78  #[inline]
79  pub fn with_rtl(ltr: String, rtl: String, priority: Option<f64>) -> Rc<InjectableStyleKind> {
80    Rc::new(InjectableStyleKind::Regular(InjectableStyle {
81      ltr,
82      rtl: Some(rtl),
83      priority,
84    }))
85  }
86}
87
88impl Default for InjectableStyle {
89  fn default() -> Self {
90    InjectableStyle {
91      ltr: "".to_string(),
92      rtl: None,
93      priority: Some(0.0),
94    }
95  }
96}
97
98impl From<InjectableConstStyle> for InjectableStyleConstBase {
99  fn from(style: InjectableConstStyle) -> Self {
100    InjectableStyleConstBase {
101      ltr: style.ltr,
102      rtl: style.rtl,
103      const_key: style.const_key,
104      const_value: style.const_value,
105    }
106  }
107}
108impl Default for InjectableConstStyle {
109  fn default() -> Self {
110    InjectableConstStyle {
111      ltr: "".to_string(),
112      rtl: None,
113      priority: Some(0.0),
114      const_key: "".to_string(),
115      const_value: "".to_string(),
116    }
117  }
118}