Repository: jdrouet/mrml (language: rust)

Issue:
Replace string by Cow<'_, str> in style set
https://github.com/jdrouet/mrml/blob/main/packages%2Fmrml-core%2Fsrc%2Fprelude%2Frender.rs#L71


Discussion / hints:

Fix patch:
diff --git a/packages/mrml-core/src/mj_accordion/render.rs b/packages/mrml-core/src/mj_accordion/render.rs
index e471e170..0d1495f0 100644
--- a/packages/mrml-core/src/mj_accordion/render.rs
+++ b/packages/mrml-core/src/mj_accordion/render.rs
@@ -27,37 +27,30 @@ struct MjAccordionRender<'e, 'h> {
     raw_siblings: usize,
 }
 
-impl<'e, 'h> MjAccordionRender<'e, 'h> {
-    fn render_style(&self) -> String {
-        r#"
-        noinput.mj-accordion-checkbox { display: block! important; }
-
-        @media yahoo, only screen and (min-width:0) {
-          .mj-accordion-element { display:block; }
-          input.mj-accordion-checkbox, .mj-accordion-less { display: none !important; }
-          input.mj-accordion-checkbox+* .mj-accordion-title { cursor: pointer; touch-action: manipulation; -webkit-user-select: none; -moz-user-select: none; user-select: none; }
-          input.mj-accordion-checkbox+* .mj-accordion-content { overflow: hidden; display: none; }
-          input.mj-accordion-checkbox+* .mj-accordion-more { display: block !important; }
-          input.mj-accordion-checkbox:checked+* .mj-accordion-content { display: block; }
-          input.mj-accordion-checkbox:checked+* .mj-accordion-more { display: none !important; }
-          input.mj-accordion-checkbox:checked+* .mj-accordion-less { display: block !important; }
-        }
-
-        .moz-text-html input.mj-accordion-checkbox+* .mj-accordion-title { cursor: auto; touch-action: auto; -webkit-user-select: auto; -moz-user-select: auto; user-select: auto; }
-        .moz-text-html input.mj-accordion-checkbox+* .mj-accordion-content { overflow: hidden; display: block; }
-        .moz-text-html input.mj-accordion-checkbox+* .mj-accordion-ico { display: none; }
-
-        @goodbye { @gmail }
-        "#.to_string()
-    }
+const STYLE: &str = r#"noinput.mj-accordion-checkbox { display: block! important; }
+@media yahoo, only screen and (min-width:0) {
+  .mj-accordion-element { display:block; }
+  input.mj-accordion-checkbox, .mj-accordion-less { display: none !important; }
+  input.mj-accordion-checkbox+* .mj-accordion-title { cursor: pointer; touch-action: manipulation; -webkit-user-select: none; -moz-user-select: none; user-select: none; }
+  input.mj-accordion-checkbox+* .mj-accordion-content { overflow: hidden; display: none; }
+  input.mj-accordion-checkbox+* .mj-accordion-more { display: block !important; }
+  input.mj-accordion-checkbox:checked+* .mj-accordion-content { display: block; }
+  input.mj-accordion-checkbox:checked+* .mj-accordion-more { display: none !important; }
+  input.mj-accordion-checkbox:checked+* .mj-accordion-less { display: block !important; }
+}
+.moz-text-html input.mj-accordion-checkbox+* .mj-accordion-title { cursor: auto; touch-action: auto; -webkit-user-select: auto; -moz-user-select: auto; user-select: auto; }
+.moz-text-html input.mj-accordion-checkbox+* .mj-accordion-content { overflow: hidden; display: block; }
+.moz-text-html input.mj-accordion-checkbox+* .mj-accordion-ico { display: none; }
+@goodbye { @gmail }
+"#;
 
+impl<'e, 'h> MjAccordionRender<'e, 'h> {
     fn update_header(&self) {
-        let style = self.render_style();
         let font_families = self.attribute("font-family");
         self.header
             .borrow_mut()
             .maybe_add_font_families(font_families);
-        self.header.borrow_mut().add_style(style);
+        self.header.borrow_mut().add_style(STYLE);
     }
 }
 
diff --git a/packages/mrml-core/src/prelude/render.rs b/packages/mrml-core/src/prelude/render.rs
index 6084d818..4fbe3746 100644
--- a/packages/mrml-core/src/prelude/render.rs
+++ b/packages/mrml-core/src/prelude/render.rs
@@ -68,7 +68,7 @@ pub struct Header<'h> {
     font_families: Map<&'h str, &'h str>,
     used_font_families: Set<String>,
     media_queries: Map<String, Size>,
-    styles: Set<String>,
+    styles: Set<Cow<'static, str>>,
     lang: Option<String>,
     generator: AtomicU16,
 }
@@ -169,15 +169,15 @@ impl<'h> Header<'h> {
         self.media_queries.insert(classname, size);
     }
 
-    pub fn styles(&self) -> &Set<String> {
+    pub fn styles(&self) -> &Set<Cow<'static, str>> {
         &self.styles
     }
 
-    pub fn add_style(&mut self, value: String) {
-        self.styles.insert(value);
+    pub fn add_style<V: Into<Cow<'static, str>>>(&mut self, value: V) {
+        self.styles.insert(value.into());
     }
 
-    pub fn maybe_add_style(&mut self, value: Option<String>) {
+    pub fn maybe_add_style<V: Into<Cow<'static, str>>>(&mut self, value: Option<V>) {
         if let Some(value) = value {
             self.add_style(value);
         }


Test patch: