taffy 0.15.0

A flexible UI layout library
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
//! Style types for representing lengths / sizes
use super::CompactLength;
use crate::geometry::Rect;
use crate::style_helpers::{FromLength, FromPercent, TaffyAuto, TaffyZero};
#[cfg(feature = "parse")]
use crate::util::parse::{from_str_from_css, CssParseResult, FromCss, Parser, Token};

/// A unit of linear measurement
///
/// This is commonly combined with [`Rect`], [`Point`](crate::geometry::Point) and [`Size<T>`](crate::geometry::Size).
#[derive(Copy, Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct LengthPercentage(pub(crate) CompactLength);
impl TaffyZero for LengthPercentage {
    const ZERO: Self = Self(CompactLength::ZERO);
}
impl FromLength for LengthPercentage {
    fn from_length<Input: Into<f64> + Copy>(value: Input) -> Self {
        Self::length(value.into() as f32)
    }
}
impl FromPercent for LengthPercentage {
    fn from_percent<Input: Into<f64> + Copy>(value: Input) -> Self {
        Self::percent(value.into() as f32)
    }
}

#[cfg(feature = "parse")]
impl FromCss for LengthPercentage {
    fn from_css<'i>(parser: &mut Parser<'i, '_>) -> CssParseResult<'i, Self> {
        match parser.next()?.clone() {
            Token::Percentage { unit_value, .. } => Ok(Self::percent(unit_value)),
            Token::Dimension { unit, value, .. } if unit == "px" => Ok(Self::length(value)),
            token => Err(parser.new_unexpected_token_error(token))?,
        }
    }
}
#[cfg(feature = "parse")]
from_str_from_css!(LengthPercentage);

impl LengthPercentage {
    /// An absolute length in some abstract units. Users of Taffy may define what they correspond
    /// to in their application (pixels, logical pixels, mm, etc) as they see fit.
    #[inline(always)]
    pub const fn length(val: f32) -> Self {
        Self(CompactLength::length(val))
    }

    /// A percentage length relative to the size of the containing block.
    ///
    /// **NOTE: percentages are represented as a f32 value in the range [0.0, 1.0] NOT the range [0.0, 100.0]**
    #[inline(always)]
    pub const fn percent(val: f32) -> Self {
        Self(CompactLength::percent(val))
    }

    /// A `calc()` value. The value passed here is treated as an opaque handle to
    /// the actual calc representation and may be a pointer, index, etc.
    ///
    /// The low 3 bits are used as a tag value and will be returned as 0.
    #[inline(always)]
    #[cfg(feature = "calc")]
    pub fn calc(ptr: *const ()) -> Self {
        Self(CompactLength::calc(ptr))
    }

    /// Create a LengthPercentage from a raw `CompactLength`.
    /// # Safety
    /// CompactLength must represent a valid variant for LengthPercentage
    #[allow(unsafe_code)]
    pub const unsafe fn from_raw(val: CompactLength) -> Self {
        Self(val)
    }

    /// Get the underlying `CompactLength` representation of the value
    pub const fn into_raw(self) -> CompactLength {
        self.0
    }

    /// Expand the compact representation into an [`ExpandedLengthPercentage`] enum.
    ///
    /// This is useful when integrating with other libraries (e.g. for style inspection or
    /// serialization) as it allows the value to be pattern-matched without having to work
    /// with the raw [`CompactLength`] tagged-pointer representation directly.
    pub fn expand(self) -> ExpandedLengthPercentage {
        match self.0.tag() {
            CompactLength::LENGTH_TAG => ExpandedLengthPercentage::Length(self.0.value()),
            CompactLength::PERCENT_TAG => ExpandedLengthPercentage::Percent(self.0.value()),
            #[cfg(feature = "calc")]
            _ if self.0.is_plain_calc() => ExpandedLengthPercentage::Calc(self.0.calc_value()),
            _ => unreachable!("LengthPercentage contains a value with an invalid tag"),
        }
    }
}

/// The expanded, non-compact representation of a [`LengthPercentage`].
///
/// Obtained via [`LengthPercentage::expand`]. Can be converted back into a [`LengthPercentage`]
/// using the [`From`] implementation.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum ExpandedLengthPercentage {
    /// An absolute length (see [`LengthPercentage::length`])
    Length(f32),
    /// A percentage length (see [`LengthPercentage::percent`])
    Percent(f32),
    /// A `calc()` value (see [`LengthPercentage::calc`]). The pointer is an opaque handle to the
    /// calc representation, exactly as passed to the constructor.
    #[cfg(feature = "calc")]
    Calc(*const ()),
}

impl From<LengthPercentage> for ExpandedLengthPercentage {
    fn from(value: LengthPercentage) -> Self {
        value.expand()
    }
}

impl From<ExpandedLengthPercentage> for LengthPercentage {
    fn from(value: ExpandedLengthPercentage) -> Self {
        match value {
            ExpandedLengthPercentage::Length(val) => Self::length(val),
            ExpandedLengthPercentage::Percent(val) => Self::percent(val),
            #[cfg(feature = "calc")]
            ExpandedLengthPercentage::Calc(ptr) => Self::calc(ptr),
        }
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for LengthPercentage {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let inner = CompactLength::deserialize(deserializer)?;
        // Note: validation intentionally excludes the CALC_TAG as deserializing calc() values is not supported
        if matches!(inner.tag(), CompactLength::LENGTH_TAG | CompactLength::PERCENT_TAG) {
            Ok(Self(inner))
        } else {
            Err(serde::de::Error::custom("Invalid tag"))
        }
    }
}

/// A unit of linear measurement
///
/// This is commonly combined with [`Rect`], [`Point`](crate::geometry::Point) and [`Size<T>`](crate::geometry::Size).
#[derive(Copy, Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct LengthPercentageAuto(pub(crate) CompactLength);
impl TaffyZero for LengthPercentageAuto {
    const ZERO: Self = Self(CompactLength::ZERO);
}
impl TaffyAuto for LengthPercentageAuto {
    const AUTO: Self = Self(CompactLength::AUTO);
}
impl FromLength for LengthPercentageAuto {
    fn from_length<Input: Into<f64> + Copy>(value: Input) -> Self {
        Self::length(value.into() as f32)
    }
}
impl FromPercent for LengthPercentageAuto {
    fn from_percent<Input: Into<f64> + Copy>(value: Input) -> Self {
        Self::percent(value.into() as f32)
    }
}
impl From<LengthPercentage> for LengthPercentageAuto {
    fn from(input: LengthPercentage) -> Self {
        Self(input.0)
    }
}

#[cfg(feature = "parse")]
impl FromCss for LengthPercentageAuto {
    fn from_css<'i>(parser: &mut Parser<'i, '_>) -> CssParseResult<'i, Self> {
        match parser.next()?.clone() {
            Token::Percentage { unit_value, .. } => Ok(Self::percent(unit_value)),
            Token::Dimension { unit, value, .. } if unit == "px" => Ok(Self::length(value)),
            Token::Ident(ident) if ident == "auto" => Ok(Self::auto()),
            token => Err(parser.new_unexpected_token_error(token))?,
        }
    }
}
#[cfg(feature = "parse")]
from_str_from_css!(LengthPercentageAuto);

impl LengthPercentageAuto {
    /// An absolute length in some abstract units. Users of Taffy may define what they correspond
    /// to in their application (pixels, logical pixels, mm, etc) as they see fit.
    #[inline(always)]
    pub const fn length(val: f32) -> Self {
        Self(CompactLength::length(val))
    }

    /// A percentage length relative to the size of the containing block.
    ///
    /// **NOTE: percentages are represented as a f32 value in the range [0.0, 1.0] NOT the range [0.0, 100.0]**
    #[inline(always)]
    pub const fn percent(val: f32) -> Self {
        Self(CompactLength::percent(val))
    }

    /// The dimension should be automatically computed according to algorithm-specific rules
    /// regarding the default size of boxes.
    #[inline(always)]
    pub const fn auto() -> Self {
        Self(CompactLength::auto())
    }

    /// A `calc()` value. The value passed here is treated as an opaque handle to
    /// the actual calc representation and may be a pointer, index, etc.
    ///
    /// The low 3 bits are used as a tag value and will be returned as 0.
    #[inline]
    #[cfg(feature = "calc")]
    pub fn calc(ptr: *const ()) -> Self {
        Self(CompactLength::calc(ptr))
    }

    /// Create a LengthPercentageAuto from a raw `CompactLength`.
    /// # Safety
    /// CompactLength must represent a valid variant for LengthPercentageAuto
    #[allow(unsafe_code)]
    pub const unsafe fn from_raw(val: CompactLength) -> Self {
        Self(val)
    }

    /// Get the underlying `CompactLength` representation of the value
    pub const fn into_raw(self) -> CompactLength {
        self.0
    }

    /// Returns:
    ///   - Some(length) for Length variants
    ///   - Some(resolved) using the provided context for Percent variants
    ///   - None for Auto variants
    #[inline(always)]
    pub fn resolve_to_option(self, context: f32, calc_resolver: impl Fn(*const (), f32) -> f32) -> Option<f32> {
        match self.0.tag() {
            CompactLength::LENGTH_TAG => Some(self.0.value()),
            CompactLength::PERCENT_TAG => Some(context * self.0.value()),
            CompactLength::AUTO_TAG => None,
            #[cfg(feature = "calc")]
            _ if self.0.is_plain_calc() => Some(calc_resolver(self.0.calc_value(), context)),
            _ => unreachable!("LengthPercentageAuto values cannot be constructed with other tags"),
        }
    }

    /// Returns true if value is LengthPercentageAuto::Auto
    #[inline(always)]
    pub fn is_auto(self) -> bool {
        self.0.is_auto()
    }

    /// Expand the compact representation into an [`ExpandedLengthPercentageAuto`] enum.
    ///
    /// This is useful when integrating with other libraries (e.g. for style inspection or
    /// serialization) as it allows the value to be pattern-matched without having to work
    /// with the raw [`CompactLength`] tagged-pointer representation directly.
    pub fn expand(self) -> ExpandedLengthPercentageAuto {
        match self.0.tag() {
            CompactLength::LENGTH_TAG => ExpandedLengthPercentageAuto::Length(self.0.value()),
            CompactLength::PERCENT_TAG => ExpandedLengthPercentageAuto::Percent(self.0.value()),
            CompactLength::AUTO_TAG => ExpandedLengthPercentageAuto::Auto,
            #[cfg(feature = "calc")]
            _ if self.0.is_plain_calc() => ExpandedLengthPercentageAuto::Calc(self.0.calc_value()),
            _ => unreachable!("LengthPercentageAuto contains a value with an invalid tag"),
        }
    }
}

/// The expanded, non-compact representation of a [`LengthPercentageAuto`].
///
/// Obtained via [`LengthPercentageAuto::expand`]. Can be converted back into a
/// [`LengthPercentageAuto`] using the [`From`] implementation.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum ExpandedLengthPercentageAuto {
    /// An absolute length (see [`LengthPercentageAuto::length`])
    Length(f32),
    /// A percentage length (see [`LengthPercentageAuto::percent`])
    Percent(f32),
    /// The automatic keyword (see [`LengthPercentageAuto::auto`])
    Auto,
    /// A `calc()` value (see [`LengthPercentageAuto::calc`]). The pointer is an opaque handle to
    /// the calc representation, exactly as passed to the constructor.
    #[cfg(feature = "calc")]
    Calc(*const ()),
}

impl From<LengthPercentageAuto> for ExpandedLengthPercentageAuto {
    fn from(value: LengthPercentageAuto) -> Self {
        value.expand()
    }
}

impl From<ExpandedLengthPercentageAuto> for LengthPercentageAuto {
    fn from(value: ExpandedLengthPercentageAuto) -> Self {
        match value {
            ExpandedLengthPercentageAuto::Length(val) => Self::length(val),
            ExpandedLengthPercentageAuto::Percent(val) => Self::percent(val),
            ExpandedLengthPercentageAuto::Auto => Self::auto(),
            #[cfg(feature = "calc")]
            ExpandedLengthPercentageAuto::Calc(ptr) => Self::calc(ptr),
        }
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for LengthPercentageAuto {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let inner = CompactLength::deserialize(deserializer)?;
        // Note: validation intentionally excludes the CALC_TAG as deserializing calc() values is not supported
        if matches!(inner.tag(), CompactLength::LENGTH_TAG | CompactLength::PERCENT_TAG | CompactLength::AUTO_TAG) {
            Ok(Self(inner))
        } else {
            Err(serde::de::Error::custom("Invalid tag"))
        }
    }
}

/// A unit of linear measurement
///
/// This is commonly combined with [`Rect`], [`Point`](crate::geometry::Point) and [`Size<T>`](crate::geometry::Size).
#[derive(Copy, Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Dimension(pub(crate) CompactLength);
impl TaffyZero for Dimension {
    const ZERO: Self = Self(CompactLength::ZERO);
}
impl TaffyAuto for Dimension {
    const AUTO: Self = Self(CompactLength::AUTO);
}
impl FromLength for Dimension {
    fn from_length<Input: Into<f64> + Copy>(value: Input) -> Self {
        Self::length(value.into() as f32)
    }
}
impl FromPercent for Dimension {
    fn from_percent<Input: Into<f64> + Copy>(value: Input) -> Self {
        Self::percent(value.into() as f32)
    }
}
impl From<LengthPercentage> for Dimension {
    fn from(input: LengthPercentage) -> Self {
        Self(input.0)
    }
}
impl From<LengthPercentageAuto> for Dimension {
    fn from(input: LengthPercentageAuto) -> Self {
        Self(input.0)
    }
}

#[cfg(feature = "parse")]
impl FromCss for Dimension {
    fn from_css<'i>(parser: &mut Parser<'i, '_>) -> CssParseResult<'i, Self> {
        let token = parser.next()?.clone();
        match token {
            Token::Percentage { unit_value, .. } => Ok(Self::percent(unit_value)),
            Token::Dimension { unit, value, .. } if unit == "px" => Ok(Self::length(value)),
            Token::Ident(ref ident) => match ident.as_ref() {
                "auto" => Ok(Self::auto()),
                "min-content" => Ok(Self::min_content()),
                "max-content" => Ok(Self::max_content()),
                "fit-content" => Ok(Self::fit_content()),
                "stretch" => Ok(Self::stretch()),
                "content" => Ok(Self::content()),
                _ => Err(parser.new_unexpected_token_error(token))?,
            },
            Token::Function(ref name) if name.as_ref() == "fit-content" => parser.parse_nested_block(|parser| {
                let token = parser.next()?.clone();
                match token {
                    Token::Percentage { unit_value, .. } => Ok(Self::fit_content_percent(unit_value)),
                    Token::Dimension { unit, value, .. } if unit == "px" => Ok(Self::fit_content_px(value)),
                    token => Err(parser.new_unexpected_token_error(token))?,
                }
            }),
            token => Err(parser.new_unexpected_token_error(token))?,
        }
    }
}
#[cfg(feature = "parse")]
from_str_from_css!(Dimension);

impl Dimension {
    /// An absolute length in some abstract units. Users of Taffy may define what they correspond
    /// to in their application (pixels, logical pixels, mm, etc) as they see fit.
    #[inline(always)]
    pub const fn length(val: f32) -> Self {
        Self(CompactLength::length(val))
    }

    /// A percentage length relative to the size of the containing block.
    ///
    /// **NOTE: percentages are represented as a f32 value in the range [0.0, 1.0] NOT the range [0.0, 100.0]**
    #[inline(always)]
    pub const fn percent(val: f32) -> Self {
        Self(CompactLength::percent(val))
    }

    /// The dimension should be automatically computed according to algorithm-specific rules
    /// regarding the default size of boxes.
    #[inline(always)]
    pub const fn auto() -> Self {
        Self(CompactLength::auto())
    }

    /// The size should be the "min-content" size.
    /// This is the smallest size that can fit the item's contents with ALL soft line-wrapping opportunities taken
    #[inline(always)]
    pub const fn min_content() -> Self {
        Self(CompactLength::min_content())
    }

    /// The size should be the "max-content" size.
    /// This is the smallest size that can fit the item's contents with NO soft line-wrapping opportunities taken
    #[inline(always)]
    pub const fn max_content() -> Self {
        Self(CompactLength::max_content())
    }

    /// The size should be computed according to the "fit content" formula:
    ///    `max(min_content, min(max_content, stretch))`
    /// where `stretch` is the size the box would take if it filled the available space
    #[inline(always)]
    pub const fn fit_content() -> Self {
        Self(CompactLength::fit_content_keyword())
    }

    /// The size should be computed according to the "fit content" formula:
    ///    `max(min_content, min(max_content, limit))`
    /// where `limit` is a LENGTH value
    #[inline(always)]
    pub const fn fit_content_px(limit: f32) -> Self {
        Self(CompactLength::fit_content_px(limit))
    }

    /// The size should be the "stretch-fit" size: the size the box would take
    /// if it filled the available space
    /// (<https://www.w3.org/TR/css-sizing-4/#stretch-fit-sizing>)
    #[inline(always)]
    pub const fn stretch() -> Self {
        Self(CompactLength::stretch())
    }

    /// The size should be an automatic size based on the box's content
    /// (<https://www.w3.org/TR/css-flexbox-1/#valdef-flex-basis-content>)
    ///
    /// This keyword is only valid for `flex-basis`. In any other context it behaves as [`auto`](Self::auto).
    #[inline(always)]
    pub const fn content() -> Self {
        Self(CompactLength::content())
    }

    /// The size should be computed according to the "fit content" formula:
    ///    `max(min_content, min(max_content, limit))`
    /// where `limit` is a PERCENTAGE value
    #[inline(always)]
    pub const fn fit_content_percent(limit: f32) -> Self {
        Self(CompactLength::fit_content_percent(limit))
    }

    /// A `calc()` value. The value passed here is treated as an opaque handle to
    /// the actual calc representation and may be a pointer, index, etc.
    ///
    /// The low 3 bits are used as a tag value and will be returned as 0.
    #[inline]
    #[cfg(feature = "calc")]
    pub fn calc(ptr: *const ()) -> Self {
        Self(CompactLength::calc(ptr))
    }

    /// A `fit-content()` value whose limit is a `calc()` value. The value passed here is treated as an opaque
    /// handle to the actual calc representation and may be a pointer, index, etc.
    ///
    /// The low 3 bits are used as a tag value and will be returned as 0.
    #[inline]
    #[cfg(feature = "calc")]
    pub fn fit_content_calc(ptr: *const ()) -> Self {
        Self(CompactLength::fit_content_calc(ptr))
    }

    /// Returns true if the value is a `fit-content()` value with a `calc()` limit
    #[inline(always)]
    pub fn is_fit_content_calc(self) -> bool {
        self.0.is_fit_content_calc()
    }

    /// Get the calc() pointer (valid for `calc()` and `fit-content(calc())` values)
    #[inline(always)]
    #[cfg(feature = "calc")]
    pub fn calc_value(self) -> *const () {
        self.0.calc_value()
    }

    /// Create a LengthPercentageAuto from a raw `CompactLength`.
    /// # Safety
    /// CompactLength must represent a valid variant for LengthPercentageAuto
    #[allow(unsafe_code)]
    pub const unsafe fn from_raw(val: CompactLength) -> Self {
        Self(val)
    }

    /// Get the underlying `CompactLength` representation of the value
    pub const fn into_raw(self) -> CompactLength {
        self.0
    }

    /// Get Length value if value is Length variant
    #[cfg(feature = "grid")]
    pub fn into_option(self) -> Option<f32> {
        match self.0.tag() {
            CompactLength::LENGTH_TAG => Some(self.0.value()),
            _ => None,
        }
    }
    /// Returns true if value is Auto
    #[inline(always)]
    pub fn is_auto(self) -> bool {
        self.0.is_auto()
    }

    /// Returns true if value is min-content, max-content, fit-content, fit-content(...), or stretch
    #[inline(always)]
    pub fn is_sizing_keyword(self) -> bool {
        self.0.is_sizing_keyword()
    }

    /// Returns true if value is the stretch keyword
    #[inline(always)]
    pub fn is_stretch(self) -> bool {
        self.0.tag() == CompactLength::STRETCH_TAG
    }

    /// Returns true if value is the content keyword
    #[inline(always)]
    pub fn is_content(self) -> bool {
        self.0.is_content()
    }

    /// Get the raw `CompactLength` tag
    pub fn tag(self) -> usize {
        self.0.tag()
    }

    /// Get the raw `CompactLength` value for non-calc variants that have a numeric parameter
    pub fn value(self) -> f32 {
        self.0.value()
    }

    /// Expand the compact representation into an [`ExpandedDimension`] enum.
    ///
    /// This is useful when integrating with other libraries (e.g. for style inspection or
    /// serialization) as it allows the value to be pattern-matched without having to work
    /// with the raw [`CompactLength`] tagged-pointer representation directly.
    pub fn expand(self) -> ExpandedDimension {
        match self.0.tag() {
            CompactLength::LENGTH_TAG => ExpandedDimension::Length(self.0.value()),
            CompactLength::PERCENT_TAG => ExpandedDimension::Percent(self.0.value()),
            CompactLength::AUTO_TAG => ExpandedDimension::Auto,
            CompactLength::MIN_CONTENT_TAG => ExpandedDimension::MinContent,
            CompactLength::MAX_CONTENT_TAG => ExpandedDimension::MaxContent,
            CompactLength::FIT_CONTENT_PX_TAG => ExpandedDimension::FitContentPx(self.0.value()),
            CompactLength::FIT_CONTENT_PERCENT_TAG => ExpandedDimension::FitContentPercent(self.0.value()),
            CompactLength::FIT_CONTENT_KEYWORD_TAG => ExpandedDimension::FitContent,
            #[cfg(feature = "calc")]
            _ if self.0.is_fit_content_calc() => ExpandedDimension::FitContentCalc(self.0.calc_value()),
            CompactLength::STRETCH_TAG => ExpandedDimension::Stretch,
            CompactLength::CONTENT_TAG => ExpandedDimension::Content,
            #[cfg(feature = "calc")]
            _ if self.0.is_plain_calc() => ExpandedDimension::Calc(self.0.calc_value()),
            _ => unreachable!("Dimension contains a value with an invalid tag"),
        }
    }
}

/// The expanded, non-compact representation of a [`Dimension`].
///
/// Obtained via [`Dimension::expand`]. Can be converted back into a [`Dimension`] using the
/// [`From`] implementation.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum ExpandedDimension {
    /// An absolute length (see [`Dimension::length`])
    Length(f32),
    /// A percentage length (see [`Dimension::percent`])
    Percent(f32),
    /// The automatic keyword (see [`Dimension::auto`])
    Auto,
    /// The `min-content` keyword (see [`Dimension::min_content`])
    MinContent,
    /// The `max-content` keyword (see [`Dimension::max_content`])
    MaxContent,
    /// A `fit-content(...)` value with a length limit (see [`Dimension::fit_content_px`])
    FitContentPx(f32),
    /// A `fit-content(...)` value with a percentage limit (see [`Dimension::fit_content_percent`])
    FitContentPercent(f32),
    /// The size is the fit-content size with a `calc()` limit (an opaque handle to the calc representation)
    #[cfg(feature = "calc")]
    FitContentCalc(*const ()),
    /// The `fit-content` keyword with no limit (see [`Dimension::fit_content`])
    FitContent,
    /// The `stretch` keyword (see [`Dimension::stretch`])
    Stretch,
    /// The `content` keyword (see [`Dimension::content`])
    Content,
    /// A `calc()` value (see [`Dimension::calc`]). The pointer is an opaque handle to the calc
    /// representation, exactly as passed to the constructor.
    #[cfg(feature = "calc")]
    Calc(*const ()),
}

impl From<Dimension> for ExpandedDimension {
    fn from(value: Dimension) -> Self {
        value.expand()
    }
}

impl From<ExpandedDimension> for Dimension {
    fn from(value: ExpandedDimension) -> Self {
        match value {
            ExpandedDimension::Length(val) => Self::length(val),
            ExpandedDimension::Percent(val) => Self::percent(val),
            ExpandedDimension::Auto => Self::auto(),
            ExpandedDimension::MinContent => Self::min_content(),
            ExpandedDimension::MaxContent => Self::max_content(),
            ExpandedDimension::FitContentPx(val) => Self::fit_content_px(val),
            ExpandedDimension::FitContentPercent(val) => Self::fit_content_percent(val),
            ExpandedDimension::FitContent => Self::fit_content(),
            #[cfg(feature = "calc")]
            ExpandedDimension::FitContentCalc(ptr) => Self::fit_content_calc(ptr),
            ExpandedDimension::Stretch => Self::stretch(),
            ExpandedDimension::Content => Self::content(),
            #[cfg(feature = "calc")]
            ExpandedDimension::Calc(ptr) => Self::calc(ptr),
        }
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Dimension {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let inner = CompactLength::deserialize(deserializer)?;
        // Note: validation intentionally excludes the CALC_TAG as deserializing calc() values is not supported
        if matches!(
            inner.tag(),
            CompactLength::LENGTH_TAG
                | CompactLength::PERCENT_TAG
                | CompactLength::AUTO_TAG
                | CompactLength::MIN_CONTENT_TAG
                | CompactLength::MAX_CONTENT_TAG
                | CompactLength::FIT_CONTENT_KEYWORD_TAG
                | CompactLength::FIT_CONTENT_PX_TAG
                | CompactLength::FIT_CONTENT_PERCENT_TAG
                | CompactLength::STRETCH_TAG
                | CompactLength::CONTENT_TAG
        ) {
            Ok(Self(inner))
        } else {
            Err(serde::de::Error::custom("Invalid tag"))
        }
    }
}

impl Rect<Dimension> {
    /// Create a new Rect with length values
    #[must_use]
    pub const fn from_length(start: f32, end: f32, top: f32, bottom: f32) -> Self {
        Rect {
            left: Dimension(CompactLength::length(start)),
            right: Dimension(CompactLength::length(end)),
            top: Dimension(CompactLength::length(top)),
            bottom: Dimension(CompactLength::length(bottom)),
        }
    }

    /// Create a new Rect with percentage values
    #[must_use]
    pub const fn from_percent(start: f32, end: f32, top: f32, bottom: f32) -> Self {
        Rect {
            left: Dimension(CompactLength::percent(start)),
            right: Dimension(CompactLength::percent(end)),
            top: Dimension(CompactLength::percent(top)),
            bottom: Dimension(CompactLength::percent(bottom)),
        }
    }
}

#[cfg(test)]
mod expand_tests {
    use super::*;

    /// A helper that produces a valid `calc()` handle for tests: a non-null pointer whose low
    /// three bits are zero (as required by `CompactLength::calc`).
    #[cfg(feature = "calc")]
    fn calc_handle() -> *const () {
        #[allow(dead_code)]
        #[repr(align(8))]
        struct Aligned(u64);
        static HANDLE: Aligned = Aligned(0);
        &HANDLE as *const Aligned as *const ()
    }

    #[test]
    fn length_percentage_round_trips() {
        let cases = [LengthPercentage::length(12.0), LengthPercentage::percent(0.5), LengthPercentage::ZERO];
        for value in cases {
            assert_eq!(LengthPercentage::from(value.expand()), value);
            assert_eq!(ExpandedLengthPercentage::from(value), value.expand());
        }
        assert_eq!(LengthPercentage::length(3.0).expand(), ExpandedLengthPercentage::Length(3.0));
        assert_eq!(LengthPercentage::percent(0.25).expand(), ExpandedLengthPercentage::Percent(0.25));
    }

    #[test]
    fn length_percentage_auto_round_trips() {
        let cases =
            [LengthPercentageAuto::length(12.0), LengthPercentageAuto::percent(0.5), LengthPercentageAuto::auto()];
        for value in cases {
            assert_eq!(LengthPercentageAuto::from(value.expand()), value);
        }
        assert_eq!(LengthPercentageAuto::auto().expand(), ExpandedLengthPercentageAuto::Auto);
    }

    #[test]
    fn dimension_round_trips_all_keywords() {
        let cases = [
            Dimension::length(12.0),
            Dimension::percent(0.5),
            Dimension::auto(),
            Dimension::min_content(),
            Dimension::max_content(),
            Dimension::fit_content_px(30.0),
            Dimension::fit_content_percent(0.75),
            Dimension::fit_content(),
            Dimension::stretch(),
            Dimension::content(),
        ];
        for value in cases {
            assert_eq!(Dimension::from(value.expand()), value);
        }
        assert_eq!(Dimension::fit_content_px(30.0).expand(), ExpandedDimension::FitContentPx(30.0));
        assert_eq!(Dimension::content().expand(), ExpandedDimension::Content);
    }

    #[cfg(feature = "calc")]
    #[test]
    fn calc_round_trips() {
        let handle = calc_handle();
        assert_eq!(LengthPercentage::calc(handle).expand(), ExpandedLengthPercentage::Calc(handle));
        assert_eq!(LengthPercentage::from(ExpandedLengthPercentage::Calc(handle)), LengthPercentage::calc(handle));
        assert_eq!(Dimension::calc(handle).expand(), ExpandedDimension::Calc(handle));
        assert_eq!(Dimension::fit_content_calc(handle).expand(), ExpandedDimension::FitContentCalc(handle));
        assert_eq!(Dimension::from(ExpandedDimension::FitContentCalc(handle)), Dimension::fit_content_calc(handle));
        assert_eq!(LengthPercentageAuto::calc(handle).expand(), ExpandedLengthPercentageAuto::Calc(handle));
    }
}