[librsvg: 1/3] Selector: turn into a more concrete type



commit c606f4f2628f772a353d202947c23c60dc4d6741
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon May 6 16:18:44 2019 -0500

    Selector: turn into a more concrete type
    
    This is a newtype over a String for now.  We'll eventually turn this
    into a smarter representation of selectors.

 rsvg_internals/src/css.rs | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
---
diff --git a/rsvg_internals/src/css.rs b/rsvg_internals/src/css.rs
index 68d9c1a8..fb7f41d9 100644
--- a/rsvg_internals/src/css.rs
+++ b/rsvg_internals/src/css.rs
@@ -67,7 +67,14 @@ impl<'i> AtRuleParser<'i> for DeclParser {
     type Error = ValueErrorKind;
 }
 
-type Selector = String;
+#[derive(Hash, PartialEq, Eq)]
+struct Selector(String);
+
+impl Selector {
+    fn new(s: &str) -> Selector {
+        Selector(s.to_string())
+    }
+}
 
 /// Contains all the mappings of selectors to style declarations
 /// that result from loading an SVG document.
@@ -186,17 +193,17 @@ impl CssRules {
             })
     }
 
-    fn add_declaration(&mut self, selector: &str, declaration: Declaration) {
+    fn add_declaration(&mut self, selector: Selector, declaration: Declaration) {
         let decl_list = self
             .selectors_to_declarations
-            .entry(selector.to_string())
+            .entry(selector)
             .or_insert_with(|| DeclarationList::new());
 
         decl_list.add_declaration(declaration);
     }
 
     pub fn lookup(&self, selector: &str) -> Option<&DeclarationList> {
-        self.selectors_to_declarations.get(selector)
+        self.selectors_to_declarations.get(&Selector::new(selector))
     }
 }
 
@@ -310,7 +317,7 @@ unsafe extern "C" fn css_property(
 
                             handler_data
                                 .css_rules
-                                .add_declaration(&selector_name, declaration);
+                                .add_declaration(Selector::new(&selector_name), declaration);
                         }
                         Err(_) => (), // invalid property name or invalid value; ignore
                     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]