From 6d7808da74f45910a4c53ec34d4b4e4555b54d48 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Sat, 28 Oct 2023 20:03:18 +0100 Subject: [PATCH] Implement `Display` for `Val` (#10296) # Objective Implement `Display` for `Val` --------- Co-authored-by: Alice Cecile --- crates/bevy_ui/src/geometry.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 9462ef4e770d5..799e827257a1d 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -4,6 +4,7 @@ use bevy_reflect::ReflectDeserialize; use bevy_reflect::ReflectSerialize; use serde::Deserialize; use serde::Serialize; +use std::fmt::Display; use std::ops::Neg; use std::ops::{Div, DivAssign, Mul, MulAssign}; use thiserror::Error; @@ -155,6 +156,22 @@ impl DivAssign for Val { } } +impl Display for Val { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let (value, suffix) = match self { + Val::Auto => return write!(f, "auto"), + Val::Px(value) => (value, "px"), + Val::Percent(value) => (value, "%"), + Val::Vw(value) => (value, "vw"), + Val::Vh(value) => (value, "vh"), + Val::VMin(value) => (value, "vmin"), + Val::VMax(value) => (value, "vmax"), + }; + value.fmt(f)?; + write!(f, "{suffix}") + } +} + impl Neg for Val { type Output = Val;