Repository: bevyengine/bevy
File: crates/bevy_ui/src/layout/convert.rs (Rust)

Code before:
        match (value.get_start(), value.get_end()) {
            (Some(start), Some(end)) => taffy::geometry::Line {
                start: style_helpers::line(start),
                end: style_helpers::line(end),
            },
            (Some(start), None) => taffy::geometry::Line {
                start: style_helpers::line(start),
                end: style_helpers::span(span),
            },
            (None, Some(end)) => taffy::geometry::Line {
                start: style_helpers::span(span),
                end: style_helpers::line(end),
            },
            (None, None) => style_helpers::span(span),
        }
    }
}

impl MinTrackSizingFunction {
    fn into_taffy(self, context: &LayoutContext) -> taffy::style::MinTrackSizingFunction {
        match self {
            MinTrackSizingFunction::Px(val) => taffy::style::MinTrackSizingFunction::length(
                Val::Px(val)
                    .into_length_percentage(context)
                    .into_raw()
                    .value(),
            ),
            MinTrackSizingFunction::Percent(val) => taffy::style::MinTrackSizingFunction::length(
                Val::Percent(val)
                    .into_length_percentage(context)
                    .into_raw()
                    .value(),
            ),
            MinTrackSizingFunction::Auto => taffy::style::MinTrackSizingFunction::auto(),
            MinTrackSizingFunction::MinContent => {
                taffy::style::MinTrackSizingFunction::min_content()
            }
            MinTrackSizingFunction::MaxContent => {
                taffy::style::MinTrackSizingFunction::max_content()
            }
            MinTrackSizingFunction::VMin(val) => taffy::style::MinTrackSizingFunction::length(
                Val::VMin(val)
                    .into_length_percentage(context)
                    .into_raw()
                    .value(),
            ),
            MinTrackSizingFunction::VMax(val) => taffy::style::MinTrackSizingFunction::length(
                Val::VMax(val)
                    .into_length_percentage(context)
                    .into_raw()
                    .value(),

Review comment (refactor):
These rather verbose conversions are due to the fact that we do some math in into_length_percentage and into_length_percentage_auto, I would like to refactor the Val type story but this keeps the behavior the same

Code after:
        match (value.get_start(), value.get_end()) {
            (Some(start), Some(end)) => taffy::geometry::Line {
                start: style_helpers::line(start),
                end: style_helpers::line(end),
            },
            (Some(start), None) => taffy::geometry::Line {
                start: style_helpers::line(start),
                end: style_helpers::span(span),
            },
            (None, Some(end)) => taffy::geometry::Line {
                start: style_helpers::span(span),
                end: style_helpers::line(end),
            },
            (None, None) => style_helpers::span(span),
        }
    }
}

impl MinTrackSizingFunction {
    fn into_taffy(self, context: &LayoutContext) -> taffy::style::MinTrackSizingFunction {
        match self {
            MinTrackSizingFunction::Px(val) => Val::Px(val).into_length_percentage(context).into(),
            MinTrackSizingFunction::Percent(val) => {
                Val::Percent(val).into_length_percentage(context).into()
            }
            MinTrackSizingFunction::Auto => taffy::style::MinTrackSizingFunction::auto(),
            MinTrackSizingFunction::MinContent => {
                taffy::style::MinTrackSizingFunction::min_content()
            }
            MinTrackSizingFunction::MaxContent => {
                taffy::style::MinTrackSizingFunction::max_content()
            }
            MinTrackSizingFunction::VMin(val) => {
                Val::VMin(val).into_length_percentage(context).into()
            }
            MinTrackSizingFunction::VMax(val) => {
                Val::VMax(val).into_length_percentage(context).into()
            }
            MinTrackSizingFunction::Vh(val) => Val::Vh(val).into_length_percentage(context).into(),
            MinTrackSizingFunction::Vw(val) => Val::Vw(val).into_length_percentage(context).into(),
        }
    }
}

impl MaxTrackSizingFunction {
    fn into_taffy(self, context: &LayoutContext) -> taffy::style::MaxTrackSizingFunction {
        match self {
            MaxTrackSizingFunction::Px(val) => Val::Px(val).into_length_percentage(context).into(),
            MaxTrackSizingFunction::Percent(val) => {
                Val::Percent(val).into_length_percentage(context).into()
            }