kacrab 0.2.0

A Kafka client for Rust, built from the protocol up.
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
//! Public input and output types for the admin client.
//!
//! These mirror the shapes of Java's `org.apache.kafka.clients.admin` API
//! (`NewTopic`, `NewPartitions`, `ConfigResource`, `Config`, `Node`, ...) but
//! use idiomatic Rust ownership and avoid leaking the generated wire structs.

use kacrab_protocol::{
    KafkaUuid,
    generated::{
        CreatableReplicaAssignment, CreatableTopic, CreatableTopicConfig,
        CreatePartitionsAssignment, CreatePartitionsTopic, DescribeConfigsResource, ErrorCode,
        alter_configs_request::{AlterConfigsResource, AlterableConfig},
        incremental_alter_configs_request::{
            AlterConfigsResource as IncrementalAlterConfigsResource,
            AlterableConfig as IncrementalAlterableConfig,
        },
    },
};

use crate::common::{OffsetAndMetadata, TopicPartition};

/// A topic to create with [`AdminClient::create_topics`](super::AdminClient::create_topics).
///
/// Set partition count and replication factor for broker-assigned placement, or
/// build with [`NewTopic::with_replica_assignments`] for manual placement (the
/// two are mutually exclusive, matching Kafka).
#[derive(Debug, Clone)]
pub struct NewTopic {
    name: String,
    num_partitions: i32,
    replication_factor: i16,
    replica_assignments: Vec<(i32, Vec<i32>)>,
    configs: Vec<(String, Option<String>)>,
}

impl NewTopic {
    /// Create a topic with a fixed partition count and replication factor.
    #[must_use]
    pub fn new(name: impl Into<String>, num_partitions: i32, replication_factor: i16) -> Self {
        Self {
            name: name.into(),
            num_partitions,
            replication_factor,
            replica_assignments: Vec::new(),
            configs: Vec::new(),
        }
    }

    /// Create a topic with explicit per-partition replica assignments.
    ///
    /// Each entry maps a partition index to its ordered replica broker ids. The
    /// partition count and replication factor are derived by the broker from the
    /// assignment, so both are sent as `-1`.
    #[must_use]
    pub fn with_replica_assignments(
        name: impl Into<String>,
        assignments: Vec<(i32, Vec<i32>)>,
    ) -> Self {
        Self {
            name: name.into(),
            num_partitions: -1,
            replication_factor: -1,
            replica_assignments: assignments,
            configs: Vec::new(),
        }
    }

    /// Attach a topic-level config override (e.g. `retention.ms`). A `None` value
    /// asks the broker to use its default for that key.
    #[must_use]
    pub fn config(mut self, name: impl Into<String>, value: Option<String>) -> Self {
        self.configs.push((name.into(), value));
        self
    }

    /// The topic name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    pub(super) fn into_creatable(self) -> CreatableTopic {
        CreatableTopic {
            name: self.name.into(),
            num_partitions: self.num_partitions,
            replication_factor: self.replication_factor,
            assignments: self
                .replica_assignments
                .into_iter()
                .map(|(partition_index, broker_ids)| CreatableReplicaAssignment {
                    partition_index,
                    broker_ids,
                    _unknown_tagged_fields: Vec::new(),
                })
                .collect(),
            configs: self
                .configs
                .into_iter()
                .map(|(name, value)| CreatableTopicConfig {
                    name: name.into(),
                    value: value.map(Into::into),
                    _unknown_tagged_fields: Vec::new(),
                })
                .collect(),
            _unknown_tagged_fields: Vec::new(),
        }
    }
}

/// A request to increase a topic's partition count via
/// [`AdminClient::create_partitions`](super::AdminClient::create_partitions).
#[derive(Debug, Clone)]
pub struct NewPartitions {
    topic: String,
    total_count: i32,
    new_assignments: Vec<Vec<i32>>,
}

impl NewPartitions {
    /// Increase `topic` to `total_count` partitions, letting the broker place the
    /// new replicas.
    #[must_use]
    pub fn increase_to(topic: impl Into<String>, total_count: i32) -> Self {
        Self {
            topic: topic.into(),
            total_count,
            new_assignments: Vec::new(),
        }
    }

    /// Provide explicit replica broker ids for each newly added partition.
    #[must_use]
    pub fn assigning(mut self, new_assignments: Vec<Vec<i32>>) -> Self {
        self.new_assignments = new_assignments;
        self
    }

    /// The topic being expanded.
    #[must_use]
    pub fn topic(&self) -> &str {
        &self.topic
    }

    pub(super) fn into_topic(self) -> CreatePartitionsTopic {
        let assignments = if self.new_assignments.is_empty() {
            None
        } else {
            Some(
                self.new_assignments
                    .into_iter()
                    .map(|broker_ids| CreatePartitionsAssignment {
                        broker_ids,
                        _unknown_tagged_fields: Vec::new(),
                    })
                    .collect(),
            )
        };
        CreatePartitionsTopic {
            name: self.topic.into(),
            count: self.total_count,
            assignments,
            _unknown_tagged_fields: Vec::new(),
        }
    }
}

/// The kind of resource a [`ConfigResource`] addresses, mirroring Kafka's
/// `ConfigResource.Type` byte values.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ResourceType {
    /// An unrecognized resource type.
    Unknown,
    /// A topic's dynamic config.
    Topic,
    /// A broker's dynamic config.
    Broker,
    /// A broker's logger levels.
    BrokerLogger,
    /// A consumer group's config.
    Group,
    /// Client-metrics subscription config.
    ClientMetrics,
}

impl ResourceType {
    pub(super) const fn to_wire(self) -> i8 {
        match self {
            Self::Unknown => 0,
            Self::Topic => 2,
            Self::Broker => 4,
            Self::BrokerLogger => 8,
            Self::ClientMetrics => 16,
            Self::Group => 32,
        }
    }

    pub(super) const fn from_wire(value: i8) -> Self {
        match value {
            2 => Self::Topic,
            4 => Self::Broker,
            8 => Self::BrokerLogger,
            16 => Self::ClientMetrics,
            32 => Self::Group,
            _ => Self::Unknown,
        }
    }
}

/// Identifies a configurable resource (a topic, broker, group, ...) for the
/// describe/alter config operations.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConfigResource {
    /// The resource kind.
    pub resource_type: ResourceType,
    /// The resource name — a topic name, broker id as a string, etc. The empty
    /// string addresses the cluster-wide default for broker configs.
    pub name: String,
}

impl ConfigResource {
    /// Address a topic's configs.
    #[must_use]
    pub fn topic(name: impl Into<String>) -> Self {
        Self {
            resource_type: ResourceType::Topic,
            name: name.into(),
        }
    }

    /// Address a broker's configs by broker id.
    #[must_use]
    pub fn broker(broker_id: i32) -> Self {
        Self {
            resource_type: ResourceType::Broker,
            name: broker_id.to_string(),
        }
    }

    pub(super) fn to_describe(&self) -> DescribeConfigsResource {
        DescribeConfigsResource {
            resource_type: self.resource_type.to_wire(),
            resource_name: self.name.clone().into(),
            configuration_keys: None,
            _unknown_tagged_fields: Vec::new(),
        }
    }

    pub(super) fn to_alter(&self, entries: Vec<ConfigEntry>) -> AlterConfigsResource {
        AlterConfigsResource {
            resource_type: self.resource_type.to_wire(),
            resource_name: self.name.clone().into(),
            configs: entries
                .into_iter()
                .map(|entry| AlterableConfig {
                    name: entry.name.into(),
                    value: entry.value.map(Into::into),
                    _unknown_tagged_fields: Vec::new(),
                })
                .collect(),
            _unknown_tagged_fields: Vec::new(),
        }
    }

    pub(super) fn to_incremental(
        &self,
        ops: Vec<AlterConfigOp>,
    ) -> IncrementalAlterConfigsResource {
        IncrementalAlterConfigsResource {
            resource_type: self.resource_type.to_wire(),
            resource_name: self.name.clone().into(),
            configs: ops
                .into_iter()
                .map(|op| IncrementalAlterableConfig {
                    name: op.name.into(),
                    config_operation: op.op_type.to_wire(),
                    value: op.value.map(Into::into),
                    _unknown_tagged_fields: Vec::new(),
                })
                .collect(),
            _unknown_tagged_fields: Vec::new(),
        }
    }
}

/// The kind of incremental config edit, mirroring Kafka's
/// `AlterConfigOp.OpType`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AlterConfigOpType {
    /// Set the config key to a value.
    Set,
    /// Reset the config key to its default / remove the override.
    Delete,
    /// Append values to a list-typed config key.
    Append,
    /// Remove values from a list-typed config key.
    Subtract,
}

impl AlterConfigOpType {
    pub(super) const fn to_wire(self) -> i8 {
        match self {
            Self::Set => 0,
            Self::Delete => 1,
            Self::Append => 2,
            Self::Subtract => 3,
        }
    }
}

/// One incremental config edit for
/// [`AdminClient::incremental_alter_configs`](super::AdminClient::incremental_alter_configs),
/// mirroring Java's `AlterConfigOp`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AlterConfigOp {
    /// The config key to edit.
    pub name: String,
    /// The value for `Set`/`Append`/`Subtract`; ignored (and sent as null) for
    /// `Delete`.
    pub value: Option<String>,
    /// The kind of edit.
    pub op_type: AlterConfigOpType,
}

impl AlterConfigOp {
    /// Set a config key to `value`.
    #[must_use]
    pub fn set(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: Some(value.into()),
            op_type: AlterConfigOpType::Set,
        }
    }

    /// Reset a config key to its default.
    #[must_use]
    pub fn delete(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: None,
            op_type: AlterConfigOpType::Delete,
        }
    }

    /// Append `value` to a list-typed config key.
    #[must_use]
    pub fn append(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: Some(value.into()),
            op_type: AlterConfigOpType::Append,
        }
    }

    /// Remove `value` from a list-typed config key.
    #[must_use]
    pub fn subtract(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: Some(value.into()),
            op_type: AlterConfigOpType::Subtract,
        }
    }
}

/// A single config key/value pair, used both when altering configs (input) and
/// when describing them (output).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConfigEntry {
    /// The config key.
    pub name: String,
    /// The config value, or `None` to clear it / use the broker default.
    pub value: Option<String>,
    /// Whether the broker reports this key as read-only (describe only).
    pub read_only: bool,
    /// Whether the broker redacted the value as sensitive (describe only).
    pub is_sensitive: bool,
    /// Where the broker says the value came from (describe only).
    pub source: ConfigSource,
}

impl ConfigEntry {
    /// Build a config entry to set or clear when altering configs.
    #[must_use]
    pub fn set(name: impl Into<String>, value: Option<String>) -> Self {
        Self {
            name: name.into(),
            value,
            read_only: false,
            is_sensitive: false,
            source: ConfigSource::Unknown,
        }
    }
}

/// Where a described config value originated, mirroring Kafka's
/// `DescribeConfigsResponse.ConfigSource`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigSource {
    /// Source not reported / not recognized.
    Unknown,
    /// Set as a per-topic config.
    TopicConfig,
    /// Set dynamically on a single broker.
    DynamicBrokerConfig,
    /// Set dynamically as the cluster-wide broker default.
    DynamicDefaultBrokerConfig,
    /// Read from the broker's static `server.properties`.
    StaticBrokerConfig,
    /// The built-in default.
    DefaultConfig,
    /// A dynamically configured broker logger level.
    DynamicBrokerLoggerConfig,
}

impl ConfigSource {
    pub(super) const fn from_wire(value: i8) -> Self {
        match value {
            1 => Self::TopicConfig,
            2 => Self::DynamicBrokerConfig,
            3 => Self::DynamicDefaultBrokerConfig,
            4 => Self::StaticBrokerConfig,
            5 => Self::DefaultConfig,
            6 => Self::DynamicBrokerLoggerConfig,
            _ => Self::Unknown,
        }
    }
}

/// The described configs of one resource: its addressed resource plus the
/// config entries the broker returned.
#[derive(Debug, Clone)]
pub struct ResourceConfig {
    /// The resource these configs belong to.
    pub resource: ConfigResource,
    /// The config entries, in broker order.
    pub entries: Vec<ConfigEntry>,
}

// `Node` lives in `kacrab::common` (always compiled) and is re-exported here so
// `kacrab::admin::Node` keeps working and describe operations share one type.
pub use crate::common::Node;

/// A description of the cluster: its id, the current controller, and the live
/// broker nodes.
#[derive(Debug, Clone)]
pub struct ClusterDescription {
    /// The cluster id, if the broker reports one.
    pub cluster_id: Option<String>,
    /// The controller node, or `None` when the cluster has no controller yet.
    pub controller: Option<Node>,
    /// The live broker nodes.
    pub nodes: Vec<Node>,
}

/// A topic entry returned by
/// [`AdminClient::list_topics`](super::AdminClient::list_topics), mirroring
/// Java's `TopicListing`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TopicListing {
    /// Topic name.
    pub name: String,
    /// Stable topic id (`KafkaUuid::ZERO` if the broker did not report one).
    pub topic_id: KafkaUuid,
    /// Whether this is an internal topic (e.g. `__consumer_offsets`).
    pub is_internal: bool,
}

/// Per-partition leadership and replica placement, mirroring Java's
/// `TopicPartitionInfo`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TopicPartitionInfo {
    /// Partition index.
    pub partition: i32,
    /// Current leader node, or `None` when the partition has no leader.
    pub leader: Option<Node>,
    /// Replica nodes, in assignment order.
    pub replicas: Vec<Node>,
    /// In-sync replica nodes.
    pub isr: Vec<Node>,
}

/// A full topic description returned by
/// [`AdminClient::describe_topics`](super::AdminClient::describe_topics),
/// mirroring Java's `TopicDescription`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TopicDescription {
    /// Topic name.
    pub name: String,
    /// Stable topic id.
    pub topic_id: KafkaUuid,
    /// Whether this is an internal topic.
    pub is_internal: bool,
    /// Partition placement, ordered by partition index.
    pub partitions: Vec<TopicPartitionInfo>,
}

/// Options for [`AdminClient::list_topics`](super::AdminClient::list_topics).
#[derive(Debug, Clone, Default)]
pub struct ListTopicsOptions {
    /// Include internal topics in the listing (Java `listInternal`, default off).
    pub list_internal: bool,
}

/// Options for
/// [`AdminClient::describe_topics`](super::AdminClient::describe_topics).
#[derive(Debug, Clone, Default)]
pub struct DescribeTopicsOptions;

/// Options for [`AdminClient::create_topics`](super::AdminClient::create_topics).
#[derive(Debug, Clone, Default)]
pub struct CreateTopicsOptions {
    /// Validate the request on the broker without actually creating the topics.
    pub validate_only: bool,
}

/// Options for
/// [`AdminClient::create_partitions`](super::AdminClient::create_partitions).
#[derive(Debug, Clone, Default)]
pub struct CreatePartitionsOptions {
    /// Validate the request on the broker without actually adding partitions.
    pub validate_only: bool,
}

/// Options for [`AdminClient::alter_configs`](super::AdminClient::alter_configs).
#[derive(Debug, Clone, Default)]
pub struct AlterConfigsOptions {
    /// Validate the request on the broker without applying the changes.
    pub validate_only: bool,
}

/// A consumer group entry returned by
/// [`AdminClient::list_consumer_groups`](super::AdminClient::list_consumer_groups),
/// mirroring Java's `ConsumerGroupListing`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConsumerGroupListing {
    /// Consumer group id.
    pub group_id: String,
    /// Whether this is a "simple" consumer group (one with no protocol type, i.e.
    /// managed directly via the offset APIs rather than the group protocol).
    pub is_simple_consumer_group: bool,
    /// The group state, if the broker reported one.
    pub state: Option<GroupState>,
    /// The group protocol type, if the broker reported one.
    pub group_type: Option<GroupType>,
}

/// A consumer/share/streams group state, mirroring Kafka's `GroupState`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[expect(missing_docs, reason = "Variants mirror Kafka's GroupState names 1:1.")]
pub enum GroupState {
    Unknown,
    PreparingRebalance,
    CompletingRebalance,
    Stable,
    Dead,
    Empty,
    Assigning,
    Reconciling,
}

impl GroupState {
    /// Parse the broker-reported group state name.
    #[must_use]
    pub fn from_broker(state: &str) -> Self {
        match state {
            "PreparingRebalance" => Self::PreparingRebalance,
            "CompletingRebalance" => Self::CompletingRebalance,
            "Stable" => Self::Stable,
            "Dead" => Self::Dead,
            "Empty" => Self::Empty,
            "Assigning" => Self::Assigning,
            "Reconciling" => Self::Reconciling,
            _ => Self::Unknown,
        }
    }
}

/// A group protocol type, mirroring Kafka's `GroupType`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[expect(missing_docs, reason = "Variants mirror Kafka's GroupType names 1:1.")]
pub enum GroupType {
    Unknown,
    Classic,
    Consumer,
    Share,
    Streams,
}

impl GroupType {
    /// Parse the broker-reported group type name (case-insensitive).
    #[must_use]
    pub fn from_broker(group_type: &str) -> Self {
        match group_type.to_ascii_lowercase().as_str() {
            "classic" => Self::Classic,
            "consumer" => Self::Consumer,
            "share" => Self::Share,
            "streams" => Self::Streams,
            _ => Self::Unknown,
        }
    }
}

/// One member of a consumer group, mirroring Java's `MemberDescription`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MemberDescription {
    /// The member id assigned by the coordinator.
    pub member_id: String,
    /// The static group instance id, if the member joined with one.
    pub group_instance_id: Option<String>,
    /// The member's rack id, if it advertised one (consumer protocol).
    pub rack_id: Option<String>,
    /// The client id the member connected with.
    pub client_id: String,
    /// The host the member connected from.
    pub host: String,
    /// The partitions currently assigned to this member. Empty for members whose
    /// assignment is not partition-shaped (e.g. streams-group task assignments).
    pub assignment: Vec<TopicPartition>,
    /// The partitions in the member's target assignment (consumer protocol).
    pub target_assignment: Vec<TopicPartition>,
    /// The member epoch (consumer protocol), if known.
    pub member_epoch: Option<i32>,
    /// Whether the member uses the new consumer rebalance protocol, if known.
    pub upgraded: Option<bool>,
}

/// A consumer group description returned by
/// [`AdminClient::describe_consumer_groups`](super::AdminClient::describe_consumer_groups),
/// mirroring Java's `ConsumerGroupDescription`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConsumerGroupDescription {
    /// Consumer group id.
    pub group_id: String,
    /// Whether this is a "simple" consumer group (empty protocol type).
    pub is_simple_consumer_group: bool,
    /// The group members.
    pub members: Vec<MemberDescription>,
    /// The partition assignor / protocol the group settled on.
    pub partition_assignor: String,
    /// The group state.
    pub state: GroupState,
    /// The group protocol type (`Classic` for `DescribeGroups` groups,
    /// `Consumer` for `ConsumerGroupDescribe` groups).
    pub group_type: GroupType,
    /// The group's coordinator broker.
    pub coordinator: Node,
    /// The operations the caller is authorized to perform on the group, if the
    /// request asked for them (empty otherwise).
    pub authorized_operations: Vec<AclOperation>,
    /// The group epoch (consumer protocol only), if known.
    pub group_epoch: Option<i32>,
    /// The target-assignment epoch (consumer protocol only), if known.
    pub target_assignment_epoch: Option<i32>,
}

/// A committed offset for one partition, returned by
/// [`AdminClient::list_consumer_group_offsets`](super::AdminClient::list_consumer_group_offsets).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GroupOffset {
    /// The partition the offset belongs to.
    pub partition: TopicPartition,
    /// The committed offset and its metadata.
    pub offset: OffsetAndMetadata,
}

/// Options for
/// [`AdminClient::list_consumer_groups`](super::AdminClient::list_consumer_groups).
#[derive(Debug, Clone, Default)]
pub struct ListConsumerGroupsOptions {
    /// Only return groups in these states (broker state names); empty = all.
    pub states_filter: Vec<String>,
    /// Only return groups of these types (broker type names); empty = all.
    pub types_filter: Vec<String>,
}

/// Options for
/// [`AdminClient::describe_consumer_groups`](super::AdminClient::describe_consumer_groups).
#[derive(Debug, Clone, Default)]
pub struct DescribeConsumerGroupsOptions {
    /// Ask the broker to include the caller's authorized operations.
    pub include_authorized_operations: bool,
}

/// Options for
/// [`AdminClient::list_consumer_group_offsets`](super::AdminClient::list_consumer_group_offsets).
#[derive(Debug, Clone, Default)]
pub struct ListConsumerGroupOffsetsOptions {
    /// Restrict the fetch to these partitions; empty fetches all committed
    /// partitions for the group.
    pub partitions: Vec<TopicPartition>,
    /// Only return offsets for partitions with stable (non-transactional-pending)
    /// commits.
    pub require_stable: bool,
}

/// Which leader election to trigger via
/// [`AdminClient::elect_leaders`](super::AdminClient::elect_leaders), mirroring
/// Kafka's `ElectionType`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ElectionType {
    /// Elect the preferred (first) replica as leader where possible.
    Preferred,
    /// Elect any in-sync replica as leader, allowing an unclean election from a
    /// non-preferred replica.
    Unclean,
}

impl ElectionType {
    pub(super) const fn to_wire(self) -> i8 {
        match self {
            Self::Preferred => 0,
            Self::Unclean => 1,
        }
    }
}

/// Which offset to look up for a partition in
/// [`AdminClient::list_offsets`](super::AdminClient::list_offsets), mirroring
/// Kafka's `OffsetSpec`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OffsetSpec {
    /// The earliest available offset (log start).
    Earliest,
    /// The next offset to be written (log end / high watermark).
    Latest,
    /// The offset of the record with the largest timestamp.
    MaxTimestamp,
    /// The earliest offset whose record timestamp is `>=` this value (ms).
    Timestamp(i64),
}

impl OffsetSpec {
    /// The wire timestamp sentinel/value Kafka uses for this spec.
    pub(super) const fn to_wire(self) -> i64 {
        match self {
            Self::Earliest => -2,
            Self::Latest => -1,
            Self::MaxTimestamp => -3,
            Self::Timestamp(timestamp) => timestamp,
        }
    }
}

/// One partition's resolved offset from
/// [`AdminClient::list_offsets`](super::AdminClient::list_offsets), mirroring
/// Java's `ListOffsetsResultInfo`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListOffsetsResult {
    /// The partition the offset belongs to.
    pub partition: TopicPartition,
    /// The resolved offset.
    pub offset: i64,
    /// The timestamp of the record at `offset`, or `-1` when not applicable.
    pub timestamp: i64,
    /// The leader epoch at `offset`, if the broker reported one.
    pub leader_epoch: Option<i32>,
}

/// The new low watermark of a partition after
/// [`AdminClient::delete_records`](super::AdminClient::delete_records).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeletedRecords {
    /// The partition whose records were deleted.
    pub partition: TopicPartition,
    /// The partition's low watermark after the delete (the first offset that
    /// still exists).
    pub low_watermark: i64,
}

/// One active producer's state on a partition, mirroring Java's `ProducerState`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProducerState {
    /// Producer id.
    pub producer_id: i64,
    /// Producer epoch.
    pub producer_epoch: i32,
    /// The last sequence number the producer wrote.
    pub last_sequence: i32,
    /// The timestamp of the producer's last write (ms).
    pub last_timestamp: i64,
    /// The coordinator epoch of the producer's current transaction, or `-1`.
    pub coordinator_epoch: i32,
    /// The start offset of the producer's current transaction, or `-1` when no
    /// transaction is open.
    pub current_transaction_start_offset: i64,
}

/// The active producers on one partition, from
/// [`AdminClient::describe_producers`](super::AdminClient::describe_producers).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PartitionProducerState {
    /// The partition these producers are writing to.
    pub partition: TopicPartition,
    /// The active producers on the partition.
    pub active_producers: Vec<ProducerState>,
}

/// A transaction's description from
/// [`AdminClient::describe_transactions`](super::AdminClient::describe_transactions),
/// mirroring Java's `TransactionDescription`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransactionDescription {
    /// The transactional id.
    pub transactional_id: String,
    /// The broker-reported transaction state name (e.g. `Ongoing`, `Empty`).
    pub state: String,
    /// The producer id bound to the transactional id.
    pub producer_id: i64,
    /// The producer epoch.
    pub producer_epoch: i16,
    /// The configured transaction timeout (ms).
    pub transaction_timeout_ms: i32,
    /// When the current transaction started (ms since epoch), or `-1`.
    pub transaction_start_time_ms: i64,
    /// The partitions enrolled in the current transaction.
    pub topic_partitions: Vec<TopicPartition>,
    /// The transaction coordinator broker.
    pub coordinator: Node,
}

/// A transaction listing from
/// [`AdminClient::list_transactions`](super::AdminClient::list_transactions),
/// mirroring Java's `TransactionListing`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransactionListing {
    /// The transactional id.
    pub transactional_id: String,
    /// The producer id bound to the transactional id.
    pub producer_id: i64,
    /// The broker-reported transaction state name.
    pub state: String,
}

/// Options for
/// [`AdminClient::list_transactions`](super::AdminClient::list_transactions).
#[derive(Debug, Clone, Default)]
pub struct ListTransactionsOptions {
    /// Only list transactions in these states (broker state names); empty = all.
    pub state_filters: Vec<String>,
    /// Only list transactions of these producer ids; empty = all.
    pub producer_id_filters: Vec<i64>,
}

/// One replica's on-disk footprint within a log dir, mirroring Java's
/// `ReplicaInfo`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LogDirReplicaInfo {
    /// The replica's size on disk, in bytes.
    pub size: i64,
    /// The lag of the replica's log end offset behind the partition high
    /// watermark (`0` for the leader).
    pub offset_lag: i64,
    /// Whether this is a "future" replica being created by a reassignment to a
    /// different log dir.
    pub is_future: bool,
}

/// The replicas hosted in one broker log directory, mirroring Java's
/// `LogDirDescription`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LogDirDescription {
    /// The absolute log directory path.
    pub log_dir: String,
    /// The error the broker reported for this log dir, if any.
    pub error: Option<ErrorCode>,
    /// Total size of the volume backing the log dir, in bytes (`-1` if unknown).
    pub total_bytes: i64,
    /// Usable (free) size of the volume, in bytes (`-1` if unknown).
    pub usable_bytes: i64,
    /// The replicas hosted in this log dir.
    pub replicas: Vec<(TopicPartition, LogDirReplicaInfo)>,
}

/// One broker's log directories from
/// [`AdminClient::describe_log_dirs`](super::AdminClient::describe_log_dirs).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BrokerLogDirs {
    /// The broker the log dirs belong to.
    pub broker_id: i32,
    /// The broker's log directories.
    pub log_dirs: Vec<LogDirDescription>,
}

/// A requested partition reassignment for
/// [`AdminClient::alter_partition_reassignments`](super::AdminClient::alter_partition_reassignments),
/// mirroring Java's `Optional<NewPartitionReassignment>`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NewPartitionReassignment {
    /// The partition to reassign.
    pub topic_partition: TopicPartition,
    /// The target replica broker ids, or `None` to cancel an ongoing
    /// reassignment of this partition.
    pub replicas: Option<Vec<i32>>,
}

impl NewPartitionReassignment {
    /// Reassign the partition to `replicas`.
    #[must_use]
    pub const fn assigning(topic_partition: TopicPartition, replicas: Vec<i32>) -> Self {
        Self {
            topic_partition,
            replicas: Some(replicas),
        }
    }

    /// Cancel an ongoing reassignment of the partition.
    #[must_use]
    pub const fn cancel(topic_partition: TopicPartition) -> Self {
        Self {
            topic_partition,
            replicas: None,
        }
    }
}

/// An ongoing partition reassignment from
/// [`AdminClient::list_partition_reassignments`](super::AdminClient::list_partition_reassignments),
/// mirroring Java's `PartitionReassignment`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PartitionReassignment {
    /// The partition being reassigned.
    pub topic_partition: TopicPartition,
    /// The current full replica set (including replicas being added/removed).
    pub replicas: Vec<i32>,
    /// Replicas being added by the reassignment.
    pub adding_replicas: Vec<i32>,
    /// Replicas being removed by the reassignment.
    pub removing_replicas: Vec<i32>,
}

/// How a feature's version level is being changed by
/// [`AdminClient::update_features`](super::AdminClient::update_features),
/// mirroring Kafka's `FeatureUpdate.UpgradeType`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FeatureUpdateUpgradeType {
    /// Raise the feature's max version level.
    Upgrade,
    /// Lower the version level, refusing if it would lose metadata.
    SafeDowngrade,
    /// Lower the version level even if it may lose metadata.
    UnsafeDowngrade,
}

impl FeatureUpdateUpgradeType {
    pub(super) const fn to_wire(self) -> i8 {
        match self {
            Self::Upgrade => 1,
            Self::SafeDowngrade => 2,
            Self::UnsafeDowngrade => 3,
        }
    }

    pub(super) const fn allows_downgrade(self) -> bool {
        !matches!(self, Self::Upgrade)
    }
}

/// A single feature version-level change for
/// [`AdminClient::update_features`](super::AdminClient::update_features),
/// mirroring Java's `FeatureUpdate`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FeatureUpdate {
    /// The feature name.
    pub feature: String,
    /// The target max version level (`0` finalizes/removes the feature).
    pub max_version_level: i16,
    /// How the level is being changed.
    pub upgrade_type: FeatureUpdateUpgradeType,
}

impl FeatureUpdate {
    /// Create a feature update.
    #[must_use]
    pub fn new(
        feature: impl Into<String>,
        max_version_level: i16,
        upgrade_type: FeatureUpdateUpgradeType,
    ) -> Self {
        Self {
            feature: feature.into(),
            max_version_level,
            upgrade_type,
        }
    }
}

/// The kind of resource an ACL applies to, mirroring Kafka's `ResourceType`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AclResourceType {
    /// Unknown / unrecognized.
    Unknown,
    /// Matches any resource type (filters only).
    Any,
    /// A topic.
    Topic,
    /// A consumer group.
    Group,
    /// The cluster.
    Cluster,
    /// A transactional id.
    TransactionalId,
    /// A delegation token.
    DelegationToken,
    /// A user (SCRAM/quota principal).
    User,
}

impl AclResourceType {
    pub(super) const fn to_wire(self) -> i8 {
        match self {
            Self::Unknown => 0,
            Self::Any => 1,
            Self::Topic => 2,
            Self::Group => 3,
            Self::Cluster => 4,
            Self::TransactionalId => 5,
            Self::DelegationToken => 6,
            Self::User => 7,
        }
    }

    pub(super) const fn from_wire(value: i8) -> Self {
        match value {
            1 => Self::Any,
            2 => Self::Topic,
            3 => Self::Group,
            4 => Self::Cluster,
            5 => Self::TransactionalId,
            6 => Self::DelegationToken,
            7 => Self::User,
            _ => Self::Unknown,
        }
    }
}

/// How an ACL resource name is matched, mirroring Kafka's `PatternType`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AclPatternType {
    /// Unknown / unrecognized.
    Unknown,
    /// Matches any pattern type (filters only).
    Any,
    /// Matches literal and prefixed patterns that would match a name (filters).
    Match,
    /// An exact resource name.
    Literal,
    /// A resource-name prefix.
    Prefixed,
}

impl AclPatternType {
    pub(super) const fn to_wire(self) -> i8 {
        match self {
            Self::Unknown => 0,
            Self::Any => 1,
            Self::Match => 2,
            Self::Literal => 3,
            Self::Prefixed => 4,
        }
    }

    pub(super) const fn from_wire(value: i8) -> Self {
        match value {
            1 => Self::Any,
            2 => Self::Match,
            3 => Self::Literal,
            4 => Self::Prefixed,
            _ => Self::Unknown,
        }
    }
}

/// An ACL operation, mirroring Kafka's `AclOperation`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[expect(
    missing_docs,
    reason = "Variants mirror Kafka's AclOperation constants 1:1."
)]
pub enum AclOperation {
    Unknown,
    Any,
    All,
    Read,
    Write,
    Create,
    Delete,
    Alter,
    Describe,
    ClusterAction,
    DescribeConfigs,
    AlterConfigs,
    IdempotentWrite,
    CreateTokens,
    DescribeTokens,
}

impl AclOperation {
    pub(super) const fn to_wire(self) -> i8 {
        match self {
            Self::Unknown => 0,
            Self::Any => 1,
            Self::All => 2,
            Self::Read => 3,
            Self::Write => 4,
            Self::Create => 5,
            Self::Delete => 6,
            Self::Alter => 7,
            Self::Describe => 8,
            Self::ClusterAction => 9,
            Self::DescribeConfigs => 10,
            Self::AlterConfigs => 11,
            Self::IdempotentWrite => 12,
            Self::CreateTokens => 13,
            Self::DescribeTokens => 14,
        }
    }

    pub(super) const fn from_wire(value: i8) -> Self {
        match value {
            1 => Self::Any,
            2 => Self::All,
            3 => Self::Read,
            4 => Self::Write,
            5 => Self::Create,
            6 => Self::Delete,
            7 => Self::Alter,
            8 => Self::Describe,
            9 => Self::ClusterAction,
            10 => Self::DescribeConfigs,
            11 => Self::AlterConfigs,
            12 => Self::IdempotentWrite,
            13 => Self::CreateTokens,
            14 => Self::DescribeTokens,
            _ => Self::Unknown,
        }
    }
}

/// Whether an ACL allows or denies, mirroring Kafka's `AclPermissionType`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AclPermissionType {
    /// Unknown / unrecognized.
    Unknown,
    /// Matches any permission type (filters only).
    Any,
    /// Denies the operation.
    Deny,
    /// Allows the operation.
    Allow,
}

impl AclPermissionType {
    pub(super) const fn to_wire(self) -> i8 {
        match self {
            Self::Unknown => 0,
            Self::Any => 1,
            Self::Deny => 2,
            Self::Allow => 3,
        }
    }

    pub(super) const fn from_wire(value: i8) -> Self {
        match value {
            1 => Self::Any,
            2 => Self::Deny,
            3 => Self::Allow,
            _ => Self::Unknown,
        }
    }
}

/// An ACL binding (rule), mirroring Java's `AclBinding`.
///
/// Used as input to [`create_acls`](super::AdminClient::create_acls) and
/// returned by [`describe_acls`](super::AdminClient::describe_acls).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AclBinding {
    /// The resource kind.
    pub resource_type: AclResourceType,
    /// The resource name.
    pub resource_name: String,
    /// How the name is matched.
    pub pattern_type: AclPatternType,
    /// The principal (e.g. `User:alice`).
    pub principal: String,
    /// The host the rule applies to (`*` for any).
    pub host: String,
    /// The operation governed.
    pub operation: AclOperation,
    /// Allow or deny.
    pub permission_type: AclPermissionType,
}

/// A filter selecting ACL bindings, mirroring Java's `AclBindingFilter`.
///
/// Used by [`describe_acls`](super::AdminClient::describe_acls) and
/// [`delete_acls`](super::AdminClient::delete_acls). `None` string fields and
/// `Any` enums match anything.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AclBindingFilter {
    /// The resource kind to match (`Any` matches all).
    pub resource_type: AclResourceType,
    /// The resource name to match, or `None` for any.
    pub resource_name: Option<String>,
    /// The pattern type to match (`Any` matches all).
    pub pattern_type: AclPatternType,
    /// The principal to match, or `None` for any.
    pub principal: Option<String>,
    /// The host to match, or `None` for any.
    pub host: Option<String>,
    /// The operation to match (`Any` matches all).
    pub operation: AclOperation,
    /// The permission type to match (`Any` matches all).
    pub permission_type: AclPermissionType,
}

impl AclBindingFilter {
    /// A filter matching every ACL (`describe_acls`/`delete_acls` "match all").
    #[must_use]
    pub const fn any() -> Self {
        Self {
            resource_type: AclResourceType::Any,
            resource_name: None,
            pattern_type: AclPatternType::Any,
            principal: None,
            host: None,
            operation: AclOperation::Any,
            permission_type: AclPermissionType::Any,
        }
    }
}

/// A client-quota entity, mirroring Java's `ClientQuotaEntity`.
///
/// An ordered set of `(entity_type, entity_name)` pairs (a `None` name addresses
/// the default entity of that type). Common entity types are
/// [`USER`](Self::USER), [`CLIENT_ID`](Self::CLIENT_ID), and [`IP`](Self::IP).
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ClientQuotaEntity {
    /// The `(entity_type, entity_name)` pairs; `None` name = the type's default.
    pub entries: Vec<(String, Option<String>)>,
}

impl ClientQuotaEntity {
    /// The `user` entity type.
    pub const USER: &'static str = "user";
    /// The `client-id` entity type.
    pub const CLIENT_ID: &'static str = "client-id";
    /// The `ip` entity type.
    pub const IP: &'static str = "ip";

    /// Build an entity from `(entity_type, entity_name)` pairs.
    #[must_use]
    pub const fn new(entries: Vec<(String, Option<String>)>) -> Self {
        Self { entries }
    }
}

/// How a [`ClientQuotaFilterComponent`] matches an entity name, mirroring
/// Kafka's quota match types.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ClientQuotaMatch {
    /// Match a specific entity name.
    Exact(String),
    /// Match only the default entity of the type.
    Default,
    /// Match any entity of the type.
    Any,
}

/// One component of a client-quota filter for
/// [`describe_client_quotas`](super::AdminClient::describe_client_quotas).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClientQuotaFilterComponent {
    /// The entity type to match (e.g. `user`, `client-id`).
    pub entity_type: String,
    /// How to match the entity name.
    pub match_type: ClientQuotaMatch,
}

/// A described quota entity and its quota values, returned by
/// [`describe_client_quotas`](super::AdminClient::describe_client_quotas).
#[derive(Debug, Clone, PartialEq)]
pub struct ClientQuotaEntry {
    /// The entity the quotas apply to.
    pub entity: ClientQuotaEntity,
    /// The quota `(key, value)` pairs (e.g. `producer_byte_rate`).
    pub quotas: Vec<(String, f64)>,
}

/// A single quota change (set when `value` is `Some`, remove when `None`),
/// mirroring Java's `ClientQuotaAlteration.Op`.
#[derive(Debug, Clone, PartialEq)]
pub struct ClientQuotaOp {
    /// The quota key (e.g. `consumer_byte_rate`).
    pub key: String,
    /// The new value, or `None` to remove the quota.
    pub value: Option<f64>,
}

/// A set of quota changes to apply to one entity via
/// [`alter_client_quotas`](super::AdminClient::alter_client_quotas), mirroring
/// Java's `ClientQuotaAlteration`.
#[derive(Debug, Clone, PartialEq)]
pub struct ClientQuotaAlteration {
    /// The entity to change.
    pub entity: ClientQuotaEntity,
    /// The quota operations to apply.
    pub ops: Vec<ClientQuotaOp>,
}

/// A SCRAM hash mechanism, mirroring Kafka's `ScramMechanism`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScramMechanism {
    /// Unknown / unrecognized.
    Unknown,
    /// SCRAM-SHA-256.
    ScramSha256,
    /// SCRAM-SHA-512.
    ScramSha512,
}

impl ScramMechanism {
    pub(super) const fn to_wire(self) -> i8 {
        match self {
            Self::Unknown => 0,
            Self::ScramSha256 => 1,
            Self::ScramSha512 => 2,
        }
    }

    pub(super) const fn from_wire(value: i8) -> Self {
        match value {
            1 => Self::ScramSha256,
            2 => Self::ScramSha512,
            _ => Self::Unknown,
        }
    }
}

/// One stored SCRAM credential's parameters, mirroring Java's
/// `ScramCredentialInfo`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScramCredentialInfo {
    /// The hash mechanism.
    pub mechanism: ScramMechanism,
    /// The iteration count.
    pub iterations: i32,
}

/// A user's SCRAM credentials, returned by
/// [`describe_user_scram_credentials`](super::AdminClient::describe_user_scram_credentials).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UserScramCredentials {
    /// The user name.
    pub user: String,
    /// The user's stored credentials, one per mechanism.
    pub credentials: Vec<ScramCredentialInfo>,
}

/// A SCRAM credential to create or update via
/// [`alter_user_scram_credentials`](super::AdminClient::alter_user_scram_credentials).
///
/// The caller supplies the already-salted password (kacrab does not perform the
/// SCRAM salting itself), matching the wire shape.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScramCredentialUpsertion {
    /// The user name.
    pub user: String,
    /// The hash mechanism.
    pub mechanism: ScramMechanism,
    /// The iteration count.
    pub iterations: i32,
    /// The random salt.
    pub salt: Vec<u8>,
    /// The salted password (the SCRAM `SaltedPassword`).
    pub salted_password: Vec<u8>,
}

/// A SCRAM credential to delete via
/// [`alter_user_scram_credentials`](super::AdminClient::alter_user_scram_credentials).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScramCredentialDeletion {
    /// The user name.
    pub user: String,
    /// The mechanism to delete.
    pub mechanism: ScramMechanism,
}

/// A SASL/OAUTHBEARER-style delegation token, mirroring Java's `DelegationToken`
/// / `TokenInformation`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DelegationToken {
    /// The token id.
    pub token_id: String,
    /// The token owner's principal type (e.g. `User`).
    pub owner_principal_type: String,
    /// The token owner's principal name.
    pub owner_principal_name: String,
    /// When the token was issued (ms since epoch).
    pub issue_timestamp_ms: i64,
    /// When the token currently expires (ms since epoch).
    pub expiry_timestamp_ms: i64,
    /// The latest time the token can be renewed to (ms since epoch).
    pub max_timestamp_ms: i64,
    /// The token HMAC (the secret used to authenticate with the token).
    pub hmac: Vec<u8>,
    /// The principals allowed to renew the token, as `(type, name)` pairs.
    pub renewers: Vec<(String, String)>,
}

/// Options for
/// [`create_delegation_token`](super::AdminClient::create_delegation_token).
#[derive(Debug, Clone, Default)]
pub struct CreateDelegationTokenOptions {
    /// The token owner `(principal_type, principal_name)`, or `None` for the
    /// authenticated principal.
    pub owner: Option<(String, String)>,
    /// Principals allowed to renew the token, as `(type, name)` pairs.
    pub renewers: Vec<(String, String)>,
    /// The token's max lifetime in ms, or `-1` for the broker default.
    pub max_lifetime_ms: i64,
}

/// A replica-to-log-directory assignment for
/// [`alter_replica_log_dirs`](super::AdminClient::alter_replica_log_dirs),
/// mirroring Java's `TopicPartitionReplica` → log-dir map entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplicaLogDirAssignment {
    /// The partition whose replica is being moved.
    pub topic_partition: TopicPartition,
    /// The broker hosting the replica.
    pub broker_id: i32,
    /// The absolute target log directory path on that broker.
    pub log_dir: String,
}

/// The result of fencing one producer via
/// [`fence_producers`](super::AdminClient::fence_producers).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FencedProducer {
    /// The transactional id that was fenced.
    pub transactional_id: String,
    /// The producer id now bound to the transactional id.
    pub producer_id: i64,
    /// The bumped producer epoch (old producers are now fenced).
    pub producer_epoch: i16,
}

/// Identifies a transaction to forcibly abort via
/// [`abort_transaction`](super::AdminClient::abort_transaction), mirroring Java's
/// `AbortTransactionSpec`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AbortTransactionSpec {
    /// The partition with the hanging transaction.
    pub topic_partition: TopicPartition,
    /// The producer id of the hanging transaction.
    pub producer_id: i64,
    /// The producer epoch of the hanging transaction.
    pub producer_epoch: i16,
    /// The transaction coordinator epoch (from `describe_producers`).
    pub coordinator_epoch: i32,
}

/// A feature's broker-supported version range, mirroring Java's
/// `SupportedVersionRange`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SupportedVersionRange {
    /// The minimum supported version.
    pub min_version: i16,
    /// The maximum supported version.
    pub max_version: i16,
}

/// A feature's cluster-finalized version range, mirroring Java's
/// `FinalizedVersionRange`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FinalizedVersionRange {
    /// The minimum finalized version level.
    pub min_version_level: i16,
    /// The maximum finalized version level.
    pub max_version_level: i16,
}

/// The cluster's feature metadata from
/// [`describe_features`](super::AdminClient::describe_features), mirroring Java's
/// `FeatureMetadata`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FeatureMetadata {
    /// The epoch of the finalized feature set, if the broker reports one.
    pub finalized_features_epoch: Option<i64>,
    /// Each feature's broker-supported range, by feature name.
    pub supported_features: Vec<(String, SupportedVersionRange)>,
    /// Each feature's cluster-finalized range, by feature name.
    pub finalized_features: Vec<(String, FinalizedVersionRange)>,
}

/// A consumer-group member to remove, mirroring Java's `MemberToRemove`.
///
/// Used by
/// [`remove_members_from_consumer_group`](super::AdminClient::remove_members_from_consumer_group):
/// identify a static member by its `group_instance_id` or a dynamic member by
/// its `member_id`.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct MemberToRemove {
    /// The dynamic member id, if removing a dynamic member.
    pub member_id: Option<String>,
    /// The static group instance id, if removing a static member.
    pub group_instance_id: Option<String>,
}

impl MemberToRemove {
    /// Remove a static member by its group instance id.
    #[must_use]
    pub fn static_member(group_instance_id: impl Into<String>) -> Self {
        Self {
            member_id: None,
            group_instance_id: Some(group_instance_id.into()),
        }
    }

    /// Remove a dynamic member by its member id.
    #[must_use]
    pub fn dynamic_member(member_id: impl Into<String>) -> Self {
        Self {
            member_id: Some(member_id.into()),
            group_instance_id: None,
        }
    }
}

/// The current/future log directory of one replica, returned by
/// [`describe_replica_log_dirs`](super::AdminClient::describe_replica_log_dirs),
/// mirroring Java's `ReplicaLogDirInfo`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplicaLogDirInfo {
    /// The replica's partition.
    pub topic_partition: TopicPartition,
    /// The broker hosting the replica.
    pub broker_id: i32,
    /// The log dir currently hosting the replica, if known.
    pub current_log_dir: Option<String>,
    /// The log dir a pending move is creating the replica in, if any.
    pub future_log_dir: Option<String>,
}

/// One replica's state in the metadata quorum.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct QuorumReplicaState {
    /// The replica (voter/observer) node id.
    pub replica_id: i32,
    /// The replica's log end offset.
    pub log_end_offset: i64,
}

/// The metadata (`KRaft`) quorum state from
/// [`describe_metadata_quorum`](super::AdminClient::describe_metadata_quorum),
/// mirroring Java's `QuorumInfo`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QuorumInfo {
    /// The current quorum leader node id.
    pub leader_id: i32,
    /// The current leader epoch.
    pub leader_epoch: i32,
    /// The quorum high watermark.
    pub high_watermark: i64,
    /// The voter replicas and their state.
    pub voters: Vec<QuorumReplicaState>,
    /// The observer replicas and their state.
    pub observers: Vec<QuorumReplicaState>,
}

/// One advertised endpoint of a `KRaft` voter for
/// [`add_raft_voter`](super::AdminClient::add_raft_voter).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RaftVoterEndpoint {
    /// The listener name (e.g. `CONTROLLER`).
    pub name: String,
    /// The advertised host.
    pub host: String,
    /// The advertised port.
    pub port: u16,
}

/// A share-group description from
/// [`describe_share_groups`](super::AdminClient::describe_share_groups)
/// (Kafka 4.x share groups, KIP-932).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShareGroupDescription {
    /// The share group id.
    pub group_id: String,
    /// The group state.
    pub state: GroupState,
    /// The group epoch.
    pub group_epoch: i32,
    /// The assignor the group settled on.
    pub assignor_name: String,
    /// The group members.
    pub members: Vec<MemberDescription>,
    /// The group's coordinator broker.
    pub coordinator: Node,
    /// The operations the caller is authorized to perform on the group, if the
    /// request asked for them (empty otherwise).
    pub authorized_operations: Vec<AclOperation>,
}

/// A streams-group description from
/// [`describe_streams_groups`](super::AdminClient::describe_streams_groups)
/// (Kafka 4.x streams groups, KIP-1071).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StreamsGroupDescription {
    /// The streams group id.
    pub group_id: String,
    /// The group state.
    pub state: GroupState,
    /// The group epoch.
    pub group_epoch: i32,
    /// The group members.
    pub members: Vec<MemberDescription>,
    /// The group's coordinator broker.
    pub coordinator: Node,
    /// The operations the caller is authorized to perform on the group, if the
    /// request asked for them (empty otherwise).
    pub authorized_operations: Vec<AclOperation>,
}

#[cfg(test)]
mod tests {
    use kacrab_protocol::KafkaString;

    use super::*;

    #[test]
    fn resource_type_wire_round_trips() {
        for ty in [
            ResourceType::Topic,
            ResourceType::Broker,
            ResourceType::BrokerLogger,
            ResourceType::ClientMetrics,
            ResourceType::Group,
            ResourceType::Unknown,
        ] {
            assert_eq!(ResourceType::from_wire(ty.to_wire()), ty);
        }
        // Unrecognized wire values fold into Unknown.
        assert_eq!(ResourceType::from_wire(99), ResourceType::Unknown);
    }

    #[test]
    fn config_source_maps_known_and_unknown() {
        assert_eq!(ConfigSource::from_wire(1), ConfigSource::TopicConfig);
        assert_eq!(ConfigSource::from_wire(4), ConfigSource::StaticBrokerConfig);
        assert_eq!(ConfigSource::from_wire(5), ConfigSource::DefaultConfig);
        assert_eq!(ConfigSource::from_wire(-1), ConfigSource::Unknown);
        assert_eq!(ConfigSource::from_wire(42), ConfigSource::Unknown);
    }

    #[test]
    fn new_topic_partition_count_and_configs() {
        let topic = NewTopic::new("orders", 6, 3)
            .config("retention.ms", Some("60000".to_owned()))
            .config("cleanup.policy", None);
        assert_eq!(topic.name(), "orders");
        let wire = topic.into_creatable();
        assert_eq!(wire.name.as_str(), "orders");
        assert_eq!(wire.num_partitions, 6);
        assert_eq!(wire.replication_factor, 3);
        assert!(wire.assignments.is_empty());
        assert_eq!(wire.configs.len(), 2);
        assert_eq!(wire.configs[0].name.as_str(), "retention.ms");
        assert_eq!(
            wire.configs[0].value.as_ref().map(KafkaString::as_str),
            Some("60000")
        );
        assert_eq!(wire.configs[1].value, None);
    }

    #[test]
    fn new_topic_with_replica_assignments_sends_negative_counts() {
        let topic =
            NewTopic::with_replica_assignments("orders", vec![(0, vec![1, 2]), (1, vec![2, 3])]);
        let wire = topic.into_creatable();
        assert_eq!(wire.num_partitions, -1);
        assert_eq!(wire.replication_factor, -1);
        assert_eq!(wire.assignments.len(), 2);
        assert_eq!(wire.assignments[0].partition_index, 0);
        assert_eq!(wire.assignments[0].broker_ids, vec![1, 2]);
    }

    #[test]
    fn new_partitions_increase_and_assign() {
        let plain = NewPartitions::increase_to("orders", 8).into_topic();
        assert_eq!(plain.name.as_str(), "orders");
        assert_eq!(plain.count, 8);
        assert!(plain.assignments.is_none());

        let assigned = NewPartitions::increase_to("orders", 8)
            .assigning(vec![vec![1, 2], vec![3, 4]])
            .into_topic();
        let assignments = assigned.assignments.expect("assignments present");
        assert_eq!(assignments.len(), 2);
        assert_eq!(assignments[1].broker_ids, vec![3, 4]);
    }

    #[test]
    fn config_resource_builders_set_type_and_name() {
        let topic = ConfigResource::topic("orders");
        assert_eq!(topic.resource_type, ResourceType::Topic);
        assert_eq!(topic.name, "orders");

        let broker = ConfigResource::broker(7);
        assert_eq!(broker.resource_type, ResourceType::Broker);
        assert_eq!(broker.name, "7");

        let describe = broker.to_describe();
        assert_eq!(describe.resource_type, ResourceType::Broker.to_wire());
        assert_eq!(describe.resource_name.as_str(), "7");
        assert!(describe.configuration_keys.is_none());
    }

    #[test]
    fn alter_config_op_type_wire_values_match_kafka() {
        assert_eq!(AlterConfigOpType::Set.to_wire(), 0);
        assert_eq!(AlterConfigOpType::Delete.to_wire(), 1);
        assert_eq!(AlterConfigOpType::Append.to_wire(), 2);
        assert_eq!(AlterConfigOpType::Subtract.to_wire(), 3);
    }

    #[test]
    fn election_and_offset_spec_wire_values_match_kafka() {
        assert_eq!(ElectionType::Preferred.to_wire(), 0);
        assert_eq!(ElectionType::Unclean.to_wire(), 1);
        assert_eq!(OffsetSpec::Earliest.to_wire(), -2);
        assert_eq!(OffsetSpec::Latest.to_wire(), -1);
        assert_eq!(OffsetSpec::MaxTimestamp.to_wire(), -3);
        assert_eq!(OffsetSpec::Timestamp(1234).to_wire(), 1234);
    }

    #[test]
    fn config_resource_to_incremental_maps_ops() {
        let resource = ConfigResource::topic("orders");
        let incremental = resource.to_incremental(vec![
            AlterConfigOp::set("retention.ms", "60000"),
            AlterConfigOp::delete("cleanup.policy"),
            AlterConfigOp::append("follower.replication.throttled.replicas", "1:2"),
        ]);
        assert_eq!(incremental.resource_name.as_str(), "orders");
        assert_eq!(incremental.configs.len(), 3);
        assert_eq!(incremental.configs[0].config_operation, 0);
        assert_eq!(
            incremental.configs[0]
                .value
                .as_ref()
                .map(KafkaString::as_str),
            Some("60000")
        );
        assert_eq!(incremental.configs[1].config_operation, 1);
        assert_eq!(incremental.configs[1].value, None);
        assert_eq!(incremental.configs[2].config_operation, 2);
    }

    #[test]
    fn config_resource_to_alter_carries_entries() {
        let resource = ConfigResource::topic("orders");
        let alter = resource.to_alter(vec![
            ConfigEntry::set("retention.ms", Some("60000".to_owned())),
            ConfigEntry::set("cleanup.policy", None),
        ]);
        assert_eq!(alter.resource_name.as_str(), "orders");
        assert_eq!(alter.configs.len(), 2);
        assert_eq!(alter.configs[0].name.as_str(), "retention.ms");
        assert_eq!(
            alter.configs[0].value.as_ref().map(KafkaString::as_str),
            Some("60000")
        );
        assert_eq!(alter.configs[1].value, None);
    }

    #[test]
    fn acl_enums_round_trip_through_wire_values() {
        for ty in [
            AclResourceType::Any,
            AclResourceType::Topic,
            AclResourceType::Group,
            AclResourceType::Cluster,
            AclResourceType::TransactionalId,
            AclResourceType::DelegationToken,
            AclResourceType::User,
            AclResourceType::Unknown,
        ] {
            assert_eq!(AclResourceType::from_wire(ty.to_wire()), ty);
        }
        assert_eq!(AclResourceType::from_wire(99), AclResourceType::Unknown);

        for pattern in [
            AclPatternType::Any,
            AclPatternType::Match,
            AclPatternType::Literal,
            AclPatternType::Prefixed,
            AclPatternType::Unknown,
        ] {
            assert_eq!(AclPatternType::from_wire(pattern.to_wire()), pattern);
        }

        for permission in [
            AclPermissionType::Any,
            AclPermissionType::Deny,
            AclPermissionType::Allow,
            AclPermissionType::Unknown,
        ] {
            assert_eq!(
                AclPermissionType::from_wire(permission.to_wire()),
                permission
            );
        }

        // Spot-check a few operation wire constants and the full round-trip.
        assert_eq!(AclOperation::Read.to_wire(), 3);
        assert_eq!(AclOperation::DescribeTokens.to_wire(), 14);
        for op in [
            AclOperation::All,
            AclOperation::Read,
            AclOperation::Write,
            AclOperation::Create,
            AclOperation::Delete,
            AclOperation::Alter,
            AclOperation::Describe,
            AclOperation::IdempotentWrite,
            AclOperation::Unknown,
        ] {
            assert_eq!(AclOperation::from_wire(op.to_wire()), op);
        }
    }

    #[test]
    fn acl_binding_filter_any_matches_everything() {
        let filter = AclBindingFilter::any();
        assert_eq!(filter.resource_type, AclResourceType::Any);
        assert_eq!(filter.pattern_type, AclPatternType::Any);
        assert_eq!(filter.operation, AclOperation::Any);
        assert_eq!(filter.permission_type, AclPermissionType::Any);
        assert_eq!(filter.resource_name, None);
        assert_eq!(filter.principal, None);
        assert_eq!(filter.host, None);
    }

    #[test]
    fn member_to_remove_constructors_set_one_identifier() {
        let static_member = MemberToRemove::static_member("instance-1");
        assert_eq!(
            static_member.group_instance_id.as_deref(),
            Some("instance-1")
        );
        assert_eq!(static_member.member_id, None);

        let dynamic_member = MemberToRemove::dynamic_member("member-1");
        assert_eq!(dynamic_member.member_id.as_deref(), Some("member-1"));
        assert_eq!(dynamic_member.group_instance_id, None);
    }

    #[test]
    fn scram_mechanism_round_trips_and_feature_upgrade_maps() {
        for mechanism in [
            ScramMechanism::ScramSha256,
            ScramMechanism::ScramSha512,
            ScramMechanism::Unknown,
        ] {
            assert_eq!(ScramMechanism::from_wire(mechanism.to_wire()), mechanism);
        }
        assert_eq!(ScramMechanism::from_wire(9), ScramMechanism::Unknown);

        assert_eq!(FeatureUpdateUpgradeType::Upgrade.to_wire(), 1);
        assert!(!FeatureUpdateUpgradeType::Upgrade.allows_downgrade());
        assert_eq!(FeatureUpdateUpgradeType::SafeDowngrade.to_wire(), 2);
        assert!(FeatureUpdateUpgradeType::SafeDowngrade.allows_downgrade());
        assert_eq!(FeatureUpdateUpgradeType::UnsafeDowngrade.to_wire(), 3);
        assert!(FeatureUpdateUpgradeType::UnsafeDowngrade.allows_downgrade());
    }
}