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
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::collections::{BTreeSet, HashMap};
use std::error;
use std::fmt;
use std::io;
use std::ops::Deref;
use std::str::FromStr;
use std::time::Duration;
use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, BufReader, BufWriter, Lines};

#[derive(Debug, Default, PartialEq, Clone)]
pub struct Stats {
    // Server info
    pub pid: i64,
    pub uptime: u64,
    pub server_time: i64,
    pub threads: u64,
    pub version: String,

    // CPU
    pub rusage_user: f64,
    pub rusage_system: f64,

    // Connections
    pub max_connections: u64,
    pub curr_connections: u64,
    pub total_connections: u64,
    pub rejected_connections: u64,

    // Commands
    pub cmd_get: u64,
    pub cmd_set: u64,
    pub cmd_flush: u64,
    pub cmd_touch: u64,
    pub cmd_meta: u64,

    // Gets
    pub get_hits: u64,
    pub get_misses: u64,
    pub get_expired: u64,
    pub get_flushed: u64,

    // Sets
    pub store_too_large: u64,
    pub store_no_memory: u64,

    // Deletes
    pub delete_hits: u64,
    pub delete_misses: u64,

    // Incr/Decr
    pub incr_hits: u64,
    pub incr_misses: u64,
    pub decr_hits: u64,
    pub decr_misses: u64,

    // Touches
    pub touch_hits: u64,
    pub touch_misses: u64,

    // Bytes
    pub bytes_read: u64,
    pub bytes_written: u64,
    pub bytes: u64,
    pub max_bytes: u64,

    // Items
    pub curr_items: u64,
    pub total_items: u64,
    pub evictions: u64,
}

impl TryFrom<&HashMap<String, String>> for Stats {
    type Error = MtopError;

    fn try_from(value: &HashMap<String, String>) -> Result<Self, Self::Error> {
        Ok(Stats {
            pid: parse_field("pid", value)?,
            uptime: parse_field("uptime", value)?,
            server_time: parse_field("time", value)?,
            version: parse_field("version", value)?,
            threads: parse_field("threads", value)?,

            rusage_user: parse_field("rusage_user", value)?,
            rusage_system: parse_field("rusage_system", value)?,

            max_connections: parse_field("max_connections", value)?,
            curr_connections: parse_field("curr_connections", value)?,
            total_connections: parse_field("total_connections", value)?,
            rejected_connections: parse_field("rejected_connections", value)?,

            cmd_get: parse_field("cmd_get", value)?,
            cmd_set: parse_field("cmd_set", value)?,
            cmd_flush: parse_field("cmd_flush", value)?,
            cmd_touch: parse_field("cmd_touch", value)?,
            cmd_meta: parse_field("cmd_meta", value)?,

            get_hits: parse_field("get_hits", value)?,
            get_misses: parse_field("get_misses", value)?,
            get_expired: parse_field("get_expired", value)?,
            get_flushed: parse_field("get_flushed", value)?,

            store_too_large: parse_field("store_too_large", value)?,
            store_no_memory: parse_field("store_no_memory", value)?,

            delete_hits: parse_field("delete_hits", value)?,
            delete_misses: parse_field("delete_misses", value)?,

            incr_hits: parse_field("incr_hits", value)?,
            incr_misses: parse_field("incr_misses", value)?,

            decr_hits: parse_field("decr_hits", value)?,
            decr_misses: parse_field("decr_misses", value)?,

            touch_hits: parse_field("touch_hits", value)?,
            touch_misses: parse_field("touch_misses", value)?,

            bytes_read: parse_field("bytes_read", value)?,
            bytes_written: parse_field("bytes_written", value)?,
            bytes: parse_field("bytes", value)?,
            max_bytes: parse_field("limit_maxbytes", value)?,

            curr_items: parse_field("curr_items", value)?,
            total_items: parse_field("total_items", value)?,
            evictions: parse_field("evictions", value)?,
        })
    }
}

#[derive(Debug, Default, PartialEq, Eq, Clone, Hash)]
pub struct Slab {
    pub id: u64,
    pub chunk_size: u64,
    pub chunks_per_page: u64,
    pub total_pages: u64,
    pub total_chunks: u64,
    pub used_chunks: u64,
    pub free_chunks: u64,
    pub get_hits: u64,
    pub cmd_set: u64,
    pub delete_hits: u64,
    pub incr_hits: u64,
    pub decr_hits: u64,
    pub cas_hits: u64,
    pub cas_badval: u64,
    pub touch_hits: u64,
}

impl PartialOrd for Slab {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Slab {
    fn cmp(&self, other: &Self) -> Ordering {
        self.id.cmp(&other.id)
    }
}

#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct Slabs {
    slabs: Vec<Slab>,
}

impl Slabs {
    pub fn iter(&self) -> impl ExactSizeIterator<Item = &Slab> {
        self.slabs.iter()
    }

    pub fn len(&self) -> usize {
        self.slabs.len()
    }

    pub fn is_empty(&self) -> bool {
        self.slabs.is_empty()
    }

    pub fn find_for_size(&self, size: u64) -> Option<&Slab> {
        // Find the slab with an appropriate chunk size for an item with the given
        // size. If there is no slab with a chunk size that fits the item, return the
        // last (hence largest) slab class since this is what Memcached does internally.
        self.slabs
            .get(self.slabs.partition_point(|s| s.chunk_size < size))
            .or_else(|| self.slabs.last())
    }
}

impl IntoIterator for Slabs {
    type Item = Slab;
    type IntoIter = std::vec::IntoIter<Slab>;

    fn into_iter(self) -> Self::IntoIter {
        self.slabs.into_iter()
    }
}

impl TryFrom<&HashMap<String, String>> for Slabs {
    type Error = MtopError;

    fn try_from(value: &HashMap<String, String>) -> Result<Self, Self::Error> {
        // Parse the slab IDs from each of the raw stats. We have to do this because
        // Memcached isn't guaranteed to use a particular slab ID if there are no items
        // to store in that size class. Otherwise, we could just loop from one to
        // $active_slabs + 1.
        let mut ids = BTreeSet::new();
        for k in value.keys() {
            let key_id: Option<u64> = k.split_once(':').map(|(raw, _rest)| raw).and_then(|raw| raw.parse().ok());

            if let Some(id) = key_id {
                ids.insert(id);
            }
        }

        let mut slabs = Vec::with_capacity(ids.len());

        for id in ids {
            slabs.push(Slab {
                id,
                chunk_size: parse_field(&format!("{}:chunk_size", id), value)?,
                chunks_per_page: parse_field(&format!("{}:chunks_per_page", id), value)?,
                total_pages: parse_field(&format!("{}:total_pages", id), value)?,
                total_chunks: parse_field(&format!("{}:total_chunks", id), value)?,
                used_chunks: parse_field(&format!("{}:used_chunks", id), value)?,
                free_chunks: parse_field(&format!("{}:free_chunks", id), value)?,
                get_hits: parse_field(&format!("{}:get_hits", id), value)?,
                cmd_set: parse_field(&format!("{}:cmd_set", id), value)?,
                delete_hits: parse_field(&format!("{}:delete_hits", id), value)?,
                incr_hits: parse_field(&format!("{}:incr_hits", id), value)?,
                decr_hits: parse_field(&format!("{}:decr_hits", id), value)?,
                cas_hits: parse_field(&format!("{}:cas_hits", id), value)?,
                cas_badval: parse_field(&format!("{}:cas_badval", id), value)?,
                touch_hits: parse_field(&format!("{}:touch_hits", id), value)?,
            })
        }

        Ok(Self { slabs })
    }
}

#[derive(Debug, Default, PartialEq, Eq, Clone, Hash)]
pub struct SlabItem {
    pub id: u64,
    pub number: u64,
    pub number_hot: u64,
    pub number_warm: u64,
    pub number_cold: u64,
    pub age_hot: u64,
    pub age_warm: u64,
    pub age: u64,
    pub mem_requested: u64,
    pub evicted: u64,
    pub evicted_nonzero: u64,
    pub evicted_time: u64,
    pub out_of_memory: u64,
    pub tail_repairs: u64,
    pub reclaimed: u64,
    pub expired_unfetched: u64,
    pub evicted_unfetched: u64,
    pub evicted_active: u64,
    pub crawler_reclaimed: u64,
    pub crawler_items_checked: u64,
    pub lrutail_reflocked: u64,
    pub moves_to_cold: u64,
    pub moves_to_warm: u64,
    pub moves_within_lru: u64,
    pub direct_reclaims: u64,
    pub hits_to_hot: u64,
    pub hits_to_warm: u64,
    pub hits_to_cold: u64,
    pub hits_to_temp: u64,
}

impl PartialOrd for SlabItem {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for SlabItem {
    fn cmp(&self, other: &Self) -> Ordering {
        self.id.cmp(&other.id)
    }
}

#[derive(Debug, Default, PartialEq, Eq, Clone, Hash)]
pub struct SlabItems {
    items: Vec<SlabItem>,
}

impl SlabItems {
    pub fn iter(&self) -> impl ExactSizeIterator<Item = &SlabItem> {
        self.items.iter()
    }

    pub fn len(&self) -> usize {
        self.items.len()
    }

    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }
}

impl IntoIterator for SlabItems {
    type Item = SlabItem;
    type IntoIter = std::vec::IntoIter<SlabItem>;

    fn into_iter(self) -> Self::IntoIter {
        self.items.into_iter()
    }
}

impl TryFrom<&HashMap<String, String>> for SlabItems {
    type Error = MtopError;

    fn try_from(value: &HashMap<String, String>) -> Result<Self, Self::Error> {
        // Parse the slab IDs from each of the raw stats. We have to do this because
        // Memcached isn't guaranteed to use a particular slab ID if there are no items
        // to store in that size class. Otherwise, we could just loop from one to
        // $active_slabs + 1.
        let mut ids = BTreeSet::new();
        for k in value.keys() {
            let key_id: Option<u64> = k
                .trim_start_matches("items:")
                .split_once(':')
                .map(|(raw, _rest)| raw)
                .and_then(|raw| raw.parse().ok());

            if let Some(id) = key_id {
                ids.insert(id);
            }
        }

        let mut items = Vec::with_capacity(ids.len());

        for id in ids {
            items.push(SlabItem {
                id,
                number: parse_field(&format!("items:{}:number", id), value)?,
                number_hot: parse_field(&format!("items:{}:number_hot", id), value)?,
                number_warm: parse_field(&format!("items:{}:number_warm", id), value)?,
                number_cold: parse_field(&format!("items:{}:number_cold", id), value)?,
                age_hot: parse_field(&format!("items:{}:age_hot", id), value)?,
                age_warm: parse_field(&format!("items:{}:age_warm", id), value)?,
                age: parse_field(&format!("items:{}:age", id), value)?,
                mem_requested: parse_field(&format!("items:{}:mem_requested", id), value)?,
                evicted: parse_field(&format!("items:{}:evicted", id), value)?,
                evicted_nonzero: parse_field(&format!("items:{}:evicted_nonzero", id), value)?,
                evicted_time: parse_field(&format!("items:{}:evicted_time", id), value)?,
                out_of_memory: parse_field(&format!("items:{}:outofmemory", id), value)?,
                tail_repairs: parse_field(&format!("items:{}:tailrepairs", id), value)?,
                reclaimed: parse_field(&format!("items:{}:reclaimed", id), value)?,
                expired_unfetched: parse_field(&format!("items:{}:expired_unfetched", id), value)?,
                evicted_unfetched: parse_field(&format!("items:{}:evicted_unfetched", id), value)?,
                evicted_active: parse_field(&format!("items:{}:evicted_active", id), value)?,
                crawler_reclaimed: parse_field(&format!("items:{}:crawler_reclaimed", id), value)?,
                crawler_items_checked: parse_field(&format!("items:{}:crawler_items_checked", id), value)?,
                lrutail_reflocked: parse_field(&format!("items:{}:lrutail_reflocked", id), value)?,
                moves_to_cold: parse_field(&format!("items:{}:moves_to_cold", id), value)?,
                moves_to_warm: parse_field(&format!("items:{}:moves_to_warm", id), value)?,
                moves_within_lru: parse_field(&format!("items:{}:moves_within_lru", id), value)?,
                direct_reclaims: parse_field(&format!("items:{}:direct_reclaims", id), value)?,
                hits_to_hot: parse_field(&format!("items:{}:hits_to_hot", id), value)?,
                hits_to_warm: parse_field(&format!("items:{}:hits_to_warm", id), value)?,
                hits_to_cold: parse_field(&format!("items:{}:hits_to_cold", id), value)?,
                hits_to_temp: parse_field(&format!("items:{}:hits_to_temp", id), value)?,
            })
        }

        Ok(Self { items })
    }
}

#[derive(Debug, Default, PartialEq, Eq, Clone, Hash)]
pub struct Meta {
    pub key: String,
    pub expires: i64,
    pub size: u64,
}

impl Meta {
    // Meta information is returned from the server as multiple key-value pairs per
    // line. We only care about a subset of those keys. Define them here to avoid doing
    // extra work when parsing the server response.
    const KEYS: &'static [&'static str] = &["key", "exp", "size"];
}

impl TryFrom<&HashMap<String, String>> for Meta {
    type Error = MtopError;

    fn try_from(value: &HashMap<String, String>) -> Result<Self, Self::Error> {
        Ok(Meta {
            key: parse_field("key", value)?,
            expires: parse_field("exp", value)?,
            size: parse_field("size", value)?,
        })
    }
}

impl PartialOrd for Meta {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Meta {
    fn cmp(&self, other: &Self) -> Ordering {
        self.key.cmp(&other.key)
    }
}

#[derive(Debug, Default, PartialEq, Eq, Clone, Hash)]
pub struct Value {
    pub key: String,
    pub cas: u64,
    pub flags: u64,
    pub data: Vec<u8>,
}

impl PartialOrd for Value {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Value {
    fn cmp(&self, other: &Self) -> Ordering {
        self.key.cmp(&other.key)
    }
}

fn parse_field<T>(key: &str, map: &HashMap<String, String>) -> Result<T, MtopError>
where
    T: FromStr,
    <T as FromStr>::Err: fmt::Display + Send + Sync + error::Error + 'static,
{
    map.get(key)
        .ok_or_else(|| MtopError::runtime(format!("field {} missing", key)))
        .and_then(|v| {
            v.parse()
                .map_err(|e| MtopError::runtime_cause(format!("field {} value '{}'", key, v), e))
        })
}

fn parse_value<T>(val: &str, line: &str) -> Result<T, MtopError>
where
    T: FromStr + fmt::Display,
    <T as FromStr>::Err: fmt::Display + Send + Sync + error::Error + 'static,
{
    val.parse()
        .map_err(|e| MtopError::runtime_cause(format!("parsing {} from '{}'", val, line), e))
}

#[derive(Debug, PartialOrd, PartialEq, Copy, Clone)]
pub enum ErrorKind {
    Runtime,
    IO,
    Protocol,
    Configuration,
}

impl fmt::Display for ErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Runtime => write!(f, "runtime error"),
            Self::IO => write!(f, "io error"),
            Self::Protocol => write!(f, "protocol error"),
            Self::Configuration => write!(f, "configuration error"),
        }
    }
}

#[derive(Debug)]
enum ErrorRepr {
    Message(String),
    Cause(Box<dyn error::Error + Send + Sync + 'static>),
    MessageCause(String, Box<dyn error::Error + Send + Sync + 'static>),
}

#[derive(Debug)]
pub struct MtopError {
    kind: ErrorKind,
    repr: ErrorRepr,
}

impl MtopError {
    pub fn runtime<S>(msg: S) -> MtopError
    where
        S: Into<String>,
    {
        MtopError {
            kind: ErrorKind::Runtime,
            repr: ErrorRepr::Message(msg.into()),
        }
    }

    pub fn runtime_cause<S, E>(msg: S, e: E) -> MtopError
    where
        S: Into<String>,
        E: error::Error + Send + Sync + 'static,
    {
        MtopError {
            kind: ErrorKind::Runtime,
            repr: ErrorRepr::MessageCause(msg.into(), Box::new(e)),
        }
    }

    pub fn configuration<S>(msg: S) -> MtopError
    where
        S: Into<String>,
    {
        MtopError {
            kind: ErrorKind::Configuration,
            repr: ErrorRepr::Message(msg.into()),
        }
    }

    pub fn configuration_cause<S, E>(msg: S, e: E) -> MtopError
    where
        S: Into<String>,
        E: error::Error + Send + Sync + 'static,
    {
        MtopError {
            kind: ErrorKind::Configuration,
            repr: ErrorRepr::MessageCause(msg.into(), Box::new(e)),
        }
    }

    pub fn timeout<D>(t: Duration, operation: D) -> MtopError
    where
        D: fmt::Display,
    {
        MtopError {
            kind: ErrorKind::IO,
            repr: ErrorRepr::Message(format!("operation {} timed out after {:?}", operation, t)),
        }
    }

    pub fn kind(&self) -> ErrorKind {
        self.kind
    }
}

impl fmt::Display for MtopError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.repr {
            ErrorRepr::Message(msg) => write!(f, "{}: {}", self.kind, msg),
            ErrorRepr::Cause(e) => write!(f, "{}: {}", self.kind, e),
            ErrorRepr::MessageCause(msg, e) => write!(f, "{}: {}: {}", self.kind, msg, e),
        }
    }
}

impl error::Error for MtopError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match &self.repr {
            ErrorRepr::Message(_) => None,
            ErrorRepr::Cause(e) => Some(e.as_ref()),
            ErrorRepr::MessageCause(_, e) => Some(e.as_ref()),
        }
    }
}

impl From<(String, io::Error)> for MtopError {
    fn from((s, e): (String, io::Error)) -> Self {
        MtopError {
            kind: ErrorKind::IO,
            repr: ErrorRepr::MessageCause(s, Box::new(e)),
        }
    }
}

impl From<io::Error> for MtopError {
    fn from(e: io::Error) -> Self {
        MtopError {
            kind: ErrorKind::IO,
            repr: ErrorRepr::Cause(Box::new(e)),
        }
    }
}

impl From<ProtocolError> for MtopError {
    fn from(e: ProtocolError) -> Self {
        MtopError {
            kind: ErrorKind::Protocol,
            repr: ErrorRepr::Cause(Box::new(e)),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub enum ProtocolErrorKind {
    BadClass,
    Busy,
    Client,
    NotFound,
    NotStored,
    Server,
    Syntax,
}

impl fmt::Display for ProtocolErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::BadClass => "BADCLASS".fmt(f),
            Self::Busy => "BUSY".fmt(f),
            Self::Client => "CLIENT_ERROR".fmt(f),
            Self::NotFound => "NOT_FOUND".fmt(f),
            Self::NotStored => "NOT_STORED".fmt(f),
            Self::Server => "SERVER_ERROR".fmt(f),
            Self::Syntax => "ERROR".fmt(f),
        }
    }
}

#[derive(Debug)]
pub struct ProtocolError {
    kind: ProtocolErrorKind,
    message: Option<String>,
}

impl fmt::Display for ProtocolError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(msg) = &self.message {
            write!(f, "{} {}", self.kind, msg)
        } else {
            write!(f, "{}", self.kind)
        }
    }
}

impl error::Error for ProtocolError {}

#[derive(Debug, Eq, PartialEq, Clone)]
enum Command<'a> {
    Add(&'a Key, u64, u32, &'a [u8]),
    CrawlerMetadump,
    Decr(&'a Key, u64),
    Delete(&'a Key),
    Gets(&'a [Key]),
    Incr(&'a Key, u64),
    Replace(&'a Key, u64, u32, &'a [u8]),
    Stats,
    StatsItems,
    StatsSlabs,
    Set(&'a Key, u64, u32, &'a [u8]),
    Touch(&'a Key, u32),
    Version,
}

impl<'a> From<Command<'a>> for Vec<u8> {
    fn from(value: Command<'a>) -> Self {
        match value {
            Command::Add(key, flags, ttl, data) => storage_command("add", key, flags, ttl, data),
            Command::CrawlerMetadump => "lru_crawler metadump hash\r\n".to_owned().into_bytes(),
            Command::Decr(key, delta) => format!("decr {} {}\r\n", key, delta).into_bytes(),
            Command::Delete(key) => format!("delete {}\r\n", key).into_bytes(),
            Command::Gets(keys) => format!("gets {}\r\n", keys.join(" ")).into_bytes(),
            Command::Incr(key, delta) => format!("incr {} {}\r\n", key, delta).into_bytes(),
            Command::Replace(key, flags, ttl, data) => storage_command("replace", key, flags, ttl, data),
            Command::Stats => "stats\r\n".to_owned().into_bytes(),
            Command::StatsItems => "stats items\r\n".to_owned().into_bytes(),
            Command::StatsSlabs => "stats slabs\r\n".to_owned().into_bytes(),
            Command::Set(key, flags, ttl, data) => storage_command("set", key, flags, ttl, data),
            Command::Touch(key, ttl) => format!("touch {} {}\r\n", key, ttl).into_bytes(),
            Command::Version => "version\r\n".to_owned().into_bytes(),
        }
    }
}

fn storage_command(verb: &str, key: &Key, flags: u64, ttl: u32, data: &[u8]) -> Vec<u8> {
    let mut bytes = Vec::with_capacity(key.len() + data.len() + 32);
    io::Write::write_all(
        &mut bytes,
        format!("{} {} {} {} {}\r\n", verb, key, flags, ttl, data.len()).as_bytes(),
    )
    .unwrap();
    io::Write::write_all(&mut bytes, data).unwrap();
    io::Write::write_all(&mut bytes, "\r\n".as_bytes()).unwrap();
    bytes
}

pub struct Memcached {
    read: Lines<BufReader<Box<dyn AsyncRead + Send + Sync + Unpin>>>,
    write: BufWriter<Box<dyn AsyncWrite + Send + Sync + Unpin>>,
}

impl Memcached {
    pub fn new<R, W>(read: R, write: W) -> Self
    where
        R: AsyncRead + Send + Sync + Unpin + 'static,
        W: AsyncWrite + Send + Sync + Unpin + 'static,
    {
        Memcached {
            read: BufReader::<Box<dyn AsyncRead + Send + Sync + Unpin>>::new(Box::new(read)).lines(),
            write: BufWriter::new(Box::new(write)),
        }
    }

    /// Get a `Stats` object with the current values of the interesting stats for the server.
    pub async fn stats(&mut self) -> Result<Stats, MtopError> {
        self.send(Command::Stats).await?;
        let raw = self.read_stats_response().await?;
        Stats::try_from(&raw)
    }

    /// Get a `Slabs` object with information about each set of `Slab`s maintained by
    /// the Memcached server. You can think of each `Slab` as a class of objects that
    /// are stored together in memory. Note that `Slab` IDs may not be contiguous based
    /// on the size of items actually stored by the server.
    pub async fn slabs(&mut self) -> Result<Slabs, MtopError> {
        self.send(Command::StatsSlabs).await?;
        let raw = self.read_stats_response().await?;
        Slabs::try_from(&raw)
    }

    /// Get a `SlabsItems` object with information about the `SlabItem` items stored in
    /// each slab class maintained by the Memcached server. The ID of each `SlabItem`
    /// corresponds to a `Slab` maintained by the server. Note that `SlabItem` IDs may
    /// not be contiguous based on the size of items actually stored by the server.
    pub async fn items(&mut self) -> Result<SlabItems, MtopError> {
        self.send(Command::StatsItems).await?;
        let raw = self.read_stats_response().await?;
        SlabItems::try_from(&raw)
    }

    async fn read_stats_response(&mut self) -> Result<HashMap<String, String>, MtopError> {
        let mut out = HashMap::new();

        while let Some(v) = self.read.next_line().await? {
            if v == "END" {
                break;
            }

            let (key, val) = Self::parse_stat_line(&v)?;
            out.insert(key.to_owned(), val.to_owned());
        }

        Ok(out)
    }

    fn parse_stat_line(line: &str) -> Result<(&str, &str), MtopError> {
        let mut parts = line.splitn(3, ' ');
        match (parts.next(), parts.next(), parts.next()) {
            (Some("STAT"), Some(key), Some(val)) => Ok((key, val)),
            _ => {
                if let Some(err) = Self::parse_error(line) {
                    Err(MtopError::from(err))
                } else {
                    Err(MtopError::runtime(format!("unable to parse '{}'", line)))
                }
            }
        }
    }

    /// Get a `Meta` object for every item in the cache which includes its key and expiration
    /// time as a UNIX timestamp. Expiration time will be `-1` if the item was set with an
    /// infinite TTL.
    pub async fn metas(&mut self) -> Result<Vec<Meta>, MtopError> {
        self.send(Command::CrawlerMetadump).await?;
        let mut out = Vec::new();
        let mut raw = HashMap::new();

        while let Some(v) = self.read.next_line().await? {
            if v == "END" {
                break;
            }

            // Check for an error first because the `metadump` command doesn't
            // have any sort of prefix for each result line like `STAT` or `VALUE`
            // so it's hard to know if it's valid without looking for an error.
            if let Some(err) = Self::parse_error(&v) {
                return Err(MtopError::from(err));
            }

            let item = Self::parse_crawler_meta(&v, Meta::KEYS, &mut raw)?;
            out.push(item);
        }

        Ok(out)
    }

    fn parse_crawler_meta(line: &str, keys: &[&str], raw: &mut HashMap<String, String>) -> Result<Meta, MtopError> {
        // Avoid allocating a new HashMap to parse every meta entry just to throw it away
        raw.clear();

        for p in line.split(' ') {
            let (key, val) = p
                .split_once('=')
                .ok_or_else(|| MtopError::runtime(format!("unexpected metadump format '{}'", line)))?;

            // Avoid spending time decoding values or allocating for data we don't care about.
            // Use a slice here since it's faster than a HashSet when the number of entries is
            // small and the number of keys we're searching through is always small.
            if !keys.contains(&key) {
                continue;
            }

            let decoded = urlencoding::decode(val)
                .map_err(|e| MtopError::runtime_cause(format!("unexpected metadump encoding '{}'", line), e))?;
            raw.insert(key.to_owned(), decoded.into_owned());
        }

        Meta::try_from(raw.deref())
    }

    /// Get a map of the requested keys and their corresponding `Value` in the cache
    /// including the key, flags, and data.
    pub async fn get(&mut self, keys: &[Key]) -> Result<HashMap<String, Value>, MtopError> {
        self.send(Command::Gets(keys)).await?;
        let mut out = HashMap::with_capacity(keys.len());

        while let Some(v) = self.read.next_line().await? {
            if v == "END" {
                break;
            }

            let value = self.parse_gets_value(&v).await?;
            out.insert(value.key.clone(), value);
        }

        Ok(out)
    }

    async fn parse_gets_value(&mut self, line: &str) -> Result<Value, MtopError> {
        let mut parts = line.splitn(5, ' ');

        match (parts.next(), parts.next(), parts.next(), parts.next(), parts.next()) {
            (Some("VALUE"), Some(k), Some(flags), Some(len), Some(cas)) => {
                let flags: u64 = parse_value(flags, line)?;
                let len: u64 = parse_value(len, line)?;
                let cas: u64 = parse_value(cas, line)?;

                // Two extra bytes to read the trailing \r\n but then truncate them.
                let mut data = Vec::with_capacity(len as usize + 2);
                let reader = self.read.get_mut();
                reader.take(len + 2).read_to_end(&mut data).await?;
                data.truncate(len as usize);

                Ok(Value {
                    key: k.to_owned(),
                    flags,
                    cas,
                    data,
                })
            }
            _ => {
                // Response doesn't look like a `VALUE` line, see if the server has
                // responded with an error that we can parse. Otherwise, consider this
                // an internal error.
                if let Some(err) = Self::parse_error(line) {
                    Err(MtopError::from(err))
                } else {
                    Err(MtopError::runtime(format!("unable to parse '{}'", line)))
                }
            }
        }
    }

    /// Increment the value of a key by the given delta if the value is numeric returning
    /// the new value. Returns an error if the value is _not_ numeric.
    pub async fn incr(&mut self, key: &Key, delta: u64) -> Result<u64, MtopError> {
        self.send(Command::Incr(key, delta)).await?;
        if let Some(v) = self.read.next_line().await? {
            Self::parse_numeric_response(&v)
        } else {
            Err(MtopError::runtime("unexpected empty response"))
        }
    }

    /// Decrement the value of a key by the given delta if the value is numeric returning
    /// the new value with a minimum of 0. Returns an error if the value is _not_ numeric.
    pub async fn decr(&mut self, key: &Key, delta: u64) -> Result<u64, MtopError> {
        self.send(Command::Decr(key, delta)).await?;
        if let Some(v) = self.read.next_line().await? {
            Self::parse_numeric_response(&v)
        } else {
            Err(MtopError::runtime("unexpected empty response"))
        }
    }

    fn parse_numeric_response(line: &str) -> Result<u64, MtopError> {
        if let Some(err) = Self::parse_error(line) {
            Err(MtopError::from(err))
        } else {
            line.parse()
                .map_err(|_e| MtopError::runtime(format!("unable to parse '{}'", line)))
        }
    }

    /// Send a simple command to verify our connection to the server is working.
    pub async fn ping(&mut self) -> Result<(), MtopError> {
        self.send(Command::Version).await?;
        if let Some(v) = self.read.next_line().await? {
            if let Some(e) = Self::parse_error(&v) {
                return Err(MtopError::from(e));
            }
            if !v.starts_with("VERSION") {
                return Err(MtopError::runtime(format!("unable to parse '{}'", v)));
            }
        }

        Ok(())
    }

    /// Store the provided item in the cache, regardless of whether it already exists.
    pub async fn set<V>(&mut self, key: &Key, flags: u64, ttl: u32, data: V) -> Result<(), MtopError>
    where
        V: AsRef<[u8]>,
    {
        self.send(Command::Set(key, flags, ttl, data.as_ref())).await?;
        if let Some(v) = self.read.next_line().await? {
            Self::parse_simple_response(&v, "STORED")
        } else {
            Err(MtopError::runtime("unexpected empty response"))
        }
    }

    /// Store the provided item in the cache only if it does not already exist.
    pub async fn add<V>(&mut self, key: &Key, flags: u64, ttl: u32, data: V) -> Result<(), MtopError>
    where
        V: AsRef<[u8]>,
    {
        self.send(Command::Add(key, flags, ttl, data.as_ref())).await?;
        if let Some(v) = self.read.next_line().await? {
            Self::parse_simple_response(&v, "STORED")
        } else {
            Err(MtopError::runtime("unexpected empty response"))
        }
    }

    /// Store the provided item in the cache only if it already exists.
    pub async fn replace<V>(&mut self, key: &Key, flags: u64, ttl: u32, data: V) -> Result<(), MtopError>
    where
        V: AsRef<[u8]>,
    {
        self.send(Command::Replace(key, flags, ttl, data.as_ref())).await?;
        if let Some(v) = self.read.next_line().await? {
            Self::parse_simple_response(&v, "STORED")
        } else {
            Err(MtopError::runtime("unexpected empty response"))
        }
    }

    /// Update the TTL of an item in the cache if it exists, return an error otherwise.
    pub async fn touch(&mut self, key: &Key, ttl: u32) -> Result<(), MtopError> {
        self.send(Command::Touch(key, ttl)).await?;
        if let Some(v) = self.read.next_line().await? {
            Self::parse_simple_response(&v, "TOUCHED")
        } else {
            Err(MtopError::runtime("unexpected empty response"))
        }
    }

    /// Delete an item in the cache if it exists, return an error otherwise.
    pub async fn delete(&mut self, key: &Key) -> Result<(), MtopError> {
        self.send(Command::Delete(key)).await?;
        if let Some(v) = self.read.next_line().await? {
            Self::parse_simple_response(&v, "DELETED")
        } else {
            Err(MtopError::runtime("unexpected empty response"))
        }
    }

    fn parse_simple_response(line: &str, expected: &str) -> Result<(), MtopError> {
        if line == expected {
            Ok(())
        } else if let Some(err) = Self::parse_error(line) {
            Err(MtopError::from(err))
        } else {
            Err(MtopError::runtime(format!("unable to parse '{}'", line)))
        }
    }

    fn parse_error(line: &str) -> Option<ProtocolError> {
        let mut values = line.splitn(2, ' ');
        let (kind, message) = match (values.next(), values.next()) {
            (Some("BADCLASS"), Some(msg)) => (ProtocolErrorKind::BadClass, Some(msg.to_owned())),
            (Some("BUSY"), Some(msg)) => (ProtocolErrorKind::Busy, Some(msg.to_owned())),
            (Some("CLIENT_ERROR"), Some(msg)) => (ProtocolErrorKind::Client, Some(msg.to_owned())),
            (Some("ERROR"), None) => (ProtocolErrorKind::Syntax, None),
            (Some("ERROR"), Some(msg)) => (ProtocolErrorKind::Syntax, Some(msg.to_owned())),
            (Some("NOT_FOUND"), None) => (ProtocolErrorKind::NotFound, None),
            (Some("NOT_STORED"), None) => (ProtocolErrorKind::NotStored, None),
            (Some("SERVER_ERROR"), Some(msg)) => (ProtocolErrorKind::Server, Some(msg.to_owned())),

            _ => return None,
        };

        Some(ProtocolError { kind, message })
    }

    async fn send<'a>(&'a mut self, cmd: Command<'a>) -> Result<(), MtopError> {
        let cmd_bytes: Vec<u8> = cmd.into();
        self.write.write_all(&cmd_bytes).await?;
        Ok(self.write.flush().await?)
    }
}

impl fmt::Debug for Memcached {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Memcached {{ read: <...>, write: <...> }}")
    }
}

#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[repr(transparent)]
pub struct Key(String);

impl Key {
    const MAX_LENGTH: usize = 250;

    pub fn one<T>(val: T) -> Result<Key, MtopError>
    where
        T: Into<String>,
    {
        let val = val.into();
        if !Self::is_legal_val(&val) {
            Err(MtopError::runtime(format!("invalid key {}", val)))
        } else {
            Ok(Key(val))
        }
    }

    pub fn many<I, T>(vals: I) -> Result<Vec<Key>, MtopError>
    where
        I: IntoIterator<Item = T>,
        T: Into<String>,
    {
        let mut out = Vec::new();
        for val in vals {
            out.push(Self::one(val)?);
        }

        Ok(out)
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    fn is_legal_val(val: &str) -> bool {
        if val.len() > Self::MAX_LENGTH {
            return false;
        }

        for c in val.chars() {
            if !c.is_ascii() || c.is_ascii_whitespace() || c.is_ascii_control() {
                return false;
            }
        }

        true
    }
}

impl fmt::Display for Key {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl AsRef<str> for Key {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl Borrow<str> for Key {
    fn borrow(&self) -> &str {
        &self.0
    }
}

#[cfg(test)]
mod test {
    use super::{ErrorKind, Key, Memcached, Meta, Slab, SlabItem, SlabItems};
    use std::io::{Cursor, Error};
    use std::pin::Pin;
    use std::task::{Context, Poll};
    use tokio::io::AsyncWrite;
    use tokio::sync::mpsc::{self, UnboundedSender};

    /////////
    // key //
    /////////

    #[test]
    fn test_key_one_length() {
        let val = "abc".repeat(Key::MAX_LENGTH);
        let res = Key::one(val);
        assert!(res.is_err());
    }

    #[test]
    fn test_key_one_non_ascii() {
        let val = "🤦";
        let res = Key::one(val);
        assert!(res.is_err());
    }

    #[test]
    fn test_key_one_whitespace() {
        let val = "some thing";
        let res = Key::one(val);
        assert!(res.is_err());
    }

    #[test]
    fn test_key_one_control_char() {
        let val = "\x7F";
        let res = Key::one(val);
        assert!(res.is_err());
    }

    #[test]
    fn test_key_one_success() {
        let val = "a-reasonable-key";
        let res = Key::one(val);
        assert!(res.is_ok());
    }

    struct WriteAdapter {
        tx: UnboundedSender<Vec<u8>>,
    }

    impl AsyncWrite for WriteAdapter {
        fn poll_write(self: Pin<&mut Self>, _cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize, Error>> {
            self.tx.send(buf.to_owned()).unwrap();
            Poll::Ready(Ok(buf.len()))
        }

        fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Error>> {
            Poll::Ready(Ok(()))
        }

        fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Error>> {
            Poll::Ready(Ok(()))
        }
    }

    /// Create a new receiver channel and `Memcached` instance to read the provided server
    /// response. Anything written by the client is able to be read from the receiver channel.
    /// NOTE that it is important that the receiver not be dropped by the caller since this will
    /// cause writes to the channel to fail from within the client.
    macro_rules! client {
        () => ({
            let (tx, rx) = mpsc::unbounded_channel();
            let reads = Vec::new();
            (rx, Memcached::new(Cursor::new(reads), WriteAdapter { tx }))
        });
        ($($line:expr),+ $(,)?) => ({
            let (tx, rx) = mpsc::unbounded_channel();
            let mut reads = Vec::new();
            $(reads.extend_from_slice($line.as_bytes());)+
            (rx, Memcached::new(Cursor::new(reads), WriteAdapter { tx }))
        })
    }

    /////////
    // get //
    /////////

    #[tokio::test]
    async fn test_memcached_get_no_key() {
        let (_rx, mut client) = client!();
        let vals: Vec<String> = vec![];
        let keys = Key::many(vals).unwrap();
        let res = client.get(&keys).await.unwrap();

        assert!(res.is_empty());
    }

    #[tokio::test]
    async fn test_memcached_get_error() {
        let (_rx, mut client) = client!("SERVER_ERROR backend failure\r\n");
        let keys = Key::many(vec!["foo", "baz"]).unwrap();
        let res = client.get(&keys).await;

        assert!(res.is_err());
        let err = res.unwrap_err();
        assert_eq!(ErrorKind::Protocol, err.kind());
    }

    #[tokio::test]
    async fn test_memcached_get_miss() {
        let (_rx, mut client) = client!("END\r\n");
        let keys = Key::many(vec!["foo", "baz"]).unwrap();
        let res = client.get(&keys).await.unwrap();

        assert!(res.is_empty());
    }

    #[tokio::test]
    async fn test_memcached_get_hit() {
        let (_rx, mut client) = client!(
            "VALUE foo 32 3 1\r\n",
            "bar\r\n",
            "VALUE baz 64 3 2\r\n",
            "qux\r\n",
            "END\r\n",
        );
        let keys = Key::many(vec!["foo", "baz"]).unwrap();
        let res = client.get(&keys).await.unwrap();

        let val1 = res.get("foo").unwrap();
        assert_eq!("foo", val1.key);
        assert_eq!("bar".as_bytes(), val1.data);
        assert_eq!(32, val1.flags);
        assert_eq!(1, val1.cas);

        let val2 = res.get("baz").unwrap();
        assert_eq!("baz", val2.key);
        assert_eq!("qux".as_bytes(), val2.data);
        assert_eq!(64, val2.flags);
        assert_eq!(2, val2.cas);
    }

    //////////
    // incr //
    //////////

    #[tokio::test]
    async fn test_memcached_incr_bad_val() {
        let (mut rx, mut client) = client!("CLIENT_ERROR cannot increment or decrement non-numeric value\r\n");
        let key = Key::one("test").unwrap();
        let res = client.incr(&key, 2).await;

        assert!(res.is_err());
        let err = res.unwrap_err();
        assert_eq!(ErrorKind::Protocol, err.kind());

        let bytes = rx.recv().await.unwrap();
        let command = String::from_utf8(bytes).unwrap();
        assert_eq!("incr test 2\r\n", command);
    }

    #[tokio::test]
    async fn test_memcached_incr_success() {
        let (mut rx, mut client) = client!("3\r\n");
        let key = Key::one("test").unwrap();
        let res = client.incr(&key, 2).await.unwrap();

        assert_eq!(3, res);
        let bytes = rx.recv().await.unwrap();
        let command = String::from_utf8(bytes).unwrap();
        assert_eq!("incr test 2\r\n", command);
    }

    //////////
    // decr //
    //////////

    #[tokio::test]
    async fn test_memcached_decr_bad_val() {
        let (mut rx, mut client) = client!("CLIENT_ERROR cannot increment or decrement non-numeric value\r\n");
        let key = Key::one("test").unwrap();
        let res = client.decr(&key, 1).await;

        assert!(res.is_err());
        let err = res.unwrap_err();
        assert_eq!(ErrorKind::Protocol, err.kind());

        let bytes = rx.recv().await.unwrap();
        let command = String::from_utf8(bytes).unwrap();
        assert_eq!("decr test 1\r\n", command);
    }

    #[tokio::test]
    async fn test_memcached_decr_success() {
        let (mut rx, mut client) = client!("3\r\n");
        let key = Key::one("test").unwrap();
        let res = client.decr(&key, 1).await.unwrap();

        assert_eq!(3, res);
        let bytes = rx.recv().await.unwrap();
        let command = String::from_utf8(bytes).unwrap();
        assert_eq!("decr test 1\r\n", command);
    }

    //////////
    // ping //
    //////////

    #[tokio::test]
    async fn test_memcached_ping_bad_val() {
        let (mut rx, mut client) = client!("220 localhost ESMTP Postfix\r\n");
        let res = client.ping().await;

        assert!(res.is_err());
        let err = res.unwrap_err();
        assert_eq!(ErrorKind::Runtime, err.kind());

        let bytes = rx.recv().await.unwrap();
        let command = String::from_utf8(bytes).unwrap();
        assert_eq!("version\r\n", command);
    }

    #[tokio::test]
    async fn test_memcached_ping_success() {
        let (mut rx, mut client) = client!("VERSION 1.6.22\r\n");
        client.ping().await.unwrap();

        let bytes = rx.recv().await.unwrap();
        let command = String::from_utf8(bytes).unwrap();
        assert_eq!("version\r\n", command);
    }

    macro_rules! test_store_command_success {
        ($method:ident, $verb:expr) => {
            let (mut rx, mut client) = client!("STORED\r\n");
            let res = client.$method(&Key::one("test").unwrap(), 0, 300, "val".as_bytes()).await;

            assert!(res.is_ok());
            let bytes = rx.recv().await.unwrap();
            let command = String::from_utf8(bytes).unwrap();
            assert_eq!(concat!($verb, " test 0 300 3\r\nval\r\n"), command);
        };
    }

    macro_rules! test_store_command_error {
        ($method:ident, $verb:expr) => {
            let (mut rx, mut client) = client!("NOT_STORED\r\n");
            let res = client.$method(&Key::one("test").unwrap(), 0, 300, "val".as_bytes()).await;

            assert!(res.is_err());
            let err = res.unwrap_err();
            assert_eq!(ErrorKind::Protocol, err.kind());

            let bytes = rx.recv().await.unwrap();
            let command = String::from_utf8(bytes).unwrap();
            assert_eq!(concat!($verb, " test 0 300 3\r\nval\r\n"), command);
        };
    }

    /////////
    // set //
    /////////

    #[tokio::test]
    async fn test_memcached_set_success() {
        test_store_command_success!(set, "set");
    }

    #[tokio::test]
    async fn test_memcached_set_error() {
        test_store_command_error!(set, "set");
    }

    /////////
    // add //
    /////////

    #[tokio::test]
    async fn test_memcached_add_success() {
        test_store_command_success!(add, "add");
    }

    #[tokio::test]
    async fn test_memcached_add_error() {
        test_store_command_error!(add, "add");
    }

    /////////////
    // replace //
    /////////////

    #[tokio::test]
    async fn test_memcached_replace_success() {
        test_store_command_success!(replace, "replace");
    }

    #[tokio::test]
    async fn test_memcached_replace_error() {
        test_store_command_error!(replace, "replace");
    }

    ///////////
    // touch //
    ///////////

    #[tokio::test]
    async fn test_memcached_touch_success() {
        let (mut rx, mut client) = client!("TOUCHED\r\n");
        let key = Key::one("test").unwrap();
        let res = client.touch(&key, 300).await;

        assert!(res.is_ok());
        let bytes = rx.recv().await.unwrap();
        let command = String::from_utf8(bytes).unwrap();
        assert_eq!("touch test 300\r\n", command);
    }

    #[tokio::test]
    async fn test_memcached_touch_error() {
        let (mut rx, mut client) = client!("NOT_FOUND\r\n");
        let key = Key::one("test").unwrap();
        let res = client.touch(&key, 300).await;

        assert!(res.is_err());
        let err = res.unwrap_err();
        assert_eq!(ErrorKind::Protocol, err.kind());

        let bytes = rx.recv().await.unwrap();
        let command = String::from_utf8(bytes).unwrap();
        assert_eq!("touch test 300\r\n", command);
    }

    ////////////
    // delete //
    ////////////

    #[tokio::test]
    async fn test_memcached_delete_success() {
        let (mut rx, mut client) = client!("DELETED\r\n");
        let key = Key::one("test").unwrap();
        let res = client.delete(&key).await;

        assert!(res.is_ok());
        let bytes = rx.recv().await.unwrap();
        let command = String::from_utf8(bytes).unwrap();
        assert_eq!("delete test\r\n", command);
    }

    #[tokio::test]
    async fn test_memcached_delete_error() {
        let (mut rx, mut client) = client!("NOT_FOUND\r\n");
        let key = Key::one("test").unwrap();
        let res = client.delete(&key).await;

        assert!(res.is_err());
        let err = res.unwrap_err();
        assert_eq!(ErrorKind::Protocol, err.kind());

        let bytes = rx.recv().await.unwrap();
        let command = String::from_utf8(bytes).unwrap();
        assert_eq!("delete test\r\n", command);
    }

    ///////////
    // stats //
    ///////////

    #[tokio::test]
    async fn test_memcached_stats_empty() {
        let (_rx, mut client) = client!("END\r\n");
        let res = client.stats().await;

        assert!(res.is_err());
        let err = res.unwrap_err();
        assert_eq!(ErrorKind::Runtime, err.kind());
    }

    #[tokio::test]
    async fn test_memcached_stats_error() {
        let (_rx, mut client) = client!("SERVER_ERROR backend failure\r\n");
        let res = client.stats().await;

        assert!(res.is_err());
        let err = res.unwrap_err();
        assert_eq!(ErrorKind::Protocol, err.kind());
    }

    #[tokio::test]
    async fn test_memcached_stats_success() {
        let (_rx, mut client) = client!(
            "STAT pid 1525\r\n",
            "STAT uptime 271984\r\n",
            "STAT time 1687212809\r\n",
            "STAT version 1.6.14\r\n",
            "STAT libevent 2.1.12-stable\r\n",
            "STAT pointer_size 64\r\n",
            "STAT rusage_user 17.544323\r\n",
            "STAT rusage_system 11.830461\r\n",
            "STAT max_connections 1024\r\n",
            "STAT curr_connections 1\r\n",
            "STAT total_connections 3\r\n",
            "STAT rejected_connections 0\r\n",
            "STAT connection_structures 2\r\n",
            "STAT response_obj_oom 0\r\n",
            "STAT response_obj_count 1\r\n",
            "STAT response_obj_bytes 32768\r\n",
            "STAT read_buf_count 4\r\n",
            "STAT read_buf_bytes 65536\r\n",
            "STAT read_buf_bytes_free 16384\r\n",
            "STAT read_buf_oom 0\r\n",
            "STAT reserved_fds 20\r\n",
            "STAT cmd_get 1\r\n",
            "STAT cmd_set 0\r\n",
            "STAT cmd_flush 0\r\n",
            "STAT cmd_touch 0\r\n",
            "STAT cmd_meta 0\r\n",
            "STAT get_hits 0\r\n",
            "STAT get_misses 1\r\n",
            "STAT get_expired 0\r\n",
            "STAT get_flushed 0\r\n",
            "STAT delete_misses 0\r\n",
            "STAT delete_hits 0\r\n",
            "STAT incr_misses 0\r\n",
            "STAT incr_hits 0\r\n",
            "STAT decr_misses 0\r\n",
            "STAT decr_hits 0\r\n",
            "STAT cas_misses 0\r\n",
            "STAT cas_hits 0\r\n",
            "STAT cas_badval 0\r\n",
            "STAT touch_hits 0\r\n",
            "STAT touch_misses 0\r\n",
            "STAT store_too_large 0\r\n",
            "STAT store_no_memory 0\r\n",
            "STAT auth_cmds 0\r\n",
            "STAT auth_errors 0\r\n",
            "STAT bytes_read 16\r\n",
            "STAT bytes_written 7\r\n",
            "STAT limit_maxbytes 67108864\r\n",
            "STAT accepting_conns 1\r\n",
            "STAT listen_disabled_num 0\r\n",
            "STAT time_in_listen_disabled_us 0\r\n",
            "STAT threads 4\r\n",
            "STAT conn_yields 0\r\n",
            "STAT hash_power_level 16\r\n",
            "STAT hash_bytes 524288\r\n",
            "STAT hash_is_expanding 0\r\n",
            "STAT slab_reassign_rescues 0\r\n",
            "STAT slab_reassign_chunk_rescues 0\r\n",
            "STAT slab_reassign_evictions_nomem 0\r\n",
            "STAT slab_reassign_inline_reclaim 0\r\n",
            "STAT slab_reassign_busy_items 0\r\n",
            "STAT slab_reassign_busy_deletes 0\r\n",
            "STAT slab_reassign_running 0\r\n",
            "STAT slabs_moved 0\r\n",
            "STAT lru_crawler_running 0\r\n",
            "STAT lru_crawler_starts 105\r\n",
            "STAT lru_maintainer_juggles 271976\r\n",
            "STAT malloc_fails 0\r\n",
            "STAT log_worker_dropped 0\r\n",
            "STAT log_worker_written 0\r\n",
            "STAT log_watcher_skipped 0\r\n",
            "STAT log_watcher_sent 0\r\n",
            "STAT log_watchers 0\r\n",
            "STAT unexpected_napi_ids 0\r\n",
            "STAT round_robin_fallback 0\r\n",
            "STAT bytes 0\r\n",
            "STAT curr_items 0\r\n",
            "STAT total_items 0\r\n",
            "STAT slab_global_page_pool 0\r\n",
            "STAT expired_unfetched 0\r\n",
            "STAT evicted_unfetched 0\r\n",
            "STAT evicted_active 0\r\n",
            "STAT evictions 0\r\n",
            "STAT reclaimed 0\r\n",
            "STAT crawler_reclaimed 0\r\n",
            "STAT crawler_items_checked 0\r\n",
            "STAT lrutail_reflocked 0\r\n",
            "STAT moves_to_cold 0\r\n",
            "STAT moves_to_warm 0\r\n",
            "STAT moves_within_lru 0\r\n",
            "STAT direct_reclaims 0\r\n",
            "STAT lru_bumps_dropped 0\r\n",
            "END\r\n",
        );
        let res = client.stats().await.unwrap();

        assert_eq!(0, res.cmd_set);
        assert_eq!(1, res.cmd_get);
        assert_eq!(1, res.get_misses);
        assert_eq!(0, res.get_hits);
    }

    ///////////
    // slabs //
    ///////////

    #[tokio::test]
    async fn test_memcached_slabs_empty() {
        let (_rx, mut client) = client!("STAT active_slabs 0\r\n", "STAT total_malloced 0\r\n", "END\r\n");
        let res = client.slabs().await.unwrap();

        assert!(res.slabs.is_empty());
    }

    #[tokio::test]
    async fn test_memcached_slabs_error() {
        let (_rx, mut client) = client!("ERROR Too many open connections\r\n");
        let res = client.slabs().await;

        assert!(res.is_err());
        let err = res.unwrap_err();
        assert_eq!(ErrorKind::Protocol, err.kind());
    }

    #[tokio::test]
    async fn test_memcached_slabs_success() {
        let (_rx, mut client) = client!(
            "STAT 6:chunk_size 304\r\n",
            "STAT 6:chunks_per_page 3449\r\n",
            "STAT 6:total_pages 1\r\n",
            "STAT 6:total_chunks 3449\r\n",
            "STAT 6:used_chunks 1\r\n",
            "STAT 6:free_chunks 3448\r\n",
            "STAT 6:free_chunks_end 0\r\n",
            "STAT 6:get_hits 951\r\n",
            "STAT 6:cmd_set 100\r\n",
            "STAT 6:delete_hits 0\r\n",
            "STAT 6:incr_hits 0\r\n",
            "STAT 6:decr_hits 0\r\n",
            "STAT 6:cas_hits 0\r\n",
            "STAT 6:cas_badval 0\r\n",
            "STAT 6:touch_hits 0\r\n",
            "STAT 7:chunk_size 384\r\n",
            "STAT 7:chunks_per_page 2730\r\n",
            "STAT 7:total_pages 1\r\n",
            "STAT 7:total_chunks 2730\r\n",
            "STAT 7:used_chunks 5\r\n",
            "STAT 7:free_chunks 2725\r\n",
            "STAT 7:free_chunks_end 0\r\n",
            "STAT 7:get_hits 4792\r\n",
            "STAT 7:cmd_set 520\r\n",
            "STAT 7:delete_hits 0\r\n",
            "STAT 7:incr_hits 0\r\n",
            "STAT 7:decr_hits 0\r\n",
            "STAT 7:cas_hits 0\r\n",
            "STAT 7:cas_badval 0\r\n",
            "STAT 7:touch_hits 0\r\n",
            "STAT active_slabs 2\r\n",
            "STAT total_malloced 30408704\r\n",
            "END\r\n",
        );
        let res = client.slabs().await.unwrap();

        let expected = vec![
            Slab {
                id: 6,
                chunk_size: 304,
                chunks_per_page: 3449,
                total_pages: 1,
                total_chunks: 3449,
                used_chunks: 1,
                free_chunks: 3448,
                get_hits: 951,
                cmd_set: 100,
                delete_hits: 0,
                incr_hits: 0,
                decr_hits: 0,
                cas_hits: 0,
                cas_badval: 0,
                touch_hits: 0,
            },
            Slab {
                id: 7,
                chunk_size: 384,
                chunks_per_page: 2730,
                total_pages: 1,
                total_chunks: 2730,
                used_chunks: 5,
                free_chunks: 2725,
                get_hits: 4792,
                cmd_set: 520,
                delete_hits: 0,
                incr_hits: 0,
                decr_hits: 0,
                cas_hits: 0,
                cas_badval: 0,
                touch_hits: 0,
            },
        ];

        assert_eq!(expected, res.slabs);
    }

    ///////////
    // items //
    ///////////

    #[tokio::test]
    async fn test_memcached_items_empty() {
        let (_rx, mut client) = client!();
        let res = client.items().await.unwrap();

        assert!(res.is_empty());
    }

    #[tokio::test]
    async fn test_memcached_items_error() {
        let (_rx, mut client) = client!("ERROR Too many open connections\r\n");
        let res = client.items().await;

        assert!(res.is_err());
        let err = res.unwrap_err();
        assert_eq!(ErrorKind::Protocol, err.kind());
    }

    #[tokio::test]
    async fn test_memcached_items_success() {
        let (_rx, mut client) = client!(
            "STAT items:39:number 3\r\n",
            "STAT items:39:number_hot 0\r\n",
            "STAT items:39:number_warm 1\r\n",
            "STAT items:39:number_cold 2\r\n",
            "STAT items:39:age_hot 0\r\n",
            "STAT items:39:age_warm 7\r\n",
            "STAT items:39:age 8\r\n",
            "STAT items:39:mem_requested 1535788\r\n",
            "STAT items:39:evicted 1646\r\n",
            "STAT items:39:evicted_nonzero 1646\r\n",
            "STAT items:39:evicted_time 0\r\n",
            "STAT items:39:outofmemory 9\r\n",
            "STAT items:39:tailrepairs 0\r\n",
            "STAT items:39:reclaimed 13\r\n",
            "STAT items:39:expired_unfetched 4\r\n",
            "STAT items:39:evicted_unfetched 202\r\n",
            "STAT items:39:evicted_active 6\r\n",
            "STAT items:39:crawler_reclaimed 0\r\n",
            "STAT items:39:crawler_items_checked 40\r\n",
            "STAT items:39:lrutail_reflocked 17365\r\n",
            "STAT items:39:moves_to_cold 8703\r\n",
            "STAT items:39:moves_to_warm 7285\r\n",
            "STAT items:39:moves_within_lru 3651\r\n",
            "STAT items:39:direct_reclaims 1949\r\n",
            "STAT items:39:hits_to_hot 894\r\n",
            "STAT items:39:hits_to_warm 4079\r\n",
            "STAT items:39:hits_to_cold 8043\r\n",
            "STAT items:39:hits_to_temp 0\r\n",
            "END\r\n",
        );
        let res = client.items().await.unwrap();

        let expected = SlabItems {
            items: vec![SlabItem {
                id: 39,
                number: 3,
                number_hot: 0,
                number_warm: 1,
                number_cold: 2,
                age_hot: 0,
                age_warm: 7,
                age: 8,
                mem_requested: 1535788,
                evicted: 1646,
                evicted_nonzero: 1646,
                evicted_time: 0,
                out_of_memory: 9,
                tail_repairs: 0,
                reclaimed: 13,
                expired_unfetched: 4,
                evicted_unfetched: 202,
                evicted_active: 6,
                crawler_reclaimed: 0,
                crawler_items_checked: 40,
                lrutail_reflocked: 17365,
                moves_to_cold: 8703,
                moves_to_warm: 7285,
                moves_within_lru: 3651,
                direct_reclaims: 1949,
                hits_to_hot: 894,
                hits_to_warm: 4079,
                hits_to_cold: 8043,
                hits_to_temp: 0,
            }],
        };

        assert_eq!(expected, res);
    }

    //////////
    // meta //
    //////////

    #[tokio::test]
    async fn test_memcached_metas_empty() {
        let (_rx, mut client) = client!();
        let res = client.metas().await.unwrap();

        assert!(res.is_empty());
    }

    #[tokio::test]
    async fn test_memcached_metas_error() {
        let (_rx, mut client) = client!("BUSY crawler is busy\r\n",);
        let res = client.metas().await;

        assert!(res.is_err());
        let err = res.unwrap_err();
        assert_eq!(ErrorKind::Protocol, err.kind());
    }

    #[tokio::test]
    async fn test_memcached_metas_success() {
        let (_rx, mut client) = client!(
            "key=memcached%2Fmurmur3_hash.c exp=1687216956 la=1687216656 cas=259502 fetch=yes cls=17 size=2912\r\n",
            "key=memcached%2Fmd5.h exp=1687216956 la=1687216656 cas=259731 fetch=yes cls=17 size=3593\r\n",
            "END\r\n",
        );
        let res = client.metas().await.unwrap();

        let expected = vec![
            Meta {
                key: "memcached/murmur3_hash.c".to_string(),
                expires: 1687216956,
                size: 2912,
            },
            Meta {
                key: "memcached/md5.h".to_string(),
                expires: 1687216956,
                size: 3593,
            },
        ];

        assert_eq!(expected, res);
    }
}