lewp_css/domain/units/conversions/
simplistic_example_of_conversion.rs

1// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.
3
4use {
5    super::{
6        AttributeConversion,
7        CssVariableConversion,
8        FontRelativeLengthConversion,
9        PercentageConversion,
10        ViewportPercentageLengthConversion,
11    },
12    crate::domain::units::Unit,
13    std::collections::HashMap,
14};
15
16#[derive(Default, Debug, Clone)]
17pub struct SimplisticExampleOfConversion<U: Unit> {
18    // Font relative lengths
19    pub em: U::Number,
20    pub ex: U::Number,
21    pub ch: U::Number,
22    pub rem: U::Number,
23
24    // Viewport percentage lengths
25    pub vw: U::Number,
26    pub vh: U::Number,
27    pub vmin: U::Number,
28    pub vmax: U::Number,
29
30    // Percent conversion
31    pub one_hundred_percent_in_absolute_units: U::Number,
32
33    // Simplistic approach
34    pub cssVariablesWithLowerCaseNamesWithoutLeadingDoubleDashToRawCss:
35        HashMap<String, String>,
36    pub attributesWithLowerCaseNames: HashMap<String, String>,
37}
38
39impl<U: Unit> FontRelativeLengthConversion<U::Number>
40    for SimplisticExampleOfConversion<U>
41{
42    #[inline(always)]
43    fn em(&self) -> U::Number {
44        self.em
45    }
46
47    #[inline(always)]
48    fn ex(&self) -> U::Number {
49        self.ex
50    }
51
52    #[inline(always)]
53    fn ch(&self) -> U::Number {
54        self.ch
55    }
56
57    #[inline(always)]
58    fn rem(&self) -> U::Number {
59        self.rem
60    }
61}
62
63impl<U: Unit> ViewportPercentageLengthConversion<U::Number>
64    for SimplisticExampleOfConversion<U>
65{
66    #[inline(always)]
67    fn vw(&self) -> U::Number {
68        self.vw
69    }
70
71    #[inline(always)]
72    fn vh(&self) -> U::Number {
73        self.vh
74    }
75
76    #[inline(always)]
77    fn vmin(&self) -> U::Number {
78        self.vmin
79    }
80
81    #[inline(always)]
82    fn vmax(&self) -> U::Number {
83        self.vmax
84    }
85}
86
87impl<U: Unit> PercentageConversion<U::Number>
88    for SimplisticExampleOfConversion<U>
89{
90    #[inline(always)]
91    fn one_hundred_percent_in_absolute_units(&self) -> U::Number {
92        self.one_hundred_percent_in_absolute_units
93    }
94}
95
96impl<U: Unit> CssVariableConversion for SimplisticExampleOfConversion<U> {
97    #[inline(always)]
98    fn cssVariableValue(
99        &self,
100        css_variable_lower_case_name_without_leading_double_dash: &str,
101    ) -> Option<&str> {
102        match self
103            .cssVariablesWithLowerCaseNamesWithoutLeadingDoubleDashToRawCss
104            .get(css_variable_lower_case_name_without_leading_double_dash)
105        {
106            Some(value) => Some(&value[..]),
107            None => None,
108        }
109    }
110}
111
112impl<U: Unit> AttributeConversion<U> for SimplisticExampleOfConversion<U> {
113    /// Returns the (value of the attribute, property default)
114    /// In this simplistic example, since we don't know th property name, we assume all property defaults are the specification default: <https://drafts.csswg.org/css-values-3/#typedef-type-or-unit>
115    #[inline(always)]
116    fn attributeValue(
117        &self,
118        attribute_lower_case_name: &str,
119    ) -> (Option<&str>, U) {
120        match self
121            .attributesWithLowerCaseNames
122            .get(attribute_lower_case_name)
123        {
124            Some(value) => (Some(&value[..]), U::default()),
125            None => (None, U::default()),
126        }
127    }
128}