bevy_ui_style_builder 0.1.0

Experimental Bevy UI helper extension methods.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
use bevy::prelude::*;
use std::ops::Div;
use std::ops::DivAssign;
use std::ops::Mul;
use std::ops::MulAssign;
use thiserror::Error;

pub mod prelude {
    pub use crate::node;
    pub use crate::style;
    pub use crate::Breadth;
    pub use crate::NodeColorExt;
    pub use crate::NumRect;
    pub use crate::StyleBuilderExt;
}

pub fn node() -> NodeBundle {
    NodeBundle::default()
}

pub fn style() -> Style {
    Style::default()
}

pub enum Either<L, R> {
    Left(L),
    Right(R),
}

/// An enum that describes the possible evaluatable (numeric) values in a flexbox layout.
/// `Breadth` is used to represent distances from side to side that the UI layout algorithm
/// cannot infer automatically.
///
/// `Breadth` is similar to the `Val` enum except that it has no non-evaluatable variants
/// and its methods have been adapted to to reflect that they always have a defined output.
/// For example, [`Val::try_add_with_size`] can return an error, but `Breadth`'s equivalent
/// returns an `f32` and is renamed to [`Breadth::add_with_size`].
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Breadth {
    /// A value in pixels
    Px(f32),
    /// A value in percent
    Percent(f32),
}

impl Default for Breadth {
    fn default() -> Self {
        Self::Px(0.)
    }
}

impl From<Breadth> for Val {
    fn from(value: Breadth) -> Self {
        match value {
            Breadth::Px(inner) => Val::Px(inner),
            Breadth::Percent(inner) => Val::Percent(inner),
        }
    }
}

impl TryFrom<Val> for Breadth {
    type Error = BreadthConversionError;
    fn try_from(value: Val) -> Result<Self, Self::Error> {
        match value {
            Val::Px(inner) => Ok(Breadth::Px(inner)),
            Val::Percent(inner) => Ok(Breadth::Percent(inner)),
            _ => Err(Self::Error::NonEvaluateable),
        }
    }
}

impl Mul<f32> for Breadth {
    type Output = Breadth;

    fn mul(self, rhs: f32) -> Self::Output {
        match self {
            Breadth::Px(value) => Breadth::Px(value * rhs),
            Breadth::Percent(value) => Breadth::Percent(value * rhs),
        }
    }
}

impl MulAssign<f32> for Breadth {
    fn mul_assign(&mut self, rhs: f32) {
        match self {
            Breadth::Px(value) | Breadth::Percent(value) => *value *= rhs,
        }
    }
}

impl Div<f32> for Breadth {
    type Output = Breadth;

    fn div(self, rhs: f32) -> Self::Output {
        match self {
            Breadth::Px(value) => Breadth::Px(value / rhs),
            Breadth::Percent(value) => Breadth::Percent(value / rhs),
        }
    }
}

impl DivAssign<f32> for Breadth {
    fn div_assign(&mut self, rhs: f32) {
        match self {
            Breadth::Px(value) | Breadth::Percent(value) => *value /= rhs,
        }
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Copy, Error)]
pub enum BreadthArithmeticError {
    #[error("the variants of the Breadths don't match")]
    NonIdenticalVariants,
}

#[derive(Debug, Eq, PartialEq, Clone, Copy, Error)]
pub enum BreadthConversionError {
    #[error("Cannot convert from non-evaluatable variants (non-numeric)")]
    NonEvaluateable,
}

impl Breadth {
    /// Tries to add the values of two [`Breadth`]s.
    /// Returns [`BreadthArithmeticError::NonIdenticalVariants`] if two [`Breadth`]s are of different variants.
    pub fn try_add(&self, rhs: Breadth) -> Result<Breadth, BreadthArithmeticError> {
        match (self, rhs) {
            (Breadth::Px(value), Breadth::Px(rhs_value)) => Ok(Breadth::Px(value + rhs_value)),
            (Breadth::Percent(value), Breadth::Percent(rhs_value)) => {
                Ok(Breadth::Percent(value + rhs_value))
            }
            _ => Err(BreadthArithmeticError::NonIdenticalVariants),
        }
    }

    /// Adds `rhs` to `self` and assigns the result to `self` (see [`Breadth::try_add`])
    pub fn try_add_assign(&mut self, rhs: Breadth) -> Result<(), BreadthArithmeticError> {
        *self = self.try_add(rhs)?;
        Ok(())
    }

    /// Tries to subtract the values of two [`Breadth`]s.
    /// Returns [`BreadthArithmeticError::NonIdenticalVariants`] if two [`Breadth`]s are of different variants.
    pub fn try_sub(&self, rhs: Breadth) -> Result<Breadth, BreadthArithmeticError> {
        match (self, rhs) {
            (Breadth::Px(value), Breadth::Px(rhs_value)) => Ok(Breadth::Px(value - rhs_value)),
            (Breadth::Percent(value), Breadth::Percent(rhs_value)) => {
                Ok(Breadth::Percent(value - rhs_value))
            }
            _ => Err(BreadthArithmeticError::NonIdenticalVariants),
        }
    }

    /// Subtracts `rhs` from `self` and assigns the result to `self` (see [`Breadth::try_sub`])
    pub fn try_sub_assign(&mut self, rhs: Breadth) -> Result<(), BreadthArithmeticError> {
        *self = self.try_sub(rhs)?;
        Ok(())
    }

    /// A convenience function for simple evaluation of [`Breadth::Percent`] variant into a concrete [`Breadth::Px`] value.
    /// Otherwise it returns an [`f32`] containing the evaluated value in pixels.
    ///
    /// **Note:** If a [`Breadth::Px`] is evaluated, it's inner value returned unchanged.
    pub fn evaluate(&self, size: f32) -> f32 {
        match self {
            Breadth::Percent(value) => size * value / 100.0,
            Breadth::Px(value) => *value,
        }
    }

    /// Similar to [`Breadth::try_add`], but performs [`Breadth::evaluate`] on both values before adding.
    /// Returns an [`f32`] value in pixels.
    pub fn add_with_size(&self, rhs: Breadth, size: f32) -> f32 {
        self.evaluate(size) + rhs.evaluate(size)
    }

    /// Similar to [`Breadth::try_add_assign`], but performs [`Breadth::evaluate`] on both values before adding.
    /// The value gets converted to [`Breadth::Px`].
    pub fn add_assign_with_size(&mut self, rhs: Breadth, size: f32) {
        *self = Breadth::Px(self.evaluate(size) + rhs.evaluate(size));
    }

    /// Similar to [`Breadth::try_sub`], but performs [`Breadth::evaluate`] on both values before subtracting.
    /// Returns an [`f32`] value in pixels.
    pub fn sub_with_size(&self, rhs: Breadth, size: f32) -> f32 {
        self.evaluate(size) - rhs.evaluate(size)
    }

    /// Similar to [`Breadth::try_sub_assign`], but performs [`Breadth::evaluate`] on both values before adding.
    /// The value gets converted to [`Breadth::Px`].
    pub fn sub_assign_with_size(&mut self, rhs: Breadth, size: f32) {
        *self = Breadth::Px(self.add_with_size(rhs, size));
    }
}

/// A copy of [`UiRect`] but without non-numeric values.
#[derive(Clone, Copy, Debug, Default)]
pub struct NumRect {
    pub left: Breadth,
    pub right: Breadth,
    pub top: Breadth,
    pub bottom: Breadth,
}

impl NumRect {
    pub fn new(left: Breadth, right: Breadth, top: Breadth, bottom: Breadth) -> Self {
        NumRect {
            left,
            right,
            top,
            bottom,
        }
    }

    pub fn all(value: Breadth) -> Self {
        NumRect {
            left: value,
            right: value,
            top: value,
            bottom: value,
        }
    }

    pub fn horizontal(value: Breadth) -> Self {
        NumRect {
            left: value,
            right: value,
            ..Default::default()
        }
    }

    pub fn vertical(value: Breadth) -> Self {
        NumRect {
            top: value,
            bottom: value,
            ..Default::default()
        }
    }

    pub fn left(value: Breadth) -> Self {
        NumRect {
            left: value,
            ..Default::default()
        }
    }

    pub fn right(value: Breadth) -> Self {
        NumRect {
            right: value,
            ..Default::default()
        }
    }

    pub fn top(value: Breadth) -> Self {
        NumRect {
            top: value,
            ..Default::default()
        }
    }

    pub fn bottom(value: Breadth) -> Self {
        NumRect {
            bottom: value,
            ..Default::default()
        }
    }
}

impl From<NumRect> for UiRect {
    fn from(rect: NumRect) -> Self {
        UiRect {
            left: rect.left.into(),
            right: rect.right.into(),
            top: rect.top.into(),
            bottom: rect.bottom.into(),
        }
    }
}

impl From<Breadth> for Either<Breadth, NumRect> {
    fn from(breadth: Breadth) -> Self {
        Either::Left(breadth)
    }
}

impl From<NumRect> for Either<Breadth, NumRect> {
    fn from(rect: NumRect) -> Self {
        Either::Right(rect)
    }
}

impl From<Val> for Either<Val, UiRect> {
    fn from(val: Val) -> Self {
        Either::Left(val)
    }
}

impl From<UiRect> for Either<Val, UiRect> {
    fn from(rect: UiRect) -> Self {
        Either::Right(rect)
    }
}

pub trait StyleBuilderExt: Sized {
    fn style(self, s: impl FnOnce(&mut Style)) -> Self;

    /// Set the left displacement of the node.
    fn left(self, left: Val) -> Self {
        self.style(|style| {
            style.position.left = left;
        })
    }

    /// Set the right displacement of the node.
    fn right(self, right: Val) -> Self {
        self.style(|style| {
            style.position.right = right;
        })
    }

    /// Set the top displacement of the node.
    fn top(self, top: Val) -> Self {
        self.style(|style| {
            style.position.top = top;
        })
    }

    /// Set the bottom displacement of the node.
    fn bottom(self, bottom: Val) -> Self {
        self.style(|style| {
            style.position.bottom = bottom;
        })
    }

    /// Display this node and its children.
    fn display(self) -> Self {
        self.style(|style| {
            style.display = Display::Flex;
        })
    }

    /// Hide this node and its children.
    fn disable(self) -> Self {
        self.style(|style| {
            style.display = Display::None;
        })
    }

    /// Set the flex-direction to `Row`.
    fn row(self) -> Self {
        self.style(|style| {
            style.flex_direction = FlexDirection::Row;
        })
    }

    /// Set the flex-direction to `Column`.
    fn column(self) -> Self {
        self.style(|style| {
            style.flex_direction = FlexDirection::Column;
        })
    }

    /// Set the flex-direction to `RowReverse`.
    fn row_reverse(self) -> Self {
        self.style(|style| {
            style.flex_direction = FlexDirection::RowReverse;
        })
    }

    /// Set the flex-direction to `ColumnReverse`.
    fn column_reverse(self) -> Self {
        self.style(|style| {
            style.flex_direction = FlexDirection::ColumnReverse;
        })
    }

    /// No wrap.
    fn no_wrap(self) -> Self {
        self.style(|style| {
            style.flex_wrap = FlexWrap::NoWrap;
        })
    }

    /// Set flex-wrap to `Wrap`.
    fn wrap(self) -> Self {
        self.style(|style| {
            style.flex_wrap = FlexWrap::Wrap;
        })
    }

    /// Set flex-wrap to `WrapReverse`.
    fn wrap_reverse(self) -> Self {
        self.style(|style| {
            style.flex_wrap = FlexWrap::WrapReverse;
        })
    }

    /// Set the position type to absolute.
    fn absolute(self) -> Self {
        self.style(|style| {
            style.position_type = PositionType::Absolute;
        })
    }

    /// Set the position type to relative.
    fn relative(self) -> Self {
        self.style(|style| {
            style.position_type = PositionType::Relative;
        })
    }

    /// Set flex-basis.
    fn basis(self, basis: Val) -> Self {
        self.style(|style| {
            style.flex_basis = basis;
        })
    }

    /// Set flex-grow.
    fn grow(self, growth: f32) -> Self {
        self.style(|style| {
            style.flex_grow = growth;
        })
    }

    /// Set flex-shrink.
    fn shrink(self, shrink: f32) -> Self {
        self.style(|style| {
            style.flex_shrink = shrink;
        })
    }

    /// Set the minimum width of the node.
    fn min_width(self, min_width: Val) -> Self {
        self.style(|style| {
            style.min_size.width = min_width;
        })
    }

    /// Set the width of the node.
    fn width(self, width: Val) -> Self {
        self.style(|style| {
            style.size.width = width;
        })
    }

    /// Set the maximum width of the node.
    fn max_width(self, max_width: Val) -> Self {
        self.style(|style| {
            style.max_size.width = max_width;
        })
    }

    /// Set the minimum height of the node.
    fn min_height(self, min_height: Val) -> Self {
        self.style(|style| {
            style.min_size.height = min_height;
        })
    }

    /// Set the height of the node.
    fn height(self, height: Val) -> Self {
        self.style(|style| {
            style.size.height = height;
        })
    }

    /// Set the maximum height of the node.
    fn max_height(self, max_height: Val) -> Self {
        self.style(|style| {
            style.max_size.height = max_height;
        })
    }

    /// Set margins for the node.
    fn margin(self, margin: impl Into<Either<Val, UiRect>>) -> Self {
        self.style(|style| {
            style.margin = match margin.into() {
                Either::Left(val) => UiRect::all(val),
                Either::Right(rect) => rect,
            };
        })
    }

    /// Set border thickness for the node.
    fn border(self, border: impl Into<Either<Breadth, NumRect>>) -> Self {
        self.style(|style| {
            style.border = match border.into() {
                Either::Left(breadth) => NumRect::all(breadth),
                Either::Right(rect) => rect,
            }
            .into();
        })
    }

    /// Set padding for the node.
    fn padding(self, padding: impl Into<Either<Breadth, NumRect>>) -> Self {
        self.style(|style| {
            style.padding = match padding.into() {
                Either::Left(breadth) => NumRect::all(breadth),
                Either::Right(rect) => rect,
            }
            .into();
        })
    }

    /// Clip overflow.
    fn hide_overflow(self) -> Self {
        self.style(|style| {
            style.overflow = Overflow::Hidden;
        })
    }

    /// Show overflow.
    fn show_overflow(self) -> Self {
        self.style(|style| {
            style.overflow = Overflow::Visible;
        })
    }

    /// The minimum size of the node.
    /// `min_size` overrides the `size` and `max_size` properties.
    fn min_size(self, size: Size) -> Self {
        self.style(|style| {
            style.min_size = size;
        })
    }

    /// The size of the node.
    fn size(self, size: Size) -> Self {
        self.style(|style| {
            style.size = size;
        })
    }

    /// The maximum size of the node.
    fn max_size(self, size: Size) -> Self {
        self.style(|style| {
            style.max_size = size;
        })
    }

    /// How this item is aligned according to the cross axis
    fn align_self(self, align: AlignSelf) -> Self {
        self.style(|style| {
            style.align_self = align;
        })
    }

    /// How child nodes are aligned according to the cross axis
    fn align_items(self, align: AlignItems) -> Self {
        self.style(|style| {
            style.align_items = align;
        })
    }

    /// Defines how wrapped lines are aligned within the flexbox.
    fn align_content(self, align: AlignContent) -> Self {
        self.style(|style| {
            style.align_content = align;
        })
    }

    /// How child nodes are aligned according to the main axis
    fn justify_content(self, justify: JustifyContent) -> Self {
        self.style(|style| {
            style.justify_content = justify;
        })
    }

    fn align_center(self) -> Self {
        self.style(|style| {
            style.align_items = AlignItems::Center;
        })
    }

    fn align_start(self) -> Self {
        self.style(|style| {
            style.align_items = AlignItems::FlexStart;
        })
    }

    fn align_end(self) -> Self {
        self.style(|style| {
            style.align_items = AlignItems::FlexEnd;
        })
    }

    fn align_stretch(self) -> Self {
        self.style(|style| {
            style.align_items = AlignItems::Stretch;
        })
    }

    fn align_baseline(self) -> Self {
        self.style(|style| {
            style.align_items = AlignItems::Baseline;
        })
    }

    fn justify_center(self) -> Self {
        self.style(|style| {
            style.justify_content = JustifyContent::Center;
        })
    }

    fn justify_start(self) -> Self {
        self.style(|style| {
            style.justify_content = JustifyContent::FlexStart;
        })
    }

    fn justify_end(self) -> Self {
        self.style(|style| {
            style.justify_content = JustifyContent::FlexEnd;
        })
    }

    fn justify_space_between(self) -> Self {
        self.style(|style| {
            style.justify_content = JustifyContent::SpaceBetween;
        })
    }

    fn justify_space_around(self) -> Self {
        self.style(|style| {
            style.justify_content = JustifyContent::SpaceAround;
        })
    }

    fn justify_space_evenly(self) -> Self {
        self.style(|style| {
            style.justify_content = JustifyContent::SpaceEvenly;
        })
    }
}

impl StyleBuilderExt for NodeBundle {
    fn style(mut self, s: impl FnOnce(&mut Style)) -> Self {
        s(&mut self.style);
        self
    }
}

impl StyleBuilderExt for TextBundle {
    fn style(mut self, s: impl FnOnce(&mut Style)) -> Self {
        s(&mut self.style);
        self
    }
}

impl StyleBuilderExt for ImageBundle {
    fn style(mut self, s: impl FnOnce(&mut Style)) -> Self {
        s(&mut self.style);
        self
    }
}

impl StyleBuilderExt for Style {
    fn style(mut self, s: impl FnOnce(&mut Style)) -> Self {
        s(&mut self);
        self
    }
}

impl StyleBuilderExt for &mut Style {
    fn style(self, s: impl FnOnce(&mut Style)) -> Self {
        s(self);
        self
    }
}

pub trait NodeColorExt {
    fn background_color(self, color: Color) -> Self;
}

impl NodeColorExt for NodeBundle {
    fn background_color(mut self, color: Color) -> Self {
        self.background_color = color.into();
        self
    }
}

#[cfg(test)]
mod tests {
    use crate::prelude::*;
    use crate::BreadthArithmeticError;
    use bevy::prelude::*;

    #[test]
    fn test_breadth() {
        let inner = 10.;
        assert_eq!(Val::from(Breadth::Px(inner)), Val::Px(10.0));
        assert_eq!(Val::from(Breadth::Percent(inner)), Val::Percent(10.0));
    }

    #[test]
    fn breadth_try_add() {
        let px_sum = Breadth::Px(20.).try_add(Breadth::Px(22.)).unwrap();
        let percent_sum = Breadth::Percent(50.)
            .try_add(Breadth::Percent(50.))
            .unwrap();

        assert_eq!(px_sum, Breadth::Px(42.));
        assert_eq!(percent_sum, Breadth::Percent(100.));
    }

    #[test]
    fn breadth_try_add_to_self() {
        let mut breadth = Breadth::Px(5.);

        breadth.try_add_assign(Breadth::Px(3.)).unwrap();

        assert_eq!(breadth, Breadth::Px(8.));
    }

    #[test]
    fn breadth_try_sub() {
        let px_sum = Breadth::Px(72.).try_sub(Breadth::Px(30.)).unwrap();
        let percent_sum = Breadth::Percent(100.)
            .try_sub(Breadth::Percent(50.))
            .unwrap();

        assert_eq!(px_sum, Breadth::Px(42.));
        assert_eq!(percent_sum, Breadth::Percent(50.));
    }

    #[test]
    fn different_variant_breadth_try_add() {
        let different_variant_sum_1 = Breadth::Px(50.).try_add(Breadth::Percent(50.));
        let different_variant_sum_2 = Breadth::Percent(50.).try_add(Breadth::Px(50.));

        assert_eq!(
            different_variant_sum_1,
            Err(BreadthArithmeticError::NonIdenticalVariants)
        );
        assert_eq!(
            different_variant_sum_2,
            Err(BreadthArithmeticError::NonIdenticalVariants)
        );
    }

    #[test]
    fn different_variant_breadth_try_sub() {
        let different_variant_diff_1 = Breadth::Px(50.).try_sub(Breadth::Percent(50.));
        let different_variant_diff_2 = Breadth::Percent(50.).try_sub(Breadth::Px(50.));

        assert_eq!(
            different_variant_diff_1,
            Err(BreadthArithmeticError::NonIdenticalVariants)
        );
        assert_eq!(
            different_variant_diff_2,
            Err(BreadthArithmeticError::NonIdenticalVariants)
        );
    }

    #[test]
    fn breadth_evaluate_percent() {
        let size = 250.;
        let result = Breadth::Percent(80.).evaluate(size);

        assert_eq!(result, size * 0.8);
    }

    #[test]
    fn breadth_evaluate_px() {
        let size = 250.;
        let result = Breadth::Px(10.).evaluate(size);

        assert_eq!(result, 10.);
    }

    #[test]
    fn breadth_add_with_size() {
        let size = 250.;

        let px_sum = Breadth::Px(21.).add_with_size(Breadth::Px(21.), size);
        let percent_sum = Breadth::Percent(20.).add_with_size(Breadth::Percent(30.), size);
        let mixed_sum = Breadth::Px(20.).add_with_size(Breadth::Percent(30.), size);

        assert_eq!(px_sum, 42.);
        assert_eq!(percent_sum, 0.5 * size);
        assert_eq!(mixed_sum, 20. + 0.3 * size);
    }

    #[test]
    fn breadth_sub_with_size() {
        let size = 250.;

        let px_sum = Breadth::Px(60.).sub_with_size(Breadth::Px(18.), size);
        let percent_sum = Breadth::Percent(80.).sub_with_size(Breadth::Percent(30.), size);
        let mixed_sum = Breadth::Percent(50.).sub_with_size(Breadth::Px(30.), size);

        assert_eq!(px_sum, 42.);
        assert_eq!(percent_sum, 0.5 * size);
        assert_eq!(mixed_sum, 0.5 * size - 30.);
    }

    #[test]
    fn breadth_arithmetic_error_messages() {
        assert_eq!(
            format!("{}", BreadthArithmeticError::NonIdenticalVariants),
            "the variants of the Breadths don't match"
        );
    }

    #[test]
    fn from_breadth_to_val() {
        let inner_value = 11.;

        assert_eq!(Val::from(Breadth::Px(inner_value)), Val::Px(inner_value));
        assert_eq!(
            Val::from(Breadth::Percent(inner_value)),
            Val::Percent(inner_value)
        );
    }

    #[test]
    fn try_from_val_to_breadth() {
        let inner_value = 22.;

        assert_eq!(
            Breadth::try_from(Val::Auto),
            Err(crate::BreadthConversionError::NonEvaluateable)
        );
        assert_eq!(
            Breadth::try_from(Val::Px(inner_value)),
            Ok(Breadth::Px(inner_value))
        );
        assert_eq!(
            Breadth::try_from(Val::Percent(inner_value)),
            Ok(Breadth::Percent(inner_value))
        );
        assert_eq!(
            Breadth::try_from(Val::Undefined),
            Err(crate::BreadthConversionError::NonEvaluateable)
        );
    }

    #[test]
    fn node_bundle_left() {
        let value = Val::Px(1.);
        let node = node().left(value);
        assert_eq!(node.style.position.left, value);
    }
}