alpha2-model 0.4.1

Versioned project and timeline model for alpha2.
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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
//! The CSS value forms a layout reads: lengths, the four-side shorthand,
//! grid track lists and grid lines.
//!
//! A JSON number is a length, a fraction of the picture's shorter side. A
//! string is CSS: `"12px"` is pixels of the project canvas, `"50%"` a
//! percent of the parent's content box where a percent is allowed. Inside
//! these strings keywords use CSS spelling (`min-content`, `auto-fill`);
//! everywhere else a layout's keywords are `snake_case`. Whitespace is any run
//! of ASCII spaces. A string that does not parse is refused by the parser,
//! naming the token and, where one is likely, the spelling meant.
//!
//! Each form serializes canonically (a number, `"<n>px"`, `"<n>%"`, the
//! sides object, a track list as CSS), and a serialized value parses to the
//! same value.

use serde::de::{self, Deserializer, MapAccess, Visitor};
use serde::ser::{SerializeStruct, Serializer};
use serde::{Deserialize, Serialize};
use std::fmt::{self, Write as _};

/// A CSS number without an exponent: `-?(\d+\.?\d*|\.\d+)`, finite. Read as
/// a JSON number is, through 64 bits, so `"0.1px"` holds the `f32` that
/// `0.1` does.
pub(crate) fn parse_number(text: &str) -> Option<f32> {
    let digits = text.strip_prefix('-').unwrap_or(text);
    let (whole, fraction) = digits.split_once('.').unwrap_or((digits, ""));
    let all_digits = |part: &str| part.bytes().all(|byte| byte.is_ascii_digit());
    if !(all_digits(whole) && all_digits(fraction)) || (whole.is_empty() && fraction.is_empty()) {
        return None;
    }
    #[allow(clippy::cast_possible_truncation)]
    let value = text.parse::<f64>().ok()? as f32;
    value.is_finite().then_some(value)
}

/// The tokens of a CSS value: runs of characters between ASCII spaces.
fn tokens(text: &str) -> impl Iterator<Item = &str> {
    text.split(' ').filter(|token| !token.is_empty())
}

/// A length's text, as it is written back.
fn write_length(out: &mut impl fmt::Write, value: f32, unit: &str) -> fmt::Result {
    write!(out, "{value}{unit}")
}

/// A layout length that may be a percent: a size or an inset side.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum LayoutLength {
    /// A fraction of the layout picture's shorter side, written as a number.
    Fraction(f32),
    /// Pixels of the project canvas, written `"<n>px"`. A plan turns it into
    /// a fraction of the canvas's shorter side.
    Px(f32),
    /// Percent (0 to 100 is 0% to 100%) of the parent's content box along
    /// the field's axis, written `"<n>%"`.
    Percent(f32),
}

impl Default for LayoutLength {
    fn default() -> Self {
        Self::Fraction(0.0)
    }
}

impl LayoutLength {
    /// The number, whatever its unit.
    #[must_use]
    pub const fn value(self) -> f32 {
        match self {
            Self::Fraction(value) | Self::Px(value) | Self::Percent(value) => value,
        }
    }

    /// Whether it is a percent.
    #[must_use]
    pub const fn is_percent(self) -> bool {
        matches!(self, Self::Percent(_))
    }

    /// The same length with pixels turned into a fraction of a picture
    /// `shorter_side` pixels on its shorter side.
    #[must_use]
    pub fn without_px(self, shorter_side: f32) -> Self {
        match self {
            Self::Px(value) => Self::Fraction(px_fraction(value, shorter_side)),
            other => other,
        }
    }

    /// The same kind of length with `value`.
    #[must_use]
    pub const fn with_value(self, value: f32) -> Self {
        match self {
            Self::Fraction(_) => Self::Fraction(value),
            Self::Px(_) => Self::Px(value),
            Self::Percent(_) => Self::Percent(value),
        }
    }

    fn parse(text: &str) -> Result<Self, String> {
        let size = |value: LayoutSize| match value {
            LayoutSize::Fraction(value) => Self::Fraction(value),
            LayoutSize::Px(value) => Self::Px(value),
        };
        match text.trim_matches(' ').strip_suffix('%') {
            Some(number) => parse_number(number)
                .map(Self::Percent)
                .ok_or_else(|| invalid_length(text, true)),
            None => LayoutSize::parse_token(text.trim_matches(' '), true)
                .map(size)
                .map_err(|_| invalid_length(text, true)),
        }
    }
}

/// `n` canvas pixels as a fraction of a shorter side of `shorter_side`
/// pixels.
#[allow(clippy::cast_possible_truncation)]
fn px_fraction(value: f32, shorter_side: f32) -> f32 {
    (f64::from(value) / f64::from(shorter_side)) as f32
}

impl fmt::Display for LayoutLength {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::Fraction(value) => write!(formatter, "{value}"),
            Self::Px(value) => write_length(formatter, value, "px"),
            Self::Percent(value) => write_length(formatter, value, "%"),
        }
    }
}

/// A layout length that may not be a percent: a gap, a radius, a border
/// width, a font size, a padding or margin side, a translation.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum LayoutSize {
    /// A fraction of the layout picture's shorter side, written as a number.
    Fraction(f32),
    /// Pixels of the project canvas, written `"<n>px"`.
    Px(f32),
}

impl Default for LayoutSize {
    fn default() -> Self {
        Self::Fraction(0.0)
    }
}

impl LayoutSize {
    /// The number, whatever its unit.
    #[must_use]
    pub const fn value(self) -> f32 {
        match self {
            Self::Fraction(value) | Self::Px(value) => value,
        }
    }

    /// The same size with pixels turned into a fraction of a picture
    /// `shorter_side` pixels on its shorter side.
    #[must_use]
    pub fn without_px(self, shorter_side: f32) -> Self {
        match self {
            Self::Px(value) => Self::Fraction(px_fraction(value, shorter_side)),
            other @ Self::Fraction(_) => other,
        }
    }

    /// Its fraction of a picture `shorter_side` pixels on its shorter side.
    #[must_use]
    pub fn fraction(self, shorter_side: f32) -> f32 {
        self.without_px(shorter_side).value()
    }

    /// The same kind of size with `value`.
    #[must_use]
    pub const fn with_value(self, value: f32) -> Self {
        match self {
            Self::Fraction(_) => Self::Fraction(value),
            Self::Px(_) => Self::Px(value),
        }
    }

    /// One token: a number or `<n>px`. `percent` says whether the field takes
    /// a percent, for the message.
    fn parse_token(token: &str, percent: bool) -> Result<Self, String> {
        if let Some(number) = token.strip_suffix("px") {
            return parse_number(number)
                .map(Self::Px)
                .ok_or_else(|| invalid_length(token, percent));
        }
        if !percent && token.ends_with('%') && parse_number(&token[..token.len() - 1]).is_some() {
            return Err(format!("percent is not allowed here: \"{token}\""));
        }
        parse_number(token)
            .map(Self::Fraction)
            .ok_or_else(|| invalid_length(token, percent))
    }

    fn parse(text: &str) -> Result<Self, String> {
        Self::parse_token(text.trim_matches(' '), false)
    }
}

impl fmt::Display for LayoutSize {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::Fraction(value) => write!(formatter, "{value}"),
            Self::Px(value) => write_length(formatter, value, "px"),
        }
    }
}

fn invalid_length(token: &str, percent: bool) -> String {
    let hint = if token.contains(' ') {
        format!(" (write {})", token.replace(' ', ""))
    } else if token.ends_with("em") {
        " (em and rem are not supported: write px or a number)".to_owned()
    } else {
        String::new()
    };
    let forms = if percent {
        "a number, \"<n>px\", or \"<n>%\""
    } else {
        "a number or \"<n>px\""
    };
    format!("invalid length \"{token}\"{hint}: write {forms}")
}

/// A visitor for a value written as a number or a string.
struct NumberOrString<T> {
    expecting: &'static str,
    number: fn(f32) -> T,
    string: fn(&str) -> Result<T, String>,
}

impl<T> Visitor<'_> for NumberOrString<T> {
    type Value = T;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.expecting)
    }

    #[allow(clippy::cast_possible_truncation)]
    fn visit_f64<E: de::Error>(self, value: f64) -> Result<T, E> {
        Ok((self.number)(value as f32))
    }

    #[allow(clippy::cast_precision_loss)]
    fn visit_u64<E: de::Error>(self, value: u64) -> Result<T, E> {
        Ok((self.number)(value as f32))
    }

    #[allow(clippy::cast_precision_loss)]
    fn visit_i64<E: de::Error>(self, value: i64) -> Result<T, E> {
        Ok((self.number)(value as f32))
    }

    fn visit_str<E: de::Error>(self, value: &str) -> Result<T, E> {
        (self.string)(value).map_err(E::custom)
    }
}

impl<'de> Deserialize<'de> for LayoutLength {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        deserializer.deserialize_any(NumberOrString {
            expecting: "a length: a number, \"<n>px\", or \"<n>%\"",
            number: Self::Fraction,
            string: Self::parse,
        })
    }
}

impl Serialize for LayoutLength {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::Fraction(value) => serializer.serialize_f32(*value),
            other => serializer.collect_str(other),
        }
    }
}

impl<'de> Deserialize<'de> for LayoutSize {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        deserializer.deserialize_any(NumberOrString {
            expecting: "a length: a number or \"<n>px\"",
            number: Self::Fraction,
            string: Self::parse,
        })
    }
}

impl Serialize for LayoutSize {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::Fraction(value) => serializer.serialize_f32(*value),
            other @ Self::Px(_) => serializer.collect_str(other),
        }
    }
}

/// A length on each side of a box (a padding or a margin). Missing sides
/// are 0.
///
/// Written as one length for every side, as the CSS shorthand (`"8px 16px"`:
/// one to four lengths, top first, clockwise, the missing ones repeating the
/// opposite side), or as the sides object.
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct Insets {
    /// Top side.
    pub top: LayoutSize,
    /// Right side.
    pub right: LayoutSize,
    /// Bottom side.
    pub bottom: LayoutSize,
    /// Left side.
    pub left: LayoutSize,
}

impl Insets {
    /// The sides, top first, clockwise.
    #[must_use]
    pub const fn sides(self) -> [LayoutSize; 4] {
        [self.top, self.right, self.bottom, self.left]
    }

    /// Insets from sides, top first, clockwise.
    #[must_use]
    pub const fn from_sides([top, right, bottom, left]: [LayoutSize; 4]) -> Self {
        Self {
            top,
            right,
            bottom,
            left,
        }
    }

    /// The same insets with pixels turned into fractions.
    #[must_use]
    pub fn without_px(self, shorter_side: f32) -> Self {
        Self::from_sides(self.sides().map(|side| side.without_px(shorter_side)))
    }

    fn parse(text: &str) -> Result<Self, String> {
        let sides = tokens(text)
            .map(|token| LayoutSize::parse_token(token, false))
            .collect::<Result<Vec<_>, _>>()?;
        let [top, right, bottom, left] = shorthand(&sides)?;
        Ok(Self {
            top,
            right,
            bottom,
            left,
        })
    }
}

/// The four sides of a CSS shorthand of one to four values.
fn shorthand<T: Copy>(values: &[T]) -> Result<[T; 4], String> {
    match *values {
        [all] => Ok([all; 4]),
        [vertical, horizontal] => Ok([vertical, horizontal, vertical, horizontal]),
        [top, horizontal, bottom] => Ok([top, horizontal, bottom, horizontal]),
        [top, right, bottom, left] => Ok([top, right, bottom, left]),
        _ => Err(format!("expected 1 to 4 lengths, got {}", values.len())),
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct InsetsObject {
    #[serde(default)]
    top: LayoutSize,
    #[serde(default)]
    right: LayoutSize,
    #[serde(default)]
    bottom: LayoutSize,
    #[serde(default)]
    left: LayoutSize,
}

struct InsetsVisitor;

impl<'de> Visitor<'de> for InsetsVisitor {
    type Value = Insets;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("insets: a length, one to four lengths, or the sides object")
    }

    #[allow(clippy::cast_possible_truncation)]
    fn visit_f64<E: de::Error>(self, value: f64) -> Result<Insets, E> {
        Ok(Insets::from_sides([LayoutSize::Fraction(value as f32); 4]))
    }

    #[allow(clippy::cast_precision_loss)]
    fn visit_u64<E: de::Error>(self, value: u64) -> Result<Insets, E> {
        Ok(Insets::from_sides([LayoutSize::Fraction(value as f32); 4]))
    }

    #[allow(clippy::cast_precision_loss)]
    fn visit_i64<E: de::Error>(self, value: i64) -> Result<Insets, E> {
        Ok(Insets::from_sides([LayoutSize::Fraction(value as f32); 4]))
    }

    fn visit_str<E: de::Error>(self, value: &str) -> Result<Insets, E> {
        Insets::parse(value).map_err(E::custom)
    }

    fn visit_map<A: MapAccess<'de>>(self, map: A) -> Result<Insets, A::Error> {
        let sides = InsetsObject::deserialize(de::value::MapAccessDeserializer::new(map))?;
        Ok(Insets {
            top: sides.top,
            right: sides.right,
            bottom: sides.bottom,
            left: sides.left,
        })
    }
}

impl<'de> Deserialize<'de> for Insets {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        deserializer.deserialize_any(InsetsVisitor)
    }
}

impl Serialize for Insets {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut sides = serializer.serialize_struct("Insets", 4)?;
        sides.serialize_field("top", &self.top)?;
        sides.serialize_field("right", &self.right)?;
        sides.serialize_field("bottom", &self.bottom)?;
        sides.serialize_field("left", &self.left)?;
        sides.end()
    }
}

/// A box's offset after layout: `x` right, `y` down.
///
/// Written as the object, or as the CSS `translate` value: one or two
/// lengths, `y` 0 when missing.
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct LayoutOffset {
    /// Rightward.
    pub x: LayoutSize,
    /// Downward.
    pub y: LayoutSize,
}

impl LayoutOffset {
    /// The same offset with pixels turned into fractions.
    #[must_use]
    pub fn without_px(self, shorter_side: f32) -> Self {
        Self {
            x: self.x.without_px(shorter_side),
            y: self.y.without_px(shorter_side),
        }
    }

    fn parse(text: &str) -> Result<Self, String> {
        let values = tokens(text)
            .map(|token| LayoutSize::parse_token(token, false))
            .collect::<Result<Vec<_>, _>>()?;
        match *values {
            [x] => Ok(Self {
                x,
                y: LayoutSize::default(),
            }),
            [x, y] => Ok(Self { x, y }),
            _ => Err(format!("expected 1 or 2 lengths, got {}", values.len())),
        }
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct OffsetObject {
    x: LayoutSize,
    y: LayoutSize,
}

struct OffsetVisitor;

impl<'de> Visitor<'de> for OffsetVisitor {
    type Value = LayoutOffset;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("an offset: one or two lengths, or {x, y}")
    }

    fn visit_str<E: de::Error>(self, value: &str) -> Result<LayoutOffset, E> {
        LayoutOffset::parse(value).map_err(E::custom)
    }

    fn visit_map<A: MapAccess<'de>>(self, map: A) -> Result<LayoutOffset, A::Error> {
        let OffsetObject { x, y } =
            OffsetObject::deserialize(de::value::MapAccessDeserializer::new(map))?;
        Ok(LayoutOffset { x, y })
    }
}

impl<'de> Deserialize<'de> for LayoutOffset {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        deserializer.deserialize_any(OffsetVisitor)
    }
}

impl Serialize for LayoutOffset {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut offset = serializer.serialize_struct("LayoutOffset", 2)?;
        offset.serialize_field("x", &self.x)?;
        offset.serialize_field("y", &self.y)?;
        offset.end()
    }
}

/// How large a grid track may be at least or at most: one side of
/// `minmax()`.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum TrackBreadth {
    /// A length or a percent of the grid's content box.
    Length(LayoutLength),
    /// A share of the free space, `<n>fr`; only as the largest size.
    Fr(f32),
    /// `auto`.
    Auto,
    /// `min-content`.
    MinContent,
    /// `max-content`.
    MaxContent,
}

impl TrackBreadth {
    const fn is_fixed(self) -> bool {
        matches!(self, Self::Length(_))
    }

    fn without_px(self, shorter_side: f32) -> Self {
        match self {
            Self::Length(length) => Self::Length(length.without_px(shorter_side)),
            other => other,
        }
    }

    fn parse(token: &str, max: bool) -> Result<Self, String> {
        match token {
            "auto" => Ok(Self::Auto),
            "min-content" => Ok(Self::MinContent),
            "max-content" => Ok(Self::MaxContent),
            _ => {
                if let Some(share) = token.strip_suffix("fr").and_then(parse_number) {
                    return if max {
                        Ok(Self::Fr(share))
                    } else {
                        Err(format!("a minimum size cannot be fr: \"{token}\""))
                    };
                }
                LayoutLength::parse(token)
                    .map(Self::Length)
                    .map_err(|_| unknown_track(token))
            }
        }
    }
}

impl fmt::Display for TrackBreadth {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Length(length) => write!(formatter, "{length}"),
            Self::Fr(share) => write_length(formatter, *share, "fr"),
            Self::Auto => formatter.write_str("auto"),
            Self::MinContent => formatter.write_str("min-content"),
            Self::MaxContent => formatter.write_str("max-content"),
        }
    }
}

/// The size of one grid track.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum TrackSize {
    /// One breadth, as CSS writes it: a length is fixed, `<n>fr` is
    /// `minmax(auto, <n>fr)`, a keyword sizes by content.
    Breadth(TrackBreadth),
    /// `minmax(min, max)`.
    MinMax(TrackBreadth, TrackBreadth),
    /// `fit-content(limit)`.
    FitContent(LayoutLength),
}

impl TrackSize {
    /// Whether it is a CSS fixed size, as `auto-fill` and `auto-fit` need:
    /// a length, or `minmax()` with a length on one side and no flexible
    /// or content minimum.
    #[must_use]
    pub const fn is_fixed(self) -> bool {
        match self {
            Self::Breadth(breadth) => breadth.is_fixed(),
            Self::MinMax(min, max) => {
                min.is_fixed() || (max.is_fixed() && !matches!(min, TrackBreadth::Fr(_)))
            }
            Self::FitContent(_) => false,
        }
    }

    fn without_px(self, shorter_side: f32) -> Self {
        match self {
            Self::Breadth(breadth) => Self::Breadth(breadth.without_px(shorter_side)),
            Self::MinMax(min, max) => {
                Self::MinMax(min.without_px(shorter_side), max.without_px(shorter_side))
            }
            Self::FitContent(limit) => Self::FitContent(limit.without_px(shorter_side)),
        }
    }

    fn lengths(self) -> impl Iterator<Item = LayoutLength> {
        let breadth = |breadth: TrackBreadth| match breadth {
            TrackBreadth::Length(length) => Some(length),
            _ => None,
        };
        let (first, second) = match self {
            Self::Breadth(single) => (breadth(single), None),
            Self::MinMax(min, max) => (breadth(min), breadth(max)),
            Self::FitContent(limit) => (Some(limit), None),
        };
        first.into_iter().chain(second)
    }

    fn fr(self) -> Option<f32> {
        match self {
            Self::Breadth(TrackBreadth::Fr(share)) | Self::MinMax(_, TrackBreadth::Fr(share)) => {
                Some(share)
            }
            _ => None,
        }
    }
}

impl fmt::Display for TrackSize {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Breadth(breadth) => write!(formatter, "{breadth}"),
            Self::MinMax(min, max) => write!(formatter, "minmax({min}, {max})"),
            Self::FitContent(limit) => write!(formatter, "fit-content({limit})"),
        }
    }
}

/// How often `repeat()` repeats its tracks.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum RepeatCount {
    /// That many times.
    Count(u16),
    /// `auto-fill`: as many as fit.
    AutoFill,
    /// `auto-fit`: as many as fit, empty ones collapsed.
    AutoFit,
}

impl fmt::Display for RepeatCount {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Count(count) => write!(formatter, "{count}"),
            Self::AutoFill => formatter.write_str("auto-fill"),
            Self::AutoFit => formatter.write_str("auto-fit"),
        }
    }
}

/// One entry of a track list: a track, or `repeat()` of some.
#[derive(Clone, Debug, PartialEq)]
pub enum TrackComponent {
    /// One track.
    Single(TrackSize),
    /// `repeat(count, tracks)`.
    Repeat(RepeatCount, Vec<TrackSize>),
}

/// A grid track list, as `grid_template_columns` writes it in CSS:
/// `"1fr 2fr 120px"`, `"repeat(3, minmax(100px, 1fr))"`,
/// `"repeat(auto-fill, 160px)"`. Named lines are not supported.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct TrackList(pub Vec<TrackComponent>);

impl TrackList {
    /// How many explicit tracks it has with each count repeated (an
    /// automatic repeat counts once).
    #[must_use]
    pub fn track_count(&self) -> usize {
        self.0
            .iter()
            .map(|component| match component {
                TrackComponent::Single(_) => 1,
                TrackComponent::Repeat(RepeatCount::Count(count), tracks) => {
                    usize::from(*count) * tracks.len()
                }
                TrackComponent::Repeat(_, tracks) => tracks.len(),
            })
            .sum()
    }

    /// Every track size, once each, repeats included once.
    pub fn sizes(&self) -> impl Iterator<Item = TrackSize> + '_ {
        self.0.iter().flat_map(|component| match component {
            TrackComponent::Single(size) => std::slice::from_ref(size).iter().copied(),
            TrackComponent::Repeat(_, tracks) => tracks.iter().copied(),
        })
    }

    /// Every length in it, for range checks.
    pub fn lengths(&self) -> impl Iterator<Item = LayoutLength> + '_ {
        self.sizes().flat_map(TrackSize::lengths)
    }

    /// Every `fr` share in it.
    pub fn shares(&self) -> impl Iterator<Item = f32> + '_ {
        self.sizes().filter_map(TrackSize::fr)
    }

    /// The same list with pixels turned into fractions.
    #[must_use]
    pub fn without_px(&self, shorter_side: f32) -> Self {
        Self(
            self.0
                .iter()
                .map(|component| match component {
                    TrackComponent::Single(size) => {
                        TrackComponent::Single(size.without_px(shorter_side))
                    }
                    TrackComponent::Repeat(count, tracks) => TrackComponent::Repeat(
                        *count,
                        tracks
                            .iter()
                            .map(|size| size.without_px(shorter_side))
                            .collect(),
                    ),
                })
                .collect(),
        )
    }

    fn parse(text: &str) -> Result<Self, String> {
        let mut parser = TrackParser { text, at: 0 };
        let mut components = Vec::new();
        while parser.skip_spaces() {
            if parser.rest().starts_with("repeat(") {
                parser.at += "repeat(".len();
                components.push(parser.repeat()?);
            } else {
                components.push(TrackComponent::Single(parser.track()?));
            }
        }
        if components.is_empty() {
            return Err("expected at least one track".to_owned());
        }
        Ok(Self(components))
    }
}

impl fmt::Display for TrackList {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (index, component) in self.0.iter().enumerate() {
            if index > 0 {
                formatter.write_char(' ')?;
            }
            match component {
                TrackComponent::Single(size) => write!(formatter, "{size}")?,
                TrackComponent::Repeat(count, tracks) => {
                    write!(formatter, "repeat({count},")?;
                    for size in tracks {
                        write!(formatter, " {size}")?;
                    }
                    formatter.write_char(')')?;
                }
            }
        }
        Ok(())
    }
}

/// The message for a track that is not one, with the spelling likely meant.
fn unknown_track(token: &str) -> String {
    let hint = match token {
        "min_content" => Some("min-content".to_owned()),
        "max_content" => Some("max-content".to_owned()),
        "auto_fill" => Some("auto-fill".to_owned()),
        "auto_fit" => Some("auto-fit".to_owned()),
        _ if token.starts_with('[') => {
            return format!("named grid lines are not supported: \"{token}\"");
        }
        _ if token.contains(' ') => Some(token.replace(' ', "")),
        _ => None,
    };
    match hint {
        Some(hint) => format!("unknown track \"{token}\" (write {hint})"),
        None => format!("unknown track \"{token}\""),
    }
}

/// Reads a track list left to right.
struct TrackParser<'a> {
    text: &'a str,
    at: usize,
}

impl<'a> TrackParser<'a> {
    fn rest(&self) -> &'a str {
        &self.text[self.at..]
    }

    /// Skips spaces; whether anything is left.
    fn skip_spaces(&mut self) -> bool {
        while self.rest().starts_with(' ') {
            self.at += 1;
        }
        !self.rest().is_empty()
    }

    /// The next token: up to a space, a comma or a parenthesis.
    fn token(&mut self) -> &'a str {
        let rest = &self.text[self.at..];
        let end = rest.find([' ', ',', '(', ')']).unwrap_or(rest.len());
        self.at += end;
        &rest[..end]
    }

    /// `expected` after optional spaces.
    fn expect(&mut self, expected: char) -> Result<(), String> {
        self.skip_spaces();
        if self.rest().starts_with(expected) {
            self.at += 1;
            Ok(())
        } else {
            Err(format!("expected \"{expected}\" in \"{}\"", self.text))
        }
    }

    fn track(&mut self) -> Result<TrackSize, String> {
        let start = self.at;
        let token = self.token();
        match token {
            "minmax" | "fit-content" if self.rest().starts_with('(') => {
                self.at += 1;
                if token == "fit-content" {
                    let limit = self.breadth(false)?;
                    self.expect(')')?;
                    return match limit {
                        TrackBreadth::Length(limit) => Ok(TrackSize::FitContent(limit)),
                        _ => Err(format!(
                            "fit-content takes a length: \"{}\"",
                            &self.text[start..self.at]
                        )),
                    };
                }
                let min = self.breadth(false)?;
                self.expect(',')?;
                let max = self.breadth(true)?;
                self.expect(')')?;
                Ok(TrackSize::MinMax(min, max))
            }
            "" => Err(format!("expected a track in \"{}\"", self.text)),
            "fit_content" | "min_max" => Err(format!(
                "unknown track \"{token}\" (write {})",
                token.replace('_', "-").replace("min-max", "minmax")
            )),
            _ => {
                // A unit standing apart from its number: "1 fr".
                let after = self.at;
                if parse_number(token).is_some() && self.skip_spaces() {
                    let mark = self.at;
                    let unit = self.token();
                    if matches!(unit, "fr" | "px" | "%") {
                        return Err(unknown_track(&self.text[start..self.at]));
                    }
                    self.at = mark;
                }
                self.at = after;
                TrackBreadth::parse(token, true).map(TrackSize::Breadth)
            }
        }
    }

    fn breadth(&mut self, max: bool) -> Result<TrackBreadth, String> {
        self.skip_spaces();
        let token = self.token();
        if token.is_empty() {
            return Err(format!("expected a track size in \"{}\"", self.text));
        }
        TrackBreadth::parse(token, max)
    }

    fn repeat(&mut self) -> Result<TrackComponent, String> {
        self.skip_spaces();
        let token = self.token();
        let count = match token {
            "auto-fill" => RepeatCount::AutoFill,
            "auto-fit" => RepeatCount::AutoFit,
            "auto_fill" | "auto_fit" => {
                return Err(format!(
                    "unknown repeat count \"{token}\" (write {})",
                    token.replace('_', "-")
                ));
            }
            _ => token
                .parse::<u16>()
                .ok()
                .filter(|_| token.bytes().all(|byte| byte.is_ascii_digit()))
                .map(RepeatCount::Count)
                .ok_or_else(|| format!("unknown repeat count \"{token}\""))?,
        };
        self.expect(',')?;
        let mut tracks = Vec::new();
        loop {
            if !self.skip_spaces() {
                return Err(format!("expected \")\" in \"{}\"", self.text));
            }
            if self.rest().starts_with(')') {
                self.at += 1;
                break;
            }
            if self.rest().starts_with("repeat(") {
                return Err("repeat() cannot hold repeat()".to_owned());
            }
            tracks.push(self.track()?);
        }
        if tracks.is_empty() {
            return Err("repeat() needs at least one track".to_owned());
        }
        Ok(TrackComponent::Repeat(count, tracks))
    }
}

impl<'de> Deserialize<'de> for TrackList {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let text = String::deserialize(deserializer)?;
        Self::parse(&text).map_err(de::Error::custom)
    }
}

impl Serialize for TrackList {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.collect_str(self)
    }
}

/// One edge of a grid item's placement.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub enum GridEdge {
    /// Placed automatically.
    #[default]
    Auto,
    /// At a line: 1 is the first, -1 the last explicit line.
    Line(i16),
    /// So many tracks from the other edge.
    Span(u16),
}

impl GridEdge {
    fn parse(text: &str) -> Result<Self, String> {
        let words = tokens(text).collect::<Vec<_>>();
        let integer = |word: &str| {
            let digits = word.strip_prefix('-').unwrap_or(word);
            (!digits.is_empty() && digits.bytes().all(|byte| byte.is_ascii_digit()))
                .then(|| word.parse::<i16>().ok())
                .flatten()
        };
        match *words {
            ["auto"] => Ok(Self::Auto),
            ["span", count] => integer(count)
                .and_then(|count| u16::try_from(count).ok())
                .map(Self::Span)
                .ok_or_else(|| invalid_line(text)),
            [line] => integer(line)
                .map(Self::Line)
                .ok_or_else(|| invalid_line(text)),
            _ => Err(invalid_line(text)),
        }
    }
}

fn invalid_line(text: &str) -> String {
    let text = text.trim_matches(' ');
    let named = text
        .split([' ', '/'])
        .filter(|word| !word.is_empty())
        .any(|word| {
            word != "span"
                && word != "auto"
                && word
                    .bytes()
                    .next()
                    .is_some_and(|byte| byte.is_ascii_alphabetic())
        });
    if named {
        format!("named grid lines are not supported: \"{text}\"")
    } else {
        format!("invalid grid line \"{text}\": write a line number, \"span <n>\", or \"auto\"")
    }
}

impl fmt::Display for GridEdge {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Auto => formatter.write_str("auto"),
            Self::Line(line) => write!(formatter, "{line}"),
            Self::Span(count) => write!(formatter, "span {count}"),
        }
    }
}

/// Where a grid item lies on one axis, as `grid_column` writes it in CSS:
/// `2`, `"1 / 3"`, `"span 2"`, `"2 / span 3"`, `"auto"`. A missing end is
/// `auto`.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct GridPlacement {
    /// The start edge.
    pub start: GridEdge,
    /// The end edge.
    pub end: GridEdge,
}

impl GridPlacement {
    /// Every edge of it.
    #[must_use]
    pub const fn edges(self) -> [GridEdge; 2] {
        [self.start, self.end]
    }

    fn parse(text: &str) -> Result<Self, String> {
        let (start, end) = match text.split_once('/') {
            Some((start, end)) => (start, Some(end)),
            None => (text, None),
        };
        if end.is_some_and(|end| end.contains('/')) {
            return Err(invalid_line(text));
        }
        let edge = |text| GridEdge::parse(text).map_err(|_| invalid_line(text));
        Ok(Self {
            start: edge(start)?,
            end: end.map_or(Ok(GridEdge::Auto), edge)?,
        })
    }
}

impl fmt::Display for GridPlacement {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.end {
            GridEdge::Auto => write!(formatter, "{}", self.start),
            end => write!(formatter, "{} / {end}", self.start),
        }
    }
}

struct PlacementVisitor;

impl Visitor<'_> for PlacementVisitor {
    type Value = GridPlacement;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a grid line: a line number, \"1 / 3\", \"span 2\", or \"auto\"")
    }

    fn visit_u64<E: de::Error>(self, value: u64) -> Result<GridPlacement, E> {
        i16::try_from(value)
            .map(|line| GridPlacement {
                start: GridEdge::Line(line),
                end: GridEdge::Auto,
            })
            .map_err(|_| E::custom(invalid_line(&value.to_string())))
    }

    fn visit_i64<E: de::Error>(self, value: i64) -> Result<GridPlacement, E> {
        i16::try_from(value)
            .map(|line| GridPlacement {
                start: GridEdge::Line(line),
                end: GridEdge::Auto,
            })
            .map_err(|_| E::custom(invalid_line(&value.to_string())))
    }

    /// A JSON number written with a fraction is a line when it is whole, as
    /// a JavaScript number is.
    #[allow(clippy::cast_possible_truncation)]
    fn visit_f64<E: de::Error>(self, value: f64) -> Result<GridPlacement, E> {
        if value.fract() == 0.0 && (f64::from(i16::MIN)..=f64::from(i16::MAX)).contains(&value) {
            return Ok(GridPlacement {
                start: GridEdge::Line(value as i16),
                end: GridEdge::Auto,
            });
        }
        Err(E::custom(invalid_line(&value.to_string())))
    }

    fn visit_str<E: de::Error>(self, value: &str) -> Result<GridPlacement, E> {
        GridPlacement::parse(value).map_err(E::custom)
    }
}

impl<'de> Deserialize<'de> for GridPlacement {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        deserializer.deserialize_any(PlacementVisitor)
    }
}

impl Serialize for GridPlacement {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match (self.start, self.end) {
            (GridEdge::Line(line), GridEdge::Auto) => serializer.serialize_i16(line),
            _ => serializer.collect_str(self),
        }
    }
}

#[cfg(feature = "schema")]
mod schema {
    //! The schema of each form: what JSON it takes. A string's grammar is
    //! the parser's; the patterns say it where a regular expression can.

    use super::{GridPlacement, Insets, LayoutLength, LayoutOffset, LayoutSize, TrackList};
    use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
    use std::borrow::Cow;

    const NUMBER: &str = r"-?(\d+\.?\d*|\.\d+)";

    fn size_pattern() -> String {
        format!("^ *{NUMBER}(px)? *$")
    }

    fn length_pattern() -> String {
        format!("^ *{NUMBER}(px|%)? *$")
    }

    fn sides_pattern() -> String {
        format!("^ *{NUMBER}(px)?( +{NUMBER}(px)?){{0,3}} *$")
    }

    impl JsonSchema for LayoutLength {
        fn schema_name() -> Cow<'static, str> {
            "LayoutLength".into()
        }

        fn json_schema(_: &mut SchemaGenerator) -> Schema {
            json_schema!({
                "description": "A length: a number is a fraction of the layout picture's shorter side, \"<n>px\" pixels of the project canvas, \"<n>%\" a percent of the parent's content box along the field's axis.",
                "anyOf": [
                    { "type": "number", "format": "float" },
                    { "type": "string", "pattern": length_pattern() },
                ],
            })
        }
    }

    impl JsonSchema for LayoutSize {
        fn schema_name() -> Cow<'static, str> {
            "LayoutSize".into()
        }

        fn json_schema(_: &mut SchemaGenerator) -> Schema {
            json_schema!({
                "description": "A length that may not be a percent: a number is a fraction of the layout picture's shorter side, \"<n>px\" pixels of the project canvas.",
                "anyOf": [
                    { "type": "number", "format": "float" },
                    { "type": "string", "pattern": size_pattern() },
                ],
            })
        }
    }

    impl JsonSchema for Insets {
        fn schema_name() -> Cow<'static, str> {
            "Insets".into()
        }

        fn json_schema(generator: &mut SchemaGenerator) -> Schema {
            let side = generator.subschema_for::<LayoutSize>();
            json_schema!({
                "description": "A length on each side of a box: one length for every side, the CSS shorthand of one to four lengths (\"8px 16px\": top first, clockwise), or the sides object, missing sides 0.",
                "anyOf": [
                    { "type": "number", "format": "float" },
                    { "type": "string", "pattern": sides_pattern() },
                    {
                        "type": "object",
                        "properties": {
                            "top": side,
                            "right": side,
                            "bottom": side,
                            "left": side,
                        },
                        "additionalProperties": false,
                    },
                ],
            })
        }
    }

    impl JsonSchema for LayoutOffset {
        fn schema_name() -> Cow<'static, str> {
            "LayoutOffset".into()
        }

        fn json_schema(generator: &mut SchemaGenerator) -> Schema {
            let side = generator.subschema_for::<LayoutSize>();
            json_schema!({
                "description": "An offset, x right and y down: one or two lengths as CSS translate writes them (y 0 when missing), or {x, y}.",
                "anyOf": [
                    {
                        "type": "string",
                        "pattern": format!("^ *{NUMBER}(px)?( +{NUMBER}(px)?)? *$"),
                    },
                    {
                        "type": "object",
                        "properties": { "x": side, "y": side },
                        "required": ["x", "y"],
                        "additionalProperties": false,
                    },
                ],
            })
        }
    }

    impl JsonSchema for TrackList {
        fn schema_name() -> Cow<'static, str> {
            "TrackList".into()
        }

        fn json_schema(_: &mut SchemaGenerator) -> Schema {
            json_schema!({
                "description": "A grid track list as CSS writes it: \"1fr 2fr 120px\", \"repeat(3, minmax(100px, 1fr))\", \"repeat(auto-fill, 160px)\", \"auto min-content max-content\", \"fit-content(200px)\". A bare number is a fraction of the picture's shorter side. Named lines are not supported.",
                "type": "string",
            })
        }
    }

    impl JsonSchema for GridPlacement {
        fn schema_name() -> Cow<'static, str> {
            "GridPlacement".into()
        }

        fn json_schema(_: &mut SchemaGenerator) -> Schema {
            json_schema!({
                "description": "Where a grid item lies on one axis, as CSS writes it: 2, \"1 / 3\", \"span 2\", \"2 / span 3\", \"auto\". Lines count from 1, or from -1 at the end.",
                "anyOf": [
                    { "type": "integer", "minimum": -32768, "maximum": 32767 },
                    { "type": "string" },
                ],
            })
        }
    }
}

#[cfg(test)]
#[path = "layout_values_tests.rs"]
mod tests;