mxblocks 0.1.1

Rich message blocks (buttons, tables, charts, forms, ...) for Matrix bots: types, validation and the content-key compat rules
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
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
//! mxblocks: structured extras ("blocks") on an ordinary Matrix
//! `m.room.message` (buttons, chips, tables, charts, cards, progress, ...),
//! under the content key [`RICH_KEY`]. Clients that know the key (the
//! Element Web module in `packages/mxblocks-element/`) render it; everyone else sees
//! `body`/`formatted_body` and can use reactions, so the text must stand
//! alone. Normative spec: `docs/spec.md`.
//!
//! ```json
//! "com.ianm.mtrix": {
//!   "v": 1,
//!   "blocks": [
//!     { "type": "buttons", "buttons": [
//!       { "label": "Send", "style": "primary", "action": { "type": "react", "key": "✅" } },
//!       { "label": "Drop", "style": "danger",  "action": { "type": "react", "key": "❌" } }
//!     ] },
//!     { "type": "table", "columns": ["item", "price"], "rows": [["A", 10]] }
//!   ]
//! }
//! ```
//!
//! A `react` action makes the client send an `m.reaction` annotation with
//! `key` on the message; a `send` action sends `body` as a message from the
//! clicking user. Action blocks (`buttons`, `chips`) are typed. Display
//! blocks ([`DISPLAY_TYPES`]) are carried as JSON and only checked for their
//! required fields and size; the renderer parses them defensively anyway.
//! Unknown block types load as [`Block::Unknown`] (so newer payloads still
//! deserialize) but don't [`Rich::validate`].
//!
//! A `stream` block marks a reply the sender is still writing:
//! `{"type": "stream", "id": "<id>", "url": "https://…/stream/<id>",
//! "state": "live"}`, `"done"` in the final edit. The renderer may follow
//! `url` (SSE) while it is live; the body is the fallback, kept current by
//! edits.
//!
//! # Content keys
//!
//! Default readers accept [`RICH_KEYS`]. Writers use [`WriteKey`] (including
//! caller-selected keys), while [`ContentKeys`] configures both the write key
//! and the exact read aliases. Applications can choose their own namespace.

#![warn(missing_docs)]

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{Map, Value};

mod form;
mod media;
mod number;

pub use form::{
    FORM_KINDS, FORM_REPLY_FORMAT, MAX_FORM_FIELDS, MAX_FORM_OPTIONS, MAX_FORM_TEXT,
    MAX_OPTION_VALUE, form_fallback,
};
pub use media::{
    MAX_CAPTION, MAX_FILE_NAME, MAX_GALLERY, MAX_PREVIEW_BYTES, MAX_PREVIEW_LINES, MIN_GALLERY,
    image_size, is_mxc, text_preview,
};
pub use number::{
    MAX_SAFE_INT, canonicalize as canonicalize_value, is_decimal, is_num, non_canonical,
};

mod keys;
pub use keys::{
    ALBUM_KEYS, ContentKeys, RICH_KEY, RICH_KEY_NEW, RICH_KEYS, USED_KEYS, WriteKey, read_album,
    read_rich, read_used, rich_value, strip_rich,
};

/// The only payload version so far.
pub const RICH_VERSION: u32 = 1;

/// Blocks in one payload.
pub const MAX_BLOCKS: usize = 8;
/// Buttons in one `buttons` block.
pub const MAX_BUTTONS: usize = 8;
/// Chips in one `chips` block.
pub const MAX_CHIPS: usize = 12;
/// Button, chip and form labels, in characters: a whole sentence, which
/// the renderer wraps.
pub const MAX_LABEL: usize = 120;
/// A reaction key, in characters.
pub const MAX_KEY: usize = 32;
/// In UTF-16 code units, as the Element module (JS `length`) counts.
pub const MAX_SEND_BODY: usize = 1000;
/// Reaction seeds on one message.
pub const MAX_SEEDS: usize = 8;
/// Serialized size of one display block, in bytes.
pub const MAX_BLOCK_BYTES: usize = 24_000;
/// Serialized size of the whole payload, in bytes (Matrix caps the whole
/// event at 65536, and `body`/`formatted_body` need room too).
pub const MAX_PAYLOAD_BYTES: usize = 40_000;

/// JSON block types (display blocks, plus `form`), with the fields each must
/// have (`docs/spec.md`).
pub const DISPLAY_TYPES: &[(&str, &[(&str, Kind)])] = &[
    ("table", &[("columns", Kind::Array), ("rows", Kind::Array)]),
    ("chart", &[("kind", Kind::String), ("series", Kind::Array)]),
    ("sources", &[("sources", Kind::Array)]),
    ("card", &[("title", Kind::String)]),
    ("cards", &[("cards", Kind::Array)]),
    ("progress", &[("steps", Kind::Array)]),
    (
        "details",
        &[("summary", Kind::String), ("body", Kind::String)],
    ),
    ("code", &[("code", Kind::String)]),
    ("diff", &[("diff", Kind::String)]),
    ("countdown", &[("until", Kind::Time)]),
    ("event", &[("title", Kind::String), ("start", Kind::Time)]),
    ("place", &[("name", Kind::String)]),
    (
        "ticker",
        &[("symbol", Kind::String), ("price", Kind::Number)],
    ),
    (
        "weather",
        &[("location", Kind::String), ("temp", Kind::Number)],
    ),
    ("math", &[("tex", Kind::String)]),
    (
        "tracker",
        &[
            ("title", Kind::String),
            ("provider", Kind::String),
            ("status", Kind::String),
        ],
    ),
    ("gallery", &[("images", Kind::Array)]),
    (
        "form",
        &[
            ("id", Kind::String),
            ("fields", Kind::Array),
            ("submit", Kind::Object),
        ],
    ),
    ("file", &[("name", Kind::String)]),
    ("stream", &[("id", Kind::String), ("state", Kind::String)]),
];

/// A `stream` block's state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum StreamState {
    /// Still being written: the body is a partial snapshot.
    Live,
    /// Finished: the body is the final text.
    Done,
    /// A state this version doesn't know.
    #[serde(other)]
    Unknown,
}

/// Longest `stream` block id.
pub const MAX_STREAM_ID: usize = 64;

/// `math.tex`, in characters (the reference math renderer refuses longer input too).
pub const MAX_TEX: usize = 4000;
/// `math.alt` / `math.caption`, in characters.
pub const MAX_MATH_LINE: usize = 200;

/// The JSON kind a required display-block field must have.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
    /// A non-blank string.
    String,
    /// A number: an integer in ±(2^53 - 1) or a decimal string ([`is_num`]).
    Number,
    /// A non-empty array.
    Array,
    /// ms since the epoch (number) or an ISO 8601 string
    Time,
    /// A JSON object.
    Object,
}

impl Kind {
    fn matches(self, v: &Value) -> bool {
        match self {
            Kind::String => v.as_str().is_some_and(|s| !s.trim().is_empty()),
            Kind::Number => is_num(v),
            Kind::Array => v.as_array().is_some_and(|a| !a.is_empty()),
            Kind::Time => is_num(v) || v.is_string(),
            Kind::Object => v.is_object(),
        }
    }
}

const CHART_KINDS: &[&str] = &["line", "bar", "area", "sparkline"];
/// `tracker.kind`.
pub const TRACKER_KINDS: &[&str] = &["food", "ride", "delivery", "parcel"];
/// `tracker.state`; the final edit sets `done` or `cancelled`.
pub const TRACKER_STATES: &[&str] = &["live", "done", "cancelled"];
/// `progress.steps[].status` and `tracker.stages[].status`.
pub const STEP_STATUSES: &[&str] = &["pending", "running", "done", "failed", "skipped"];
/// `tracker.stages`.
pub const MAX_TRACKER_STAGES: usize = 12;
/// `tracker.items`.
pub const MAX_TRACKER_ITEMS: usize = 20;

/// A rich payload: the object stored under the content key
/// (`{"v":1,"blocks":[...]}`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Rich {
    /// Payload version ([`RICH_VERSION`]).
    pub v: u32,
    /// The blocks, drawn in order under the message.
    pub blocks: Vec<Block>,
}

/// One entry of `blocks`, tagged by its `type` field on the wire.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Block {
    /// `{"type":"buttons","buttons":[...]}`
    Buttons(Prompts),
    /// `{"type":"chips","chips":[...]}`: quick-reply chips, same items as
    /// buttons (`style` ignored).
    Chips(Prompts),
    /// A display block of a type in [`DISPLAY_TYPES`] at block version 1,
    /// kept as the JSON object (including `type`).
    Display(Map<String, Value>),
    /// A block this version can't check (unknown type, or a block `v` above
    /// [`BLOCK_VERSION`]), kept as-is. Sendable only with a `fallback`.
    Unknown(Value),
}

/// The items of a `buttons`/`chips` block plus the fields every block may
/// carry. Shaped after MSC4139 `m.prompts`: the items are preset prompts with
/// stable ids, `scope` lists who may answer.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Prompts {
    /// The buttons or chips, in order.
    pub items: Vec<Button>,
    /// MXIDs allowed to click; `None` = anyone who can see the message.
    pub scope: Option<Vec<String>>,
    /// Block version (absent = 1).
    pub v: Option<u32>,
    /// Plain text shown by renderers that can't draw this block.
    pub fallback: Option<String>,
}

impl Prompts {
    /// These items, with no scope, version or fallback.
    pub fn new(items: Vec<Button>) -> Self {
        Self {
            items,
            ..Self::default()
        }
    }
}

#[derive(Serialize, Deserialize)]
struct PromptsWire {
    #[serde(rename = "type")]
    ty: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    buttons: Option<Vec<Button>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    chips: Option<Vec<Button>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    scope: Option<Vec<String>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    v: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    fallback: Option<String>,
}

/// The block version every block type is at.
pub const BLOCK_VERSION: u64 = 1;
/// A block's `fallback` text, in characters.
pub const MAX_FALLBACK: usize = 500;
/// `card.actions` (ChatGPT Apps SDK: an inline card has at most 2 actions).
pub const MAX_CARD_ACTIONS: usize = 2;
/// Items in `cards` (ChatGPT inline carousel: 3 to 8; fewer render as single cards).
pub const MAX_CAROUSEL: usize = 8;
/// Actions per carousel item.
pub const MAX_CAROUSEL_ACTIONS: usize = 1;
/// `scope` entries.
pub const MAX_SCOPE: usize = 50;

impl Serialize for Block {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        let prompts = |ty: &str, p: &Prompts, chips: bool| PromptsWire {
            ty: ty.to_owned(),
            buttons: (!chips).then(|| p.items.clone()),
            chips: chips.then(|| p.items.clone()),
            scope: p.scope.clone(),
            v: p.v,
            fallback: p.fallback.clone(),
        };
        match self {
            Block::Buttons(p) => prompts("buttons", p, false).serialize(s),
            Block::Chips(p) => prompts("chips", p, true).serialize(s),
            Block::Display(m) => m.serialize(s),
            Block::Unknown(v) => v.serialize(s),
        }
    }
}

impl<'de> Deserialize<'de> for Block {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let v = Value::deserialize(d)?;
        let ty = v.get("type").and_then(Value::as_str).unwrap_or_default();
        // A higher block version than we know: opaque, like an unknown type.
        let newer = v
            .get("v")
            .is_some_and(|x| x.as_u64().is_none_or(|n| n > BLOCK_VERSION));
        if newer {
            return Ok(Block::Unknown(v));
        }
        Ok(match ty {
            "buttons" | "chips" => {
                let w: PromptsWire = serde_json::from_value(v).map_err(serde::de::Error::custom)?;
                let chips = w.ty == "chips";
                let p = Prompts {
                    items: if chips { w.chips } else { w.buttons }.ok_or_else(|| {
                        serde::de::Error::custom(format!("a {} block needs {:?}", w.ty, w.ty))
                    })?,
                    scope: w.scope,
                    v: w.v,
                    fallback: w.fallback,
                };
                if chips {
                    Block::Chips(p)
                } else {
                    Block::Buttons(p)
                }
            }
            t if DISPLAY_TYPES.iter().any(|(name, _)| *name == t) => match v {
                Value::Object(m) => Block::Display(m),
                _ => unreachable!("a value with a type field is an object"),
            },
            _ => Block::Unknown(v),
        })
    }
}

impl Block {
    /// The wire `type`.
    pub fn kind(&self) -> &str {
        match self {
            Block::Buttons(_) => "buttons",
            Block::Chips(_) => "chips",
            Block::Display(m) => m.get("type").and_then(Value::as_str).unwrap_or_default(),
            Block::Unknown(v) => v.get("type").and_then(Value::as_str).unwrap_or("?"),
        }
    }

    /// Every clickable action: buttons, chips, and `card`/`cards` actions.
    pub fn actions(&self) -> Vec<Button> {
        match self {
            Block::Buttons(p) | Block::Chips(p) => p.items.clone(),
            Block::Display(m) => action_lists(m)
                .into_iter()
                .filter_map(|(_, a)| serde_json::from_value::<Vec<Button>>(a?.clone()).ok())
                .flatten()
                .collect(),
            Block::Unknown(_) => Vec::new(),
        }
    }

    /// The MSC4139 prompt ids in this block: button and card-action ids, and
    /// a form's id.
    pub fn prompt_ids(&self) -> Vec<String> {
        let mut ids: Vec<String> = self.actions().into_iter().filter_map(|b| b.id).collect();
        if let Block::Display(m) = self
            && self.kind() == "form"
            && let Some(id) = m.get("id").and_then(Value::as_str)
        {
            ids.push(id.to_owned());
        }
        ids
    }

    fn validate(&self) -> Result<(), String> {
        let floats = match self {
            Block::Display(m) => number::non_canonical_map(m, self.kind()),
            Block::Unknown(v) => non_canonical(v, self.kind()),
            _ => None,
        };
        if let Some(at) = floats {
            return Err(format!(
                "{at}: event content is canonical JSON, so no floats (use a decimal \
                 string like \"62.99\") and integers within ±{MAX_SAFE_INT}"
            ));
        }
        match self {
            Block::Buttons(p) => p.validate("buttons", MAX_BUTTONS),
            Block::Chips(p) => p.validate("chips", MAX_CHIPS),
            Block::Display(m) => validate_display(m),
            Block::Unknown(v) => {
                if v.get("fallback")
                    .and_then(Value::as_str)
                    .is_some_and(|f| !f.trim().is_empty())
                {
                    validate_common(v.as_object(), self.kind())?;
                    return check_size(v, self.kind());
                }
                let known: Vec<&str> = ["buttons", "chips"]
                    .into_iter()
                    .chain(DISPLAY_TYPES.iter().map(|(n, _)| *n))
                    .collect();
                Err(format!(
                    "unsupported block type {} or block version {} (known types: {}, \
                     version {BLOCK_VERSION}); add a \"fallback\" text to send it anyway",
                    v.get("type").map_or("(none)".to_owned(), Value::to_string),
                    v.get("v").map_or("1".to_owned(), Value::to_string),
                    known.join(", ")
                ))
            }
        }
    }
}

impl Prompts {
    fn validate(&self, ty: &str, max: usize) -> Result<(), String> {
        if self.items.is_empty() || self.items.len() > max {
            return Err(format!("a {ty} block has 1 to {max} {ty}"));
        }
        self.items.iter().try_for_each(Button::validate)?;
        validate_scope(self.scope.as_deref(), ty)?;
        if let Some(f) = &self.fallback {
            validate_fallback(f, ty)?;
        }
        Ok(())
    }
}

fn validate_scope(scope: Option<&[String]>, ty: &str) -> Result<(), String> {
    let Some(scope) = scope else { return Ok(()) };
    if scope.len() > MAX_SCOPE {
        return Err(format!("{ty} block: at most {MAX_SCOPE} users in scope"));
    }
    for id in scope {
        let ok = id
            .strip_prefix('@')
            .and_then(|r| r.split_once(':'))
            .is_some_and(|(l, s)| !l.is_empty() && !s.is_empty())
            && !id.chars().any(char::is_whitespace);
        if !ok {
            return Err(format!("{ty} block: scope entry {id:?} isn't a user id"));
        }
    }
    Ok(())
}

fn validate_fallback(f: &str, ty: &str) -> Result<(), String> {
    if f.trim().is_empty() || f.chars().count() > MAX_FALLBACK {
        return Err(format!(
            "{ty} block: fallback has 1 to {MAX_FALLBACK} characters"
        ));
    }
    Ok(())
}

/// `v`, `fallback` and `scope` when present on a JSON block.
fn validate_common(m: Option<&Map<String, Value>>, ty: &str) -> Result<(), String> {
    let Some(m) = m else {
        return Err("a block is a JSON object".into());
    };
    if let Some(f) = m.get("fallback") {
        validate_fallback(
            f.as_str().ok_or(format!("{ty} block: fallback is text"))?,
            ty,
        )?;
    }
    if let Some(sc) = m.get("scope") {
        let list: Vec<String> = serde_json::from_value(sc.clone())
            .map_err(|_| format!("{ty} block: scope is a list of user ids"))?;
        validate_scope(Some(&list), ty)?;
    }
    Ok(())
}

fn check_size(v: &impl Serialize, ty: &str) -> Result<(), String> {
    let n = serde_json::to_string(v).map_or(usize::MAX, |s| s.len());
    if n > MAX_BLOCK_BYTES {
        return Err(format!(
            "{ty} block: {n} bytes of JSON, at most {MAX_BLOCK_BYTES}"
        ));
    }
    Ok(())
}

/// The raw `actions` list of a `card`, or of each `cards` item, with the cap
/// that applies (ChatGPT Apps SDK: 2 on an inline card, 1 per carousel item).
fn action_lists(m: &Map<String, Value>) -> Vec<(usize, Option<&Value>)> {
    match m.get("type").and_then(Value::as_str) {
        Some("card") => vec![(MAX_CARD_ACTIONS, m.get("actions"))],
        Some("cards") => m
            .get("cards")
            .and_then(Value::as_array)
            .into_iter()
            .flatten()
            .map(|c| (MAX_CAROUSEL_ACTIONS, c.get("actions")))
            .collect(),
        _ => Vec::new(),
    }
}

/// Required fields present with the right JSON kind, a known chart kind,
/// and at most [`MAX_BLOCK_BYTES`] serialized.
fn validate_display(m: &Map<String, Value>) -> Result<(), String> {
    let ty = m.get("type").and_then(Value::as_str).unwrap_or_default();
    let (_, fields) = DISPLAY_TYPES
        .iter()
        .find(|(n, _)| *n == ty)
        .ok_or_else(|| format!("unsupported block type {ty:?}"))?;
    for (field, kind) in *fields {
        if !m.get(*field).is_some_and(|v| kind.matches(v)) {
            let want = match kind {
                Kind::String => "a non-empty string",
                Kind::Number => "an integer or a decimal string (\"62.99\")",
                Kind::Array => "a non-empty array",
                Kind::Time => "ms since the epoch or an ISO 8601 string",
                Kind::Object => "an object",
            };
            return Err(format!("{ty} block: {field:?} must be {want}"));
        }
    }
    if ty == "chart" {
        let k = m.get("kind").and_then(Value::as_str).unwrap_or_default();
        if !CHART_KINDS.contains(&k) {
            return Err(format!(
                "chart block: kind {k:?} isn't one of {}",
                CHART_KINDS.join(", ")
            ));
        }
    }
    if ty == "math" {
        validate_math(m)?;
    }
    if ty == "stream" {
        validate_stream(m)?;
    }
    if ty == "cards" {
        let n = m.get("cards").and_then(Value::as_array).map_or(0, Vec::len);
        if n > MAX_CAROUSEL {
            return Err(format!(
                "cards block: at most {MAX_CAROUSEL} cards (a carousel shows 3 to 8)"
            ));
        }
    }
    match ty {
        "tracker" => validate_tracker(m)?,
        "gallery" => media::validate_gallery(m)?,
        "file" => media::validate_file(m)?,
        "form" => form::validate_form(m)?,
        _ => {}
    }
    for (max, raw) in action_lists(m) {
        let Some(raw) = raw else { continue };
        let acts: Vec<Button> = serde_json::from_value(raw.clone())
            .map_err(|e| format!("{ty} block: bad actions: {e}"))?;
        if acts.is_empty() || acts.len() > max {
            let what = if ty == "card" {
                "card"
            } else {
                "carousel item"
            };
            return Err(format!("{ty} block: 1 to {max} actions per {what}"));
        }
        acts.iter().try_for_each(Button::validate)?;
    }
    validate_common(Some(m), ty)?;
    check_size(m, ty)
}

/// A stream block: an id of 1..=[`MAX_STREAM_ID`] of `A-Z a-z 0-9 _ -`,
/// an `https:` url (if any) and a known state.
fn validate_stream(m: &Map<String, Value>) -> Result<(), String> {
    let id = m.get("id").and_then(Value::as_str).unwrap_or_default();
    if id.is_empty()
        || id.len() > MAX_STREAM_ID
        || !id
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
    {
        return Err(format!(
            "stream id {id:?}: 1 to {MAX_STREAM_ID} of A-Z a-z 0-9 _ -"
        ));
    }
    if let Some(u) = m.get("url")
        && !u.as_str().is_some_and(|u| u.starts_with("https://"))
    {
        return Err(format!("stream url {u}: https only"));
    }
    if !matches!(
        m.get("state").and_then(Value::as_str),
        Some("live" | "done")
    ) {
        return Err("stream state: live or done".into());
    }
    Ok(())
}

/// `math`: `tex` up to [`MAX_TEX`] characters without NULs, `display` a
/// boolean, `alt` / `caption` one line of up to [`MAX_MATH_LINE`].
fn validate_math(m: &Map<String, Value>) -> Result<(), String> {
    let tex = m.get("tex").and_then(Value::as_str).unwrap_or_default();
    if tex.chars().count() > MAX_TEX {
        return Err(format!("math block: tex has at most {MAX_TEX} characters"));
    }
    if tex.contains('\0') {
        return Err("math block: tex contains a NUL".into());
    }
    if m.get("display").is_some_and(|d| !d.is_boolean()) {
        return Err("math block: display is true or false".into());
    }
    for field in ["alt", "caption"] {
        let Some(v) = m.get(field) else { continue };
        let ok = v.as_str().is_some_and(|s| {
            !s.trim().is_empty() && s.chars().count() <= MAX_MATH_LINE && !s.contains('\n')
        });
        if !ok {
            return Err(format!(
                "math block: {field} is one line of 1 to {MAX_MATH_LINE} characters"
            ));
        }
    }
    Ok(())
}

/// A math block's TeX and whether it is display math (the default).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Math {
    /// The TeX source, without delimiters.
    pub tex: String,
    /// Display (block) math when true, inline math when false.
    pub display: bool,
}

impl Math {
    /// MSC2191 fallback HTML: `<div data-mx-maths="…"><code>…</code></div>`
    /// for display math, `<span …>` for inline. Clients with LaTeX support
    /// (Element's labs flag) render the attribute; others show the code.
    pub fn html(&self) -> String {
        let t = html_escape(&self.tex);
        if self.display {
            format!("<div data-mx-maths=\"{t}\"><code>{t}</code></div>")
        } else {
            format!("<span data-mx-maths=\"{t}\"><code>{t}</code></span>")
        }
    }
}

/// Escapes text for an HTML text node or a double-quoted attribute.
pub fn html_escape(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            '\'' => out.push_str("&#39;"),
            c => out.push(c),
        }
    }
    out
}

/// `tracker` beyond its required fields: the enums, stage shape, the
/// `mxc://` map image, an https link and the list caps.
fn validate_tracker(m: &Map<String, Value>) -> Result<(), String> {
    let err = |what: &str| Err(format!("tracker block: {what}"));
    let one_of = |field: &str, allowed: &[&str]| -> Result<(), String> {
        match m.get(field) {
            None => Ok(()),
            Some(v) if v.as_str().is_some_and(|s| allowed.contains(&s)) => Ok(()),
            Some(v) => Err(format!(
                "tracker block: {field} {v} isn't one of {}",
                allowed.join(", ")
            )),
        }
    };
    one_of("kind", TRACKER_KINDS)?;
    one_of("state", TRACKER_STATES)?;
    for f in ["eta", "updated_at"] {
        if m.get(f).is_some_and(|v| !Kind::Time.matches(v)) {
            return err(&format!(
                "{f:?} must be ms since the epoch or an RFC 3339 string"
            ));
        }
    }
    if let Some(stages) = m.get("stages") {
        let Some(stages) = stages.as_array().filter(|a| a.len() <= MAX_TRACKER_STAGES) else {
            return err(&format!("stages is a list of at most {MAX_TRACKER_STAGES}"));
        };
        for s in stages {
            let label = s.get("label").and_then(Value::as_str).unwrap_or_default();
            let status = s.get("status").and_then(Value::as_str).unwrap_or_default();
            if label.trim().is_empty() || !STEP_STATUSES.contains(&status) {
                return err(&format!(
                    "each stage needs a label and a status ({})",
                    STEP_STATUSES.join(", ")
                ));
            }
        }
    }
    if let Some(map) = m.get("map") {
        let url = map.get("url").and_then(Value::as_str).unwrap_or_default();
        let ok = url
            .strip_prefix("mxc://")
            .and_then(|r| r.split_once('/'))
            .is_some_and(|(server, id)| {
                !server.is_empty()
                    && !id.is_empty()
                    && id
                        .chars()
                        .all(|c| c.is_ascii_alphanumeric() || "_-".contains(c))
            });
        if !ok {
            return err("map.url must be an mxc:// URI (upload the static map first)");
        }
    }
    if let Some(d) = m.get("driver")
        && !d
            .get("name")
            .and_then(Value::as_str)
            .is_some_and(|n| !n.trim().is_empty())
    {
        return err("driver needs a name");
    }
    if let Some(items) = m.get("items")
        && !items
            .as_array()
            .is_some_and(|a| a.len() <= MAX_TRACKER_ITEMS && a.iter().all(Value::is_string))
    {
        return err(&format!(
            "items is a list of at most {MAX_TRACKER_ITEMS} strings"
        ));
    }
    if let Some(t) = m.get("total")
        && !(t.get("amount").is_some_and(is_num)
            && t.get("currency").and_then(Value::as_str).is_some())
    {
        return err("total is {amount: integer or decimal string, currency: string}");
    }
    if let Some(l) = m.get("link")
        && !l.as_str().is_some_and(|u| u.starts_with("https://"))
    {
        return err("link must be an https URL");
    }
    Ok(())
}

impl Rich {
    /// One math block (`display` false: inline math).
    pub fn math(tex: &str, display: bool) -> Self {
        let mut m = Map::new();
        m.insert("type".into(), "math".into());
        m.insert("tex".into(), tex.into());
        m.insert("display".into(), display.into());
        Self {
            v: RICH_VERSION,
            blocks: vec![Block::Display(m)],
        }
    }

    /// The math of a payload whose only block is a `math` block: such a
    /// message is sent as an `m.image` (the PNG render) when it has no
    /// separate text. `None` for anything else.
    pub fn math_only(&self) -> Option<Math> {
        let [Block::Display(m)] = self.blocks.as_slice() else {
            return None;
        };
        if m.get("type").and_then(Value::as_str) != Some("math") {
            return None;
        }
        Some(Math {
            tex: m.get("tex")?.as_str()?.to_owned(),
            display: m.get("display").and_then(Value::as_bool).unwrap_or(true),
        })
    }

    /// The first math block's TeX and whether it is display math (the
    /// default), in any payload (unlike [`Rich::math_only`]).
    pub fn find_math(&self) -> Option<(&str, bool)> {
        self.blocks.iter().find_map(|b| match b {
            Block::Display(m) if m.get("type").and_then(Value::as_str) == Some("math") => Some((
                m.get("tex").and_then(Value::as_str).unwrap_or_default(),
                m.get("display").and_then(Value::as_bool).unwrap_or(true),
            )),
            _ => None,
        })
    }

    /// One stream block.
    pub fn stream(id: &str, url: Option<&str>, state: StreamState) -> Self {
        let mut m = Map::new();
        m.insert("type".into(), "stream".into());
        m.insert("id".into(), id.into());
        if let Some(u) = url {
            m.insert("url".into(), u.into());
        }
        m.insert(
            "state".into(),
            serde_json::to_value(state).unwrap_or(Value::Null),
        );
        Self {
            v: RICH_VERSION,
            blocks: vec![Block::Display(m)],
        }
    }

    /// One buttons block.
    pub fn buttons(buttons: Vec<Button>) -> Self {
        Self {
            v: RICH_VERSION,
            blocks: vec![Block::Buttons(Prompts::new(buttons))],
        }
    }

    /// Parses a payload, turns its floats into decimal strings
    /// ([`Rich::canonicalize`]) and validates it.
    pub fn parse(json: &str) -> Result<Self, String> {
        let mut r: Rich =
            serde_json::from_str(json).map_err(|e| format!("bad rich payload: {e}"))?;
        r.canonicalize()?;
        r.validate()?;
        Ok(r)
    }

    /// Makes the payload canonical JSON, which event content must be: every
    /// float becomes an integer (no fraction) or its shortest decimal string
    /// (`62.99` → `"62.99"`). Senders call this before [`Rich::validate`], so
    /// callers that write floats keep working. Integers outside
    /// ±[`MAX_SAFE_INT`] are an error.
    pub fn canonicalize(&mut self) -> Result<(), String> {
        for (i, b) in self.blocks.iter_mut().enumerate() {
            let path = format!("blocks[{i}]");
            match b {
                Block::Display(m) => number::canonicalize_map(m, &path)?,
                Block::Unknown(v) => number::canonicalize(v, &path)?,
                Block::Buttons(_) | Block::Chips(_) => {}
            }
        }
        Ok(())
    }

    /// Whether this version can send it: known `v`, 1..=[`MAX_BLOCKS`]
    /// blocks of known types, 1..=[`MAX_BUTTONS`] buttons (or
    /// 1..=[`MAX_CHIPS`] chips) per action block with labels of
    /// 1..=[`MAX_LABEL`] characters, non-empty react keys and send bodies,
    /// display blocks with their required fields, and the size caps.
    pub fn validate(&self) -> Result<(), String> {
        if self.v != RICH_VERSION {
            return Err(format!(
                "rich payload version {} isn't supported (only {RICH_VERSION})",
                self.v
            ));
        }
        if self.blocks.is_empty() || self.blocks.len() > MAX_BLOCKS {
            return Err(format!("a rich payload has 1 to {MAX_BLOCKS} blocks"));
        }
        for b in &self.blocks {
            b.validate()?;
        }
        if self.blocks.iter().filter(|b| b.kind() == "math").count() > 1 {
            return Err("at most one math block per message".into());
        }
        // Prompt ids (buttons, card actions, forms) are unique per message.
        let mut ids: Vec<String> = Vec::new();
        for id in self.blocks.iter().flat_map(Block::prompt_ids) {
            if ids.contains(&id) {
                return Err(format!("prompt id {id:?} used twice in one message"));
            }
            ids.push(id);
        }
        let n = serde_json::to_string(self).map_or(usize::MAX, |s| s.len());
        if n > MAX_PAYLOAD_BYTES {
            return Err(format!(
                "rich payload: {n} bytes of JSON, at most {MAX_PAYLOAD_BYTES}"
            ));
        }
        Ok(())
    }

    /// A plain-text `body` for the payload's forms ([`form_fallback`]), or
    /// `None` without forms: a sender's default text when it has none.
    pub fn form_fallback(&self) -> Option<String> {
        let forms: Vec<String> = self
            .blocks
            .iter()
            .filter_map(|b| match b {
                Block::Display(m) if b.kind() == "form" => Some(form_fallback(m)),
                _ => None,
            })
            .collect();
        (!forms.is_empty()).then(|| forms.join("\n\n"))
    }

    /// The keys of every `react` action (buttons, chips, card actions), in
    /// order, without duplicates.
    pub fn react_keys(&self) -> Vec<String> {
        let mut out: Vec<String> = Vec::new();
        for btn in self.blocks.iter().flat_map(Block::actions) {
            if let Action::React { key } = btn.action
                && !out.contains(&key)
            {
                out.push(key);
            }
        }
        out
    }

    /// Every user-visible string of the actions (labels, keys, send bodies).
    pub fn texts(&self) -> Vec<String> {
        let mut out = Vec::new();
        for btn in self.blocks.iter().flat_map(Block::actions) {
            out.push(btn.label);
            match btn.action {
                Action::React { key } => out.push(key),
                Action::Send { body } => out.push(body),
                Action::Unknown => {}
            }
        }
        out
    }
}

/// One button or chip: a label and what a click does.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Button {
    /// Stable per message (MSC4139 prompt `id`): 1 to 64 of `[A-Za-z0-9_.:-]`.
    /// Echoed as `com.ianm.mtrix.used.id` on `send` clicks.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// The text on the button, 1..=[`MAX_LABEL`] characters.
    pub label: String,
    /// How the button is drawn (ignored on chips).
    #[serde(default)]
    pub style: Style,
    /// What a click does.
    pub action: Action,
}

/// A button's look.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Style {
    /// The main, recommended choice.
    Primary,
    /// A destructive or risky choice.
    Danger,
    /// A plain button.
    #[default]
    Default,
}

/// What a button click does, tagged by `type` on the wire.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Action {
    /// React to the message with `key`.
    React {
        /// The reaction key, 1..=[`MAX_KEY`] characters, no whitespace.
        key: String,
    },
    /// Send `body` as the clicking user.
    Send {
        /// The message body, up to [`MAX_SEND_BODY`] UTF-16 code units.
        body: String,
    },
    /// An action type this version doesn't know.
    #[serde(other)]
    Unknown,
}

impl Button {
    /// A button that reacts with `key`.
    pub fn react(label: &str, style: Style, key: &str) -> Self {
        Self {
            id: None,
            label: label.to_owned(),
            style,
            action: Action::React {
                key: key.to_owned(),
            },
        }
    }

    /// A button that sends `body` as the clicking user.
    pub fn send(label: &str, style: Style, body: &str) -> Self {
        Self {
            id: None,
            label: label.to_owned(),
            style,
            action: Action::Send {
                body: body.to_owned(),
            },
        }
    }

    /// With a stable id (MSC4139 prompt id).
    pub fn with_id(mut self, id: &str) -> Self {
        self.id = Some(id.to_owned());
        self
    }

    fn validate(&self) -> Result<(), String> {
        if let Some(id) = &self.id {
            validate_id(id).map_err(|e| format!("button {e}"))?;
        }
        let n = self.label.trim().chars().count();
        if n == 0 || self.label.chars().count() > MAX_LABEL {
            return Err(format!(
                "button label {:?}: 1 to {MAX_LABEL} characters",
                self.label
            ));
        }
        match &self.action {
            Action::React { key } => {
                validate_key(key).map_err(|e| format!("button {:?}: {e}", self.label))?
            }
            Action::Send { body } => {
                if body.trim().is_empty() || body.encode_utf16().count() > MAX_SEND_BODY {
                    return Err(format!(
                        "button {:?}: a send body has 1 to {MAX_SEND_BODY} characters",
                        self.label
                    ));
                }
            }
            Action::Unknown => {
                return Err(format!(
                    "button {:?}: unsupported action type (react or send)",
                    self.label
                ));
            }
        }
        Ok(())
    }
}

/// An MSC4139 prompt id (buttons, forms, form fields): 1 to 64 of
/// `[A-Za-z0-9_.:-]`.
pub fn validate_id(id: &str) -> Result<(), String> {
    if id.is_empty()
        || id.len() > 64
        || !id
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || "_.:-".contains(c))
    {
        return Err(format!("id {id:?}: 1 to 64 of A-Z a-z 0-9 _ . : -"));
    }
    Ok(())
}

/// A reaction key: 1..=[`MAX_KEY`] characters, no whitespace.
pub fn validate_key(key: &str) -> Result<(), String> {
    if key.is_empty() || key.chars().count() > MAX_KEY || key.chars().any(char::is_whitespace) {
        return Err(format!(
            "reaction key {key:?}: 1 to {MAX_KEY} characters, no spaces"
        ));
    }
    Ok(())
}

/// Reaction seeds: valid keys, at most [`MAX_SEEDS`], no duplicates.
pub fn validate_seeds(keys: &[String]) -> Result<(), String> {
    if keys.len() > MAX_SEEDS {
        return Err(format!("at most {MAX_SEEDS} reaction seeds"));
    }
    for (i, k) in keys.iter().enumerate() {
        validate_key(k)?;
        if keys[..i].contains(k) {
            return Err(format!("reaction seed {k:?} given twice"));
        }
    }
    Ok(())
}

/// Every number in `v` (any JSON, e.g. a whole event content with its
/// `m.new_content`) made canonical: [`number::canonicalize`] from the top.
///
/// # Errors
/// An integer outside ±[`MAX_SAFE_INT`] or a non-finite number, naming where.
pub fn canonical_numbers(v: &mut Value) -> Result<(), String> {
    number::canonicalize(v, "content")
}

/// Whether `v` has a float anywhere (cheap check before [`canonical_numbers`]).
pub fn has_float(v: &Value) -> bool {
    match v {
        Value::Number(n) => n.is_f64(),
        Value::Array(a) => a.iter().any(has_float),
        Value::Object(m) => m.values().any(has_float),
        _ => false,
    }
}

/// `content` (a whole event content, `m.new_content` included) as
/// canonical JSON, ready to send: floats become integers or decimal strings
/// ([`canonical_numbers`]). Borrowed when it has no float; also borrowed,
/// unchanged, when it can't be fixed (an integer beyond ±[`MAX_SAFE_INT`]),
/// so the homeserver's own error reaches the caller.
pub fn canonical_content(content: &Value) -> std::borrow::Cow<'_, Value> {
    if !has_float(content) {
        return std::borrow::Cow::Borrowed(content);
    }
    let mut c = content.clone();
    match canonical_numbers(&mut c) {
        Ok(()) => std::borrow::Cow::Owned(c),
        Err(_) => std::borrow::Cow::Borrowed(content),
    }
}

/// A payload from JSON, made canonical ([`Rich::canonicalize`]) and
/// validated, as [`Rich::parse`] does with text.
///
/// # Errors
/// What's invalid, as a message for the user.
pub fn rich_from_value(v: Value) -> Result<Rich, String> {
    let mut r: Rich = serde_json::from_value(v).map_err(|e| format!("bad rich payload: {e}"))?;
    r.canonicalize()?;
    r.validate()?;
    Ok(r)
}

/// A `--button` spec: `[STYLE:]LABEL=react:KEY` or
/// `[STYLE:]LABEL=send:BODY`, STYLE one of primary, danger, default. The
/// label can't contain `=` (use `--rich` for that).
pub fn parse_button(spec: &str) -> Result<Button, String> {
    let (left, act) = spec.split_once('=').ok_or_else(|| {
        format!("--button {spec:?}: expected [STYLE:]LABEL=react:KEY or [STYLE:]LABEL=send:BODY")
    })?;
    let (style, label) = match left.split_once(':') {
        Some(("primary", l)) => (Style::Primary, l),
        Some(("danger", l)) => (Style::Danger, l),
        Some(("default", l)) => (Style::Default, l),
        _ => (Style::Default, left),
    };
    let action = match act.split_once(':') {
        Some(("react", key)) => Action::React {
            key: key.to_owned(),
        },
        Some(("send", body)) => Action::Send {
            body: body.to_owned(),
        },
        _ => {
            return Err(format!(
                "--button {spec:?}: the action is react:KEY or send:BODY"
            ));
        }
    };
    let b = Button {
        id: None,
        label: label.to_owned(),
        style,
        action,
    };
    b.validate()?;
    Ok(b)
}

/// `--react-seed KEYS`: comma-separated keys; `""` or `none`: no seeds.
pub fn parse_seeds(arg: &str) -> Vec<String> {
    if arg.trim().eq_ignore_ascii_case("none") {
        return Vec::new();
    }
    arg.split(',')
        .map(str::trim)
        .filter(|k| !k.is_empty())
        .map(str::to_owned)
        .collect()
}

/// A reaction key as compared: without the U+FE0F variation selector
/// (clients add or drop it).
pub fn norm_key(key: &str) -> String {
    key.replace('\u{fe0f}', "")
}

/// The payload from `--button` specs (one buttons block) or a `--rich`
/// JSON text (not both); `None` when neither is given.
///
/// # Errors
/// What's invalid, as a message for the user.
pub fn from_args(buttons: &[String], rich: Option<&str>) -> Result<Option<Rich>, String> {
    match (buttons.is_empty(), rich) {
        (true, None) => Ok(None),
        (false, Some(_)) => Err("give --button or --rich, not both".into()),
        (true, Some(json)) => Rich::parse(json).map(Some),
        (false, None) => {
            let bs = buttons
                .iter()
                .map(|s| parse_button(s))
                .collect::<Result<Vec<_>, _>>()?;
            let r = Rich::buttons(bs);
            r.validate()?;
            Ok(Some(r))
        }
    }
}

/// A math block (`--math TEX [--inline]`).
pub fn math_block(tex: &str, display: bool) -> Block {
    let mut m = Map::new();
    m.insert("type".into(), "math".into());
    m.insert("tex".into(), tex.into());
    m.insert("display".into(), display.into());
    Block::Display(m)
}

/// [`from_args`] plus `--math`: the math block first, then the buttons
/// (`--rich` with `--math` is refused: put the math block in the JSON).
///
/// # Errors
/// What's invalid, as a message for the user.
pub fn from_args_math(
    buttons: &[String],
    rich: Option<&str>,
    math: Option<(&str, bool)>,
) -> Result<Option<Rich>, String> {
    let Some((tex, display)) = math else {
        return from_args(buttons, rich);
    };
    if rich.is_some() {
        return Err(
            "give --math or --rich, not both (put a {\"type\":\"math\"} block in the JSON)".into(),
        );
    }
    let mut blocks = vec![math_block(tex, display)];
    if let Some(b) = from_args(buttons, None)? {
        blocks.extend(b.blocks);
    }
    let r = Rich {
        v: RICH_VERSION,
        blocks,
    };
    r.validate()?;
    Ok(Some(r))
}

/// The reactions to seed: `given` (`--react-seed`), else the react keys.
pub fn seeds(rich: Option<&Rich>, given: Option<&[String]>) -> Vec<String> {
    match given {
        Some(g) => g.to_vec(),
        None => rich.map(Rich::react_keys).unwrap_or_default(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn sent_content_is_canonical_json() {
        use std::borrow::Cow;
        let plain = json!({"body": "x", "n": 1});
        assert!(matches!(canonical_content(&plain), Cow::Borrowed(_)));
        let edit = json!({"body": "* x", "com.ianm.mtrix": {"v": 1, "blocks": [
                {"type": "weather", "location": "M", "temp": 30.5}]},
            "m.new_content": {"body": "x", "com.ianm.mtrix": {"v": 1, "blocks": [
                {"type": "chart", "kind": "line", "series": [{"values": [1.0, -0.61]}]}]}}});
        let c = canonical_content(&edit);
        assert!(!has_float(&c), "{c}");
        assert_eq!(c["com.ianm.mtrix"]["blocks"][0]["temp"], "30.5");
        assert_eq!(
            c["m.new_content"]["com.ianm.mtrix"]["blocks"][0]["series"][0]["values"],
            json!([1, "-0.61"])
        );
        // Unfixable: sent as is, for the homeserver to refuse.
        let big = json!({"n": 1.5, "m": 9_007_199_254_740_993_u64});
        assert!(matches!(canonical_content(&big), Cow::Borrowed(_)));
    }

    #[test]
    fn wire_format() {
        let r = Rich::buttons(vec![
            Button::react("Send", Style::Primary, "✅"),
            Button::react("Drop", Style::Danger, "❌"),
        ]);
        assert_eq!(
            serde_json::to_value(&r).unwrap(),
            json!({"v": 1, "blocks": [{"type": "buttons", "buttons": [
                {"label": "Send", "style": "primary", "action": {"type": "react", "key": "✅"}},
                {"label": "Drop", "style": "danger", "action": {"type": "react", "key": "❌"}}
            ]}]})
        );
        r.validate().unwrap();
        assert_eq!(r.react_keys(), vec!["✅", "❌"]);
    }

    #[test]
    fn parses_and_validates() {
        let ok = r#"{"v":1,"blocks":[{"type":"buttons","buttons":[
            {"label":"Yes","action":{"type":"send","body":"yes please"}}]}]}"#;
        let r = Rich::parse(ok).unwrap();
        let Block::Buttons(p) = &r.blocks[0] else {
            panic!()
        };
        assert_eq!(p.items[0].style, Style::Default);
        assert!(r.react_keys().is_empty());

        // Unknown block/action types load but don't validate.
        let holo = r#"{"v":1,"blocks":[{"type":"hologram","series":[1,2]}]}"#;
        let r: Rich = serde_json::from_str(holo).unwrap();
        assert_eq!(
            r.blocks,
            vec![Block::Unknown(
                json!({"type": "hologram", "series": [1, 2]})
            )]
        );
        assert!(
            Rich::parse(holo)
                .unwrap_err()
                .contains("unsupported block type \"hologram\"")
        );
        let act = r#"{"v":1,"blocks":[{"type":"buttons","buttons":[
            {"label":"x","action":{"type":"open_url","url":"https://x"}}]}]}"#;
        assert!(Rich::parse(act).unwrap_err().contains("unsupported action"));

        for bad in [
            r#"{"v":2,"blocks":[{"type":"buttons","buttons":[{"label":"x","action":{"type":"react","key":"a"}}]}]}"#,
            r#"{"v":1,"blocks":[]}"#,
            r#"{"v":1,"blocks":[{"type":"buttons","buttons":[]}]}"#,
            r#"{"v":1,"blocks":[{"type":"buttons","buttons":[{"label":" ","action":{"type":"react","key":"a"}}]}]}"#,
            r#"{"v":1,"blocks":[{"type":"buttons","buttons":[{"label":"x","action":{"type":"react","key":""}}]}]}"#,
            r#"{"v":1,"blocks":[{"type":"buttons","buttons":[{"label":"x","action":{"type":"send","body":""}}]}]}"#,
            r#"{"v":1,"blocks":[{"type":"buttons","buttons":[{"label":"x","style":"loud","action":{"type":"react","key":"a"}}]}]}"#,
            "not json",
        ] {
            assert!(Rich::parse(bad).is_err(), "{bad}");
        }
        let long = Rich::buttons(vec![Button::react(
            &"x".repeat(MAX_LABEL + 1),
            Style::Default,
            "a",
        )]);
        assert!(long.validate().is_err());
        let ok = Rich::buttons(vec![Button::react(
            &"é".repeat(MAX_LABEL),
            Style::Default,
            "a",
        )]);
        ok.validate().unwrap();
        let body = |n: usize| {
            Rich::buttons(vec![Button {
                id: None,
                label: "x".into(),
                style: Style::Default,
                action: Action::Send {
                    body: "😀".repeat(n),
                },
            }])
        };
        body(500).validate().unwrap();
        assert!(body(501).validate().is_err(), "1002 UTF-16 units");
        let nine = Rich::buttons(vec![Button::react("x", Style::Default, "a"); 9]);
        assert!(nine.validate().is_err());
    }

    #[test]
    fn chips_are_action_blocks() {
        let r = Rich::parse(
            r#"{"v":1,"blocks":[{"type":"chips","chips":[
                {"label":"Tell me more","action":{"type":"send","body":"tell me more"}},
                {"label":"👍","action":{"type":"react","key":"👍"}}]}]}"#,
        )
        .unwrap();
        assert_eq!(r.blocks[0].kind(), "chips");
        assert_eq!(r.react_keys(), vec!["👍"]);
        assert_eq!(r.texts().len(), 4);
        let v = serde_json::to_value(&r).unwrap();
        assert_eq!(v["blocks"][0]["type"], "chips");
        assert_eq!(v["blocks"][0]["chips"][0]["action"]["type"], "send");
        let empty = r#"{"v":1,"blocks":[{"type":"chips","chips":[]}]}"#;
        assert!(Rich::parse(empty).is_err());
        let chip = json!({"label": "x", "action": {"type": "react", "key": "a"}});
        let thirteen = json!({"v": 1, "blocks": [{"type": "chips", "chips": vec![chip; 13]}]});
        assert!(Rich::parse(&thirteen.to_string()).is_err());
    }

    #[test]
    fn display_blocks_pass_through() {
        let blocks = json!([
            {"type": "table", "columns": ["a", "b"], "rows": [[1, 2]], "caption": "t"},
            {"type": "chart", "kind": "line", "series": [{"name": "x", "values": [1, 2, 3]}]},
            {"type": "sources", "sources": [{"title": "x", "url": "https://x.com"}]},
            {"type": "card", "title": "Keyboard", "price": "₱2,499"},
            {"type": "cards", "cards": [{"title": "a"}, {"title": "b"}]},
            {"type": "progress", "steps": [{"id": "a", "label": "A", "status": "running"}]},
            {"type": "details", "summary": "Logs", "body": "..."},
            {"type": "code", "code": "fn main() {}", "language": "rust"},
        ]);
        let r = Rich::parse(&json!({"v": 1, "blocks": blocks}).to_string()).unwrap();
        assert!(r.blocks.iter().all(|b| matches!(b, Block::Display(_))));
        // Round-trips byte for byte (extra optional fields included).
        assert_eq!(serde_json::to_value(&r).unwrap()["blocks"], blocks);
        assert!(r.react_keys().is_empty() && r.texts().is_empty());

        let more = json!([
            {"type": "diff", "diff": "-a\n+b"},
            {"type": "countdown", "until": 1_800_000_000_000_u64, "label": "x"},
            {"type": "event", "title": "Dinner", "start": "2026-10-15T18:00:00+08:00"},
            {"type": "place", "name": "Downtown"},
            {"type": "ticker", "symbol": "BTC", "price": "1.5"},
            {"type": "weather", "location": "Example City", "temp": 31},
            {"type": "buttons", "buttons": [{"label": "Ok", "action": {"type": "react", "key": "👍"}}]},
        ]);
        Rich::parse(&json!({"v": 1, "blocks": more}).to_string()).unwrap();

        for (bad, why) in [
            (
                json!({"type": "table", "columns": [], "rows": [[1]]}),
                "\"columns\"",
            ),
            (
                json!({"type": "chart", "kind": "pie", "series": [1]}),
                "kind \"pie\"",
            ),
            (json!({"type": "card"}), "\"title\""),
            (
                json!({"type": "ticker", "symbol": "X", "price": "one"}),
                "\"price\"",
            ),
            (
                json!({"type": "details", "summary": "x", "body": "  "}),
                "\"body\"",
            ),
            (
                json!({"type": "event", "title": "x", "start": true}),
                "\"start\"",
            ),
        ] {
            let err = Rich::parse(&json!({"v": 1, "blocks": [bad]}).to_string()).unwrap_err();
            assert!(err.contains(why), "{err}");
        }

        let huge =
            json!({"v": 1, "blocks": [{"type": "code", "code": "x".repeat(MAX_BLOCK_BYTES)}]});
        assert!(
            Rich::parse(&huge.to_string())
                .unwrap_err()
                .contains("bytes")
        );
        let big = json!({"type": "code", "code": "x".repeat(MAX_BLOCK_BYTES - 100)});
        let total = json!({"v": 1, "blocks": [big.clone(), big]});
        assert!(
            Rich::parse(&total.to_string())
                .unwrap_err()
                .contains("payload")
        );
        let nine = json!({"v": 1, "blocks": vec![json!({"type": "card", "title": "x"}); 9]});
        assert!(Rich::parse(&nine.to_string()).is_err());
    }

    #[test]
    fn prompt_ids_scope_and_fallback() {
        let ok = json!({"v": 1, "blocks": [
            {"type": "buttons", "scope": ["@alice:example.org"], "fallback": "React ✅ or ❌",
             "buttons": [
                {"id": "approve", "style": "default", "label": "Send", "action": {"type": "react", "key": "✅"}},
                {"id": "deny", "style": "default", "label": "Drop", "action": {"type": "react", "key": "❌"}}]},
            {"type": "card", "title": "x", "actions": [
                {"id": "buy", "label": "Buy", "action": {"type": "send", "body": "buy x"}}]}
        ]});
        let r = Rich::parse(&ok.to_string()).unwrap();
        let Block::Buttons(p) = &r.blocks[0] else {
            panic!()
        };
        assert_eq!(
            p.scope.as_deref(),
            Some(&["@alice:example.org".to_owned()][..])
        );
        assert_eq!(p.items[0].id.as_deref(), Some("approve"));
        // Round-trips, and card actions count as actions.
        assert_eq!(serde_json::to_value(&r).unwrap(), ok);
        assert_eq!(r.react_keys(), vec!["✅", "❌"]);
        assert_eq!(r.blocks[1].actions()[0].id.as_deref(), Some("buy"));

        let btn =
            |id: &str| json!({"id": id, "label": "x", "action": {"type": "react", "key": "a"}});
        for (bad, why) in [
            (
                json!([{"type": "buttons", "buttons": [btn("a b")]}]),
                "button id",
            ),
            (
                json!([{"type": "buttons", "buttons": [btn("x")]}, {"type": "chips", "chips": [btn("x")]}]),
                "twice",
            ),
            (
                json!([{"type": "buttons", "scope": ["alice"], "buttons": [btn("x")]}]),
                "scope entry",
            ),
            (
                json!([{"type": "buttons", "fallback": "", "buttons": [btn("x")]}]),
                "fallback",
            ),
            (
                json!([{"type": "card", "title": "x", "actions": [btn("a"), btn("b"), btn("c")]}]),
                "1 to 2 actions",
            ),
            (
                json!([{"type": "cards", "cards": [{"title": "x", "actions": [btn("a"), btn("b")]}]}]),
                "carousel item",
            ),
            (
                json!([{"type": "cards", "cards": vec![json!({"title": "x"}); 9]}]),
                "at most 8",
            ),
            (
                json!([{"type": "card", "title": "x", "scope": "everyone"}]),
                "scope",
            ),
        ] {
            let err = Rich::parse(&json!({"v": 1, "blocks": bad}).to_string()).unwrap_err();
            assert!(err.contains(why), "{why}: {err}");
        }

        // Unknown types and newer block versions need a fallback.
        let newer = json!({"type": "table", "v": 2, "fallback": "a table", "grid": {}});
        let r = Rich::parse(&json!({"v": 1, "blocks": [newer]}).to_string()).unwrap();
        assert!(matches!(r.blocks[0], Block::Unknown(_)));
        let holo = json!({"type": "hologram", "fallback": "a hologram"});
        Rich::parse(&json!({"v": 1, "blocks": [holo]}).to_string()).unwrap();
        let bare = json!({"type": "buttons", "v": 2, "buttons": []});
        let err = Rich::parse(&json!({"v": 1, "blocks": [bare]}).to_string()).unwrap_err();
        assert!(err.contains("fallback"), "{err}");
    }

    #[test]
    fn math_blocks() {
        let r = Rich::parse(
            r#"{"v":1,"blocks":[{"type":"math","tex":"\\frac{a}{b}","display":true,"alt":"a over b"}]}"#,
        )
        .unwrap();
        assert_eq!(
            r.math_only(),
            Some(Math {
                tex: "\\frac{a}{b}".into(),
                display: true
            })
        );
        // display defaults to true; inline via Rich::math.
        let r = Rich::parse(r#"{"v":1,"blocks":[{"type":"math","tex":"x^2"}]}"#).unwrap();
        assert!(r.math_only().unwrap().display);
        let inline = Rich::math("e^{i\\pi}", false);
        inline.validate().unwrap();
        assert_eq!(
            serde_json::to_value(&inline).unwrap(),
            json!({"v": 1, "blocks": [{"type": "math", "tex": "e^{i\\pi}", "display": false}]})
        );
        assert!(!inline.math_only().unwrap().display);
        // Mixed payloads aren't math-only.
        let mixed = json!({"v": 1, "blocks": [
            {"type": "math", "tex": "x"}, {"type": "card", "title": "y"}]});
        let mixed = Rich::parse(&mixed.to_string()).unwrap();
        assert_eq!(mixed.math_only(), None);
        assert_eq!(
            Rich::buttons(vec![Button::react("a", Style::Default, "b")]).math_only(),
            None
        );

        Rich::math(&"x".repeat(MAX_TEX), true).validate().unwrap();
        for (bad, why) in [
            (json!({"type": "math"}), "\"tex\""),
            (json!({"type": "math", "tex": "  "}), "\"tex\""),
            (json!({"type": "math", "tex": 1}), "\"tex\""),
            (
                json!({"type": "math", "tex": "x".repeat(MAX_TEX + 1)}),
                "at most 4000",
            ),
            (json!({"type": "math", "tex": "a\u{0}b"}), "NUL"),
            (
                json!({"type": "math", "tex": "x", "display": "yes"}),
                "display",
            ),
            (json!({"type": "math", "tex": "x", "alt": ""}), "alt"),
            (
                json!({"type": "math", "tex": "x", "caption": "a\nb"}),
                "caption",
            ),
            (
                json!({"type": "math", "tex": "x", "caption": "c".repeat(201)}),
                "caption",
            ),
        ] {
            let err = Rich::parse(&json!({"v": 1, "blocks": [bad]}).to_string()).unwrap_err();
            assert!(err.contains(why), "{why}: {err}");
        }
    }

    #[test]
    fn tracker() {
        let full = json!({"type": "tracker", "title": "Noodle Bar", "provider": "Example Delivery",
            "kind": "food", "status": "Picking up", "state": "live",
            "stages": [
                {"id": "placed", "label": "Placed", "status": "done", "at": "2026-10-15T21:20:00+08:00"},
                {"id": "pickup", "label": "Picking up", "status": "running"},
                {"id": "deliver", "label": "On the way", "status": "pending"}],
            "eta": "2026-10-15T21:50:00+08:00",
            "map": {"url": "mxc://example.org/AbC_d-1", "w": 600, "h": 400, "alt": "Rider near Uptown"},
            "driver": {"name": "Sam D.", "vehicle": "Scooter", "plate": "ABC 1234", "rating": "4.9"},
            "items": ["2× Tonkotsu", "1× Gyoza"],
            "total": {"amount": "1240.5", "currency": "PHP"},
            "link": "https://delivery.example.org/order/A-123",
            "updated_at": 1_760_535_000_000_u64});
        let r = Rich::parse(&json!({"v": 1, "blocks": [full]}).to_string()).unwrap();
        assert_eq!(r.blocks[0].kind(), "tracker");
        assert_eq!(serde_json::to_value(&r).unwrap()["blocks"][0], full);
        let min = json!({"type": "tracker", "title": "Courier parcel", "provider": "Example Courier", "status": "In transit"});
        Rich::parse(&json!({"v": 1, "blocks": [min]}).to_string()).unwrap();

        let with = |k: &str, v: Value| {
            let mut b = json!({"type": "tracker", "title": "x", "provider": "Example Delivery", "status": "s"});
            b[k] = v;
            Rich::parse(&json!({"v": 1, "blocks": [b]}).to_string())
        };
        for (k, v, why) in [
            ("status", json!(""), "\"status\""),
            ("kind", json!("boat"), "kind"),
            ("state", json!("finished"), "state"),
            ("eta", json!(true), "\"eta\""),
            (
                "stages",
                json!([{"label": "x", "status": "waiting"}]),
                "stage",
            ),
            ("stages", json!([{"status": "done"}]), "stage"),
            (
                "stages",
                json!(vec![json!({"label": "x", "status": "done"}); 13]),
                "at most 12",
            ),
            ("map", json!({"url": "https://maps.example/x.png"}), "mxc"),
            ("map", json!({"url": "mxc://server/../x"}), "mxc"),
            ("driver", json!({"plate": "X"}), "driver"),
            ("items", json!([1, 2]), "items"),
            (
                "total",
                json!({"amount": "1,240", "currency": "PHP"}),
                "total",
            ),
            ("link", json!("http://delivery.example.org"), "https"),
        ] {
            let err = with(k, v).unwrap_err();
            assert!(err.contains(why), "{k}: {err}");
        }
    }

    #[test]
    fn math_fallback_html() {
        let m = Math {
            tex: r#"a<b & "c" 'd'"#.into(),
            display: true,
        };
        assert_eq!(
            m.html(),
            r#"<div data-mx-maths="a&lt;b &amp; &quot;c&quot; &#39;d&#39;"><code>a&lt;b &amp; &quot;c&quot; &#39;d&#39;</code></div>"#
        );
        let m = Math {
            tex: "x^2".into(),
            display: false,
        };
        assert_eq!(
            m.html(),
            r#"<span data-mx-maths="x^2"><code>x^2</code></span>"#
        );
    }

    #[test]
    fn button_specs() {
        assert_eq!(
            parse_button("primary:✅ Send=react:✅").unwrap(),
            Button::react("✅ Send", Style::Primary, "✅")
        );
        assert_eq!(
            parse_button("Drop=react:❌").unwrap(),
            Button::react("Drop", Style::Default, "❌")
        );
        let b = parse_button("danger:Cancel=send:no: cancel it").unwrap();
        assert_eq!(b.style, Style::Danger);
        assert_eq!(
            b.action,
            Action::Send {
                body: "no: cancel it".into()
            }
        );
        // A label with a colon that isn't a style keeps it.
        assert_eq!(
            parse_button("Re: lunch=react:👍").unwrap().label,
            "Re: lunch"
        );
        for bad in [
            "Send",
            "Send=",
            "Send=click:x",
            "=react:✅",
            "x=react:",
            "x=react:a b",
        ] {
            assert!(parse_button(bad).is_err(), "{bad}");
        }
    }

    #[test]
    fn numbers_are_canonical_json() {
        // Floats everywhere a number goes: chart values, ticker, weather,
        // place, tracker rating/amount, a float timestamp.
        let json = r#"{"v":1,"blocks":[
            {"type":"chart","kind":"line","series":[{"name":"a","values":[1.5,2,null,3.25]}]},
            {"type":"ticker","symbol":"AAPL","price":229.87,"change":-1.2,"change_pct":-0.52,"series":[1.1,2.0]},
            {"type":"weather","location":"Example City","temp":31.5,"high":33.0,"low":"26.4"},
            {"type":"place","name":"Downtown","lat":51.5007,"lng":-0.1246},
            {"type":"tracker","title":"Noodle Bar","provider":"Example Delivery","status":"Picking up",
             "driver":{"name":"Sam","rating":4.9},"total":{"amount":1240.5,"currency":"PHP"},
             "eta":1760536200000.0},
            {"type":"table","columns":["a"],"rows":[[0.1]]}]}"#;
        let r = Rich::parse(json).unwrap();
        let out = serde_json::to_value(&r).unwrap();
        assert_eq!(non_canonical(&out, "rich"), None);
        let text = serde_json::to_string(&r).unwrap();
        for n in ["229.87", "-0.52", "31.5", "51.5007", "4.9", "1240.5", "0.1"] {
            assert!(
                text.contains(&format!("\"{n}\"")),
                "{n} is a string: {text}"
            );
        }
        assert_eq!(
            out["blocks"][2]["high"], 33,
            "integral floats become integers"
        );
        assert_eq!(out["blocks"][4]["eta"], 1_760_536_200_000_i64);
        assert_eq!(
            out["blocks"][0]["series"][0]["values"],
            json!(["1.5", 2, null, "3.25"])
        );

        // Without canonicalize, validation refuses floats (the homeserver would).
        let raw: Rich = serde_json::from_str(json).unwrap();
        let e = raw.validate().unwrap_err();
        assert!(e.contains("blocks") || e.contains("chart"), "{e}");
        assert!(e.contains("decimal string"), "{e}");

        // Decimal strings and integers are numbers; other strings are not.
        let ok = r#"{"v":1,"blocks":[{"type":"ticker","symbol":"X","price":"62.99"}]}"#;
        Rich::parse(ok).unwrap();
        let bad = r#"{"v":1,"blocks":[{"type":"ticker","symbol":"X","price":"62,99"}]}"#;
        assert!(Rich::parse(bad).unwrap_err().contains("decimal string"));
        let big = r#"{"v":1,"blocks":[{"type":"weather","location":"X","temp":9007199254740993}]}"#;
        assert!(Rich::parse(big).unwrap_err().contains("outside"));
        let amt = r#"{"v":1,"blocks":[{"type":"tracker","title":"t","provider":"p","status":"s",
            "total":{"amount":"12x","currency":"PHP"}}]}"#;
        assert!(Rich::parse(amt).is_err());
    }

    #[test]
    fn seed_keys() {
        validate_seeds(&["✅".into(), "❌".into()]).unwrap();
        assert!(validate_seeds(&["✅".into(), "✅".into()]).is_err());
        assert!(validate_seeds(&[String::new()]).is_err());
        assert!(validate_seeds(&vec!["a".to_string(); 9]).is_err());
    }

    #[test]
    fn math_gallery_form_file() {
        let ok = json!({"v": 1, "blocks": [
            {"type": "math", "tex": "e^{i\\pi} + 1 = 0", "display": true, "alt": "Euler"},
            {"type": "gallery", "images": [{"url": "mxc://hs/a", "alt": "a"}, {"url": "mxc://hs/b"}]},
            {"type": "form", "id": "book", "fields": [{"id": "date", "kind": "date", "label": "When"}],
             "submit": {"label": "Book"}},
            {"type": "file", "name": "report.pdf", "size": 1234, "url": "mxc://hs/f"},
        ]});
        let r = Rich::parse(&ok.to_string()).unwrap();
        assert_eq!(serde_json::to_value(&r).unwrap(), ok);
        assert_eq!(r.find_math(), Some(("e^{i\\pi} + 1 = 0", true)));
        let inline = Rich::parse(r#"{"v":1,"blocks":[{"type":"math","tex":"x^2"}]}"#).unwrap();
        assert_eq!(
            inline.find_math(),
            Some(("x^2", true)),
            "display defaults to true"
        );
        for (bad, why) in [
            (json!({"type": "math", "tex": " "}), "\"tex\""),
            (
                json!({"type": "math", "tex": "x".repeat(MAX_TEX + 1)}),
                "at most",
            ),
            (
                json!({"type": "math", "tex": "x", "display": "yes"}),
                "display",
            ),
            (
                json!({"type": "math", "tex": "x", "caption": "a\nb"}),
                "caption",
            ),
            (json!({"type": "math", "tex": "x\u{0}"}), "NUL"),
            (
                json!({"type": "gallery", "images": [{"url": "https://x"}, {"url": "mxc://a/b"}]}),
                "mxc",
            ),
            (
                json!({"type": "gallery", "images": vec![json!({"url": "mxc://a/b"}); 21]}),
                "images",
            ),
            (
                json!({"type": "form", "id": "f", "fields": [{"label": "x"}], "submit": {"label": "Go"}}),
                "form field 1",
            ),
            (
                json!({"type": "form", "id": "f", "fields": [{"id": "x", "kind": "text", "label": "X"}]}),
                "submit",
            ),
            (json!({"type": "file", "size": 3}), "\"name\""),
        ] {
            let err = Rich::parse(&json!({"v": 1, "blocks": [bad]}).to_string()).unwrap_err();
            assert!(err.contains(why), "{why}: {err}");
        }
        let two =
            json!({"v": 1, "blocks": [{"type": "math", "tex": "a"}, {"type": "math", "tex": "b"}]});
        assert!(
            Rich::parse(&two.to_string())
                .unwrap_err()
                .contains("one math")
        );

        let r = from_args_math(&["Ok=react:👍".to_owned()], None, Some(("x^2", false)))
            .unwrap()
            .unwrap();
        assert_eq!(r.find_math(), Some(("x^2", false)));
        assert_eq!(r.blocks[1].kind(), "buttons");
        assert_eq!(r.react_keys(), vec!["👍"]);
        assert!(from_args_math(&[], Some("{}"), Some(("x", true))).is_err());
        assert!(from_args_math(&[], None, Some(("", true))).is_err());
        assert_eq!(from_args_math(&[], None, None).unwrap(), None);
    }

    #[test]
    fn stream_block() {
        let r = Rich::stream(
            "ab12",
            Some("https://s.example/stream/ab12"),
            StreamState::Live,
        );
        assert_eq!(
            serde_json::to_value(&r).unwrap(),
            json!({"v": 1, "blocks": [{"type": "stream", "id": "ab12",
                    "url": "https://s.example/stream/ab12", "state": "live"}]})
        );
        r.validate().unwrap();
        assert!(r.react_keys().is_empty());
        let done = Rich::stream("ab12", None, StreamState::Done);
        assert_eq!(
            serde_json::to_value(&done).unwrap(),
            json!({"v": 1, "blocks": [{"type": "stream", "id": "ab12", "state": "done"}]})
        );
        let back: Rich = serde_json::from_value(serde_json::to_value(&done).unwrap()).unwrap();
        assert_eq!(back, done);
        // A newer state parses but doesn't validate; so do bad ids and urls.
        let odd: Rich = serde_json::from_str(
            r#"{"v":1,"blocks":[{"type":"stream","id":"x","state":"paused"}]}"#,
        )
        .unwrap();
        assert!(odd.validate().is_err());
        assert!(
            Rich::stream("a b", None, StreamState::Live)
                .validate()
                .is_err()
        );
        assert!(
            Rich::stream("", None, StreamState::Live)
                .validate()
                .is_err()
        );
        assert!(
            Rich::stream("x", Some("http://s/stream/x"), StreamState::Live)
                .validate()
                .is_err()
        );
    }

    #[test]
    fn args_and_seeds() {
        assert_eq!(from_args(&[], None).unwrap(), None);
        let two = ["Yes=react:✅".to_owned(), "No=react:❌".to_owned()];
        let r = from_args(&two, None).unwrap().unwrap();
        assert_eq!(r.react_keys(), vec!["✅", "❌"]);
        assert!(from_args(&two, Some("{}")).is_err());
        assert!(from_args(&vec!["x=react:a".to_owned(); 9], None).is_err());
        assert!(parse_button("x=send: ").is_err());
        assert_eq!(
            parse_button("danger:Cancel=send:no: cancel it").unwrap(),
            Button::send("Cancel", Style::Danger, "no: cancel it")
        );
        let json = r#"{"v":1,"blocks":[{"type":"buttons","buttons":[{"label":"Go","action":{"type":"react","key":"🚀"}}]}]}"#;
        assert_eq!(
            from_args(&[], Some(json)).unwrap().unwrap().react_keys(),
            vec!["🚀"]
        );
        let table = r#"{"v":1,"blocks":[{"type":"table","columns":["a"],"rows":[[1]]}]}"#;
        assert!(
            from_args(&[], Some(table))
                .unwrap()
                .unwrap()
                .react_keys()
                .is_empty()
        );

        assert_eq!(seeds(Some(&r), None), vec!["✅", "❌"]);
        assert!(seeds(Some(&r), Some(&[])).is_empty());
        assert_eq!(seeds(None, Some(&["👍".to_owned()])), vec!["👍"]);
        assert_eq!(parse_seeds("✅, ❌"), vec!["✅", "❌"]);
        assert!(parse_seeds("").is_empty());
        assert!(parse_seeds("none").is_empty());
        assert_eq!(norm_key("✔\u{fe0f}"), "✔");
    }

    #[test]
    fn canonical_json_numbers() {
        let mut v = json!({"a": [1.5, 2.0, -0.61, 62.99, 1e-7, 7],
                           "b": {"c": 0.1}, "s": "1.5"});
        canonical_numbers(&mut v).unwrap();
        assert_eq!(
            v,
            json!({"a": ["1.5", 2, "-0.61", "62.99", "0.0000001", 7],
                   "b": {"c": "0.1"}, "s": "1.5"})
        );
        assert!(!has_float(&v));
        let e = canonical_numbers(&mut json!({"x": [MAX_SAFE_INT + 1]})).unwrap_err();
        assert!(e.starts_with("content.x[0]: the integer"), "{e}");
        assert!(canonical_numbers(&mut json!({"x": u64::MAX})).is_err());
        canonical_numbers(&mut json!([MAX_SAFE_INT, -MAX_SAFE_INT])).unwrap();
        // Number fields take an integer or a decimal string; a float left
        // uncoerced (validate without canonicalize) is refused, as is text.
        let w =
            |t: Value| json!({"v": 1, "blocks": [{"type": "weather", "location": "M", "temp": t}]});
        for t in [json!(31), json!("30.5"), json!(30.5)] {
            Rich::parse(&w(t.clone()).to_string()).unwrap_or_else(|e| panic!("{t}: {e}"));
        }
        let mut raw: Rich = serde_json::from_value(w(json!(30.5))).unwrap();
        let e = raw.validate().unwrap_err();
        assert!(e.contains("weather.temp") && e.contains("decimal"), "{e}");
        raw = serde_json::from_value(w(json!("warm"))).unwrap();
        assert!(raw.validate().is_err());
        let tracker = json!({"v": 1, "blocks": [{"type": "tracker", "title": "t", "provider": "p",
            "status": "s", "total": {"amount": 62.99, "currency": "PHP"}}]});
        let r = rich_from_value(tracker).unwrap();
        assert_eq!(
            serde_json::to_value(&r).unwrap()["blocks"][0]["total"]["amount"],
            "62.99"
        );
        let big = json!({"v": 1, "blocks": [{"type": "ticker", "symbol": "X", "price": u64::MAX}]});
        let e = rich_from_value(big).unwrap_err();
        assert!(e.contains("blocks[0].price"), "{e}");
    }

    /// The demo payloads (`tests/fixtures/rich/NN-*.json`, one per block
    /// family of the catalogue) validate, with their `body` fallback text
    /// beside them.
    #[test]
    fn catalogue_fixtures() {
        let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/rich");
        let mut n = 0;
        let mut kinds = std::collections::BTreeSet::new();
        for e in std::fs::read_dir(dir).unwrap() {
            let path = e.unwrap().path();
            if path.extension().and_then(|x| x.to_str()) != Some("json") {
                continue;
            }
            let json = std::fs::read_to_string(&path).unwrap();
            let r = Rich::parse(&json).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
            // Float-free (canonical JSON), as a message and as an edit.
            let sent = serde_json::to_value(&r).unwrap();
            let edit = json!({"body": "x", RICH_KEY: sent,
                              "m.new_content": {"body": "x", RICH_KEY: sent}});
            assert!(!has_float(&edit), "{}: {edit}", path.display());
            kinds.extend(r.blocks.iter().map(|b| b.kind().to_owned()));
            n += 1;
        }
        assert!(n >= 9, "{n} fixtures");
        // The two Synapse refused with M_BAD_JSON "float": decimal strings now.
        let read = |f: &str| {
            let j = std::fs::read_to_string(format!("{dir}/{f}")).unwrap();
            serde_json::to_value(Rich::parse(&j).unwrap()).unwrap()
        };
        let chart = read("03-chart-sources.json");
        assert_eq!(chart["blocks"][0]["series"][0]["values"][0], "-0.61");
        assert_eq!(chart["blocks"][0]["series"][0]["values"][1], "-0.6");
        let tw = read("09-ticker-weather.json");
        assert_eq!(tw["blocks"][0]["price"], "0.53396");
        assert_eq!(tw["blocks"][0]["change"], "0.0038");
        assert_eq!(tw["blocks"][1]["temp"], 29);
        for t in ["buttons", "chips"]
            .into_iter()
            .chain(DISPLAY_TYPES.iter().map(|(t, _)| *t))
            .filter(|t| *t != "stream")
        {
            assert!(kinds.contains(t), "no fixture has a {t} block");
        }
    }

    #[test]
    fn read_any_key() {
        let r = Rich::buttons(vec![Button::react("Ok", Style::Default, "👍")]);
        let v = serde_json::to_value(&r).unwrap();
        for key in RICH_KEYS {
            let mut c = Map::new();
            c.insert("body".into(), "x".into());
            c.insert((*key).into(), v.clone());
            assert_eq!(read_rich(&c).as_ref(), Some(&r), "{key}");
            assert_eq!(rich_value(&c).map(|(k, _)| k), Some(*key));
        }
        // The newest key present wins; a broken newer one falls through.
        let other = Rich::buttons(vec![Button::react("New", Style::Default, "🆕")]);
        let mut c = Map::new();
        c.insert(RICH_KEY.into(), v.clone());
        c.insert(RICH_KEY_NEW.into(), serde_json::to_value(&other).unwrap());
        assert_eq!(read_rich(&c), Some(other));
        c.insert(RICH_KEY_NEW.into(), json!("garbage"));
        assert_eq!(read_rich(&c), Some(r));
        assert_eq!(read_rich(&Map::new()), None);

        for (i, key) in USED_KEYS.iter().enumerate() {
            let mut c = Map::new();
            c.insert((*key).into(), json!({"id": format!("b{i}")}));
            assert_eq!(read_used(&c), Some(&json!({"id": format!("b{i}")})));
        }
        for key in ALBUM_KEYS {
            let mut c = Map::new();
            c.insert((*key).into(), json!({"n": 2}));
            assert_eq!(read_album(&c), Some(&json!({"n": 2})));
        }
        // The key sets line up: `<rich key>.used`, `<rich key>.album`.
        for (i, k) in RICH_KEYS.iter().enumerate() {
            assert_eq!(USED_KEYS[i], format!("{k}.used"));
            assert_eq!(ALBUM_KEYS[i], format!("{k}.album"));
        }
    }

    #[test]
    fn write_chosen_key() {
        // Today's behaviour: the default writes com.ianm.mtrix.
        assert_eq!(WriteKey::default(), WriteKey::Legacy);
        assert_eq!(WriteKey::default().rich_key(), "com.ianm.mtrix");
        assert_eq!(WriteKey::default().used_key(), "com.ianm.mtrix.used");
        assert_eq!(WriteKey::default().album_key(), "com.ianm.mtrix.album");
        assert_eq!(WriteKey::New.rich_key(), "com.talaagent.mxblocks");
        assert_eq!(WriteKey::New.used_key(), "com.talaagent.mxblocks.used");
        assert_eq!(WriteKey::New.album_key(), "com.talaagent.mxblocks.album");
        assert_eq!(serde_json::to_value(WriteKey::New).unwrap(), json!("new"));
        assert_eq!(
            serde_json::from_value::<WriteKey>(json!("legacy")).unwrap(),
            WriteKey::Legacy
        );

        let r = Rich::buttons(vec![Button::react("Ok", Style::Default, "👍")]);
        for wk in [WriteKey::Legacy, WriteKey::New] {
            // An edit of a message that had the payload under the other keys.
            let mut c = Map::new();
            for k in RICH_KEYS {
                c.insert((*k).into(), json!({"v": 1, "blocks": []}));
            }
            wk.write_rich(&mut c, &r);
            let keys: Vec<&str> = RICH_KEYS
                .iter()
                .copied()
                .filter(|k| c.contains_key(*k))
                .collect();
            assert_eq!(keys, vec![wk.rich_key()]);
            assert_eq!(read_rich(&c), Some(r.clone()));

            let mut c = Map::new();
            c.insert(USED_KEYS[0].into(), json!(1));
            wk.write_used(&mut c, json!({"id": "x"}));
            assert_eq!(c.len(), 1);
            assert_eq!(c[&wk.used_key()], json!({"id": "x"}));

            let mut c = Map::new();
            wk.write_album(&mut c, json!({"n": 3}));
            assert_eq!(read_album(&c), Some(&json!({"n": 3})));
            assert!(c.contains_key(&wk.album_key()));
        }
        let mut c = Map::new();
        c.insert(RICH_KEY_NEW.into(), json!({}));
        assert!(strip_rich(&mut c));
        assert!(!strip_rich(&mut c));
    }
}