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

Code before:
        physical_target_size: Vec2,
    ) -> Result<f32, ValArithmeticError> {
        match self {
            Val::Percent(value) => Ok(physical_base_value * value / 100.0),
            Val::Px(value) => Ok(value * scale_factor),
            Val::Vw(value) => Ok(physical_target_size.x * value / 100.0),
            Val::Vh(value) => Ok(physical_target_size.y * value / 100.0),
            Val::VMin(value) => {
                Ok(physical_target_size.x.min(physical_target_size.y) * value / 100.0)
            }
            Val::VMax(value) => {
                Ok(physical_target_size.x.max(physical_target_size.y) * value / 100.0)
            }
            Val::Auto => Err(ValArithmeticError::NonEvaluable),
        }
    }
}

/// A type which is commonly used to define margins, paddings and borders.
///
/// # Examples
///
/// ## Margin
///
/// A margin is used to create space around UI elements, outside of any defined borders.
///
/// ```
/// # use bevy_ui::{UiRect, Val};
/// #
/// let margin = UiRect::all(Val::Auto); // Centers the UI element
/// ```
///
/// ## Padding
///
/// A padding is used to create space around UI elements, inside of any defined borders.
///
/// ```
/// # use bevy_ui::{UiRect, Val};
/// #
/// let padding = UiRect {
///     left: Val::Px(10.0),
///     right: Val::Px(20.0),
///     top: Val::Px(30.0),
///     bottom: Val::Px(40.0),
/// };
/// ```
///
/// ## Borders
///
/// A border is used to define the width of the border of a UI element.
///

Review comment (suggestion):
The alternative to this is adding `use Val::*` to the prelude, which feels less "special" to me, ties the docs directly to `Val`, keeps our code size down, and aligns with Rust std patterns (ex: `Some`, `None`, `Ok`, `Err`, etc).

The only problem is right now `bsn!` breaks on this case (as it assumes it can do `parent_type.value.0` to set `Some(x)`). This is obviously a problem! The solution is to use `if let` to destructure all types. I believe this will also solve one of the outstanding autocomplete issues (and it will remove the need for the `touch_type` hack). 

Code after:
        physical_target_size: Vec2,
    ) -> Result<f32, ValArithmeticError> {
        match self {
            Val::Percent(value) => Ok(physical_base_value * value / 100.0),
            Val::Px(value) => Ok(value * scale_factor),
            Val::Vw(value) => Ok(physical_target_size.x * value / 100.0),
            Val::Vh(value) => Ok(physical_target_size.y * value / 100.0),
            Val::VMin(value) => {
                Ok(physical_target_size.x.min(physical_target_size.y) * value / 100.0)
            }
            Val::VMax(value) => {
                Ok(physical_target_size.x.max(physical_target_size.y) * value / 100.0)
            }
            Val::Auto => Err(ValArithmeticError::NonEvaluable),
        }
    }
}

/// Returns a [`Val::Auto`] where the value is automatically determined
/// based on the context and other [`Node`](crate::Node) properties.
pub const fn auto() -> Val {
    Val::Auto
}

/// Returns a [`Val::Px`] representing a value in logical pixels.
pub const fn px(value: f32) -> Val {
    Val::Px(value)
}

/// Returns a [`Val::Percent`] representing a percentage of the parent node's length
/// along a specific axis.
///
/// If the UI node has no parent, the percentage is based on the window's length
/// along that axis.
///
/// Axis rules:
/// * For `flex_basis`, the percentage is relative to the main-axis length determined by the `flex_direction`.
/// * For `gap`, `min_size`, `size`, and `max_size`:
///   - `width` is relative to the parent's width.
///   - `height` is relative to the parent's height.
/// * For `margin`, `padding`, and `border` values: the percentage is relative to the parent's width.
/// * For positions, `left` and `right` are relative to the parent's width, while `bottom` and `top` are relative to the parent's height.
pub const fn percent(value: f32) -> Val {
    Val::Percent(value)
}

/// Returns a [`Val::Vw`] representing a percentage of the viewport width.
pub const fn vw(value: f32) -> Val {
    Val::Vw(value)
}