1use std::time::Duration;
2
3use taffy::style::{Dimension, LengthPercentage, LengthPercentageAuto};
4
5#[derive(Debug, Clone, Copy, PartialEq)]
7pub struct Px(pub f64);
8
9#[derive(Debug, Clone, Copy, PartialEq)]
11pub struct Pct(pub f64);
12impl From<f32> for Pct {
13 fn from(value: f32) -> Self {
14 Pct(value as f64)
15 }
16}
17
18impl From<i32> for Pct {
19 fn from(value: i32) -> Self {
20 Pct(value as f64)
21 }
22}
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub struct Auto;
27
28impl From<f64> for Px {
29 fn from(value: f64) -> Self {
30 Px(value)
31 }
32}
33
34impl From<f32> for Px {
35 fn from(value: f32) -> Self {
36 Px(value as f64)
37 }
38}
39
40impl From<i32> for Px {
41 fn from(value: i32) -> Self {
42 Px(value as f64)
43 }
44}
45
46#[derive(Debug, Clone, Copy, PartialEq)]
47pub enum PxPct {
48 Px(f64),
49 Pct(f64),
50}
51
52impl From<Pct> for PxPct {
53 fn from(value: Pct) -> Self {
54 PxPct::Pct(value.0)
55 }
56}
57
58impl<T> From<T> for PxPct
59where
60 T: Into<Px>,
61{
62 fn from(value: T) -> Self {
63 PxPct::Px(value.into().0)
64 }
65}
66
67#[derive(Debug, Clone, Copy, PartialEq)]
68pub enum PxPctAuto {
69 Px(f64),
70 Pct(f64),
71 Auto,
72}
73
74impl From<Pct> for PxPctAuto {
75 fn from(value: Pct) -> Self {
76 PxPctAuto::Pct(value.0)
77 }
78}
79
80impl From<Auto> for PxPctAuto {
81 fn from(_: Auto) -> Self {
82 PxPctAuto::Auto
83 }
84}
85
86impl<T> From<T> for PxPctAuto
87where
88 T: Into<Px>,
89{
90 fn from(value: T) -> Self {
91 PxPctAuto::Px(value.into().0)
92 }
93}
94
95impl From<PxPct> for PxPctAuto {
96 fn from(value: PxPct) -> Self {
97 match value {
98 PxPct::Pct(pct) => PxPctAuto::Pct(pct),
99 PxPct::Px(px) => PxPctAuto::Px(px),
100 }
101 }
102}
103
104pub trait DurationUnitExt {
105 fn minutes(self) -> Duration;
106 fn seconds(self) -> Duration;
107 fn millis(self) -> Duration;
108}
109impl DurationUnitExt for u64 {
110 fn minutes(self) -> Duration {
111 Duration::from_secs(self)
112 }
113
114 fn seconds(self) -> Duration {
115 Duration::from_secs(self)
116 }
117
118 fn millis(self) -> Duration {
119 Duration::from_millis(self)
120 }
121}
122
123pub trait UnitExt {
124 fn pct(self) -> Pct;
125 fn px(self) -> Px;
126}
127
128impl UnitExt for f64 {
129 fn pct(self) -> Pct {
130 Pct(self)
131 }
132
133 fn px(self) -> Px {
134 Px(self)
135 }
136}
137
138impl UnitExt for i32 {
139 fn pct(self) -> Pct {
140 Pct(self as f64)
141 }
142
143 fn px(self) -> Px {
144 Px(self as f64)
145 }
146}
147
148impl From<PxPctAuto> for Dimension {
149 fn from(value: PxPctAuto) -> Self {
150 match value {
151 PxPctAuto::Px(v) => Dimension::length(v as f32),
152 PxPctAuto::Pct(v) => Dimension::percent(v as f32 / 100.0),
153 PxPctAuto::Auto => Dimension::auto(),
154 }
155 }
156}
157
158impl From<PxPct> for LengthPercentage {
159 fn from(value: PxPct) -> Self {
160 match value {
161 PxPct::Px(v) => LengthPercentage::length(v as f32),
162 PxPct::Pct(v) => LengthPercentage::percent(v as f32 / 100.0),
163 }
164 }
165}
166
167impl From<PxPctAuto> for LengthPercentageAuto {
168 fn from(value: PxPctAuto) -> Self {
169 match value {
170 PxPctAuto::Px(v) => LengthPercentageAuto::length(v as f32),
171 PxPctAuto::Pct(v) => LengthPercentageAuto::percent(v as f32 / 100.0),
172 PxPctAuto::Auto => LengthPercentageAuto::auto(),
173 }
174 }
175}