Repository: jaytaph/gosub-browser
File: crates/gosub_css3/src/matcher/styling.rs (Rust)

Code before:

    /// Returns the actual value of the property. Will compute the value when needed
    pub fn compute_value(&mut self) -> &CssValue {
        if self.dirty {
            self.calculate_value();
            self.dirty = false;
        }

        &self.actual
    }

    fn calculate_value(&mut self) {
        self.cascaded = self.find_cascaded_value();
        self.specified = self.find_specified_value();
        self.computed = self.find_computed_value();
        self.used = self.find_used_value();
        self.actual = self.find_actual_value();
    }

    fn find_cascaded_value(&self) -> Option<CssValue> {
        self.declared
            .iter()
            .max_by(|a, b| match a.cmp(b) {
                Ordering::Greater => Ordering::Greater,
                Ordering::Less => Ordering::Less,
                Ordering::Equal => a.specificity.cmp(&b.specificity),
            })
            .map(|v| v.value.clone())
    }

    fn find_specified_value(&self) -> CssValue {
        self.cascaded.as_ref().unwrap_or(&self.inherited).clone()
    }

    fn find_computed_value(&self) -> CssValue {
        if self.specified != CssValue::None {
            return self.specified.clone();
        }

        self.get_initial_value().unwrap_or(CssValue::None)
    }

    fn find_used_value(&self) -> CssValue {
        self.computed.clone()
    }

    fn find_actual_value(&self) -> CssValue {
        // @TODO: stuff like clipping and such should occur as well
        match &self.used {
            CssValue::Number(len) => CssValue::Number(len.round()),
            CssValue::Percentage(perc) => CssValue::Percentage(perc.round()),

Review comment (suggestion):
Hellohello, another random comment from me after months of silence... You can use `Ordering::then_with` instead of manually matching against `Equal`. It would look like `a.cmp(b).then_with(|| a.specificity.cmp(&b.specificity))`.
I'm following this project a bit through email notifications, it is amazing what you already managed to do!

Code after:

    /// Returns the actual value of the property. Will compute the value when needed
    pub fn compute_value(&mut self) -> &CssValue {
        if self.dirty {
            self.calculate_value();
            self.dirty = false;
        }

        &self.actual
    }

    fn calculate_value(&mut self) {
        self.cascaded = self.find_cascaded_value();
        self.specified = self.find_specified_value();
        self.computed = self.find_computed_value();
        self.used = self.find_used_value();
        self.actual = self.find_actual_value();
    }

    fn find_cascaded_value(&self) -> Option<CssValue> {
        self.declared
            .iter()
            .max_by(|a, b| a.cmp(b).then_with(|| a.specificity.cmp(&b.specificity)))
            .map(|v| v.value.clone())
    }

    fn find_specified_value(&self) -> CssValue {
        self.cascaded.as_ref().unwrap_or(&self.inherited).clone()
    }

    fn find_computed_value(&self) -> CssValue {
        if self.specified != CssValue::None {
            return self.specified.clone();
        }

        self.get_initial_value().unwrap_or(CssValue::None)
    }

    fn find_used_value(&self) -> CssValue {
        self.computed.clone()
    }

    fn find_actual_value(&self) -> CssValue {
        // @TODO: stuff like clipping and such should occur as well
        match &self.used {
            CssValue::Number(len) => CssValue::Number(len.round()),
            CssValue::Percentage(perc) => CssValue::Percentage(perc.round()),
            CssValue::Unit(value, unit) => CssValue::Unit(value.round(), unit.clone()),
            _ => self.used.clone(),
        }
    }