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
use crate::resp3::utils as resp3_utils;
use crate::types::{Redirection, RedisProtocolError, RedisProtocolErrorKind};
use crate::utils;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet, VecDeque};
use std::convert::{TryFrom, TryInto};
use std::hash::{Hash, Hasher};
use std::mem;
use std::str;

#[cfg(feature = "index-map")]
use indexmap::{IndexMap, IndexSet};

/// Byte prefix before a simple string type.
pub const SIMPLE_STRING_BYTE: u8 = b'+';
/// Byte prefix before a simple error type.
pub const SIMPLE_ERROR_BYTE: u8 = b'-';
/// Byte prefix before a Number type.
pub const NUMBER_BYTE: u8 = b':';
/// Byte prefix before a Double type.
pub const DOUBLE_BYTE: u8 = b',';
/// Byte prefix before a blob string type.
pub const BLOB_STRING_BYTE: u8 = b'$';
/// Byte prefix before a blob error type.
pub const BLOB_ERROR_BYTE: u8 = b'!';
/// Byte prefix before a boolean type.
pub const BOOLEAN_BYTE: u8 = b'#';
/// Byte prefix before a verbatim string type.
pub const VERBATIM_STRING_BYTE: u8 = b'=';
/// Byte prefix before a big number type.
pub const BIG_NUMBER_BYTE: u8 = b'(';
/// Byte prefix before an array type.
pub const ARRAY_BYTE: u8 = b'*';
/// Byte prefix before a map type.
pub const MAP_BYTE: u8 = b'%';
/// Byte prefix before a set type.
pub const SET_BYTE: u8 = b'~';
/// Byte prefix before an attribute type.
pub const ATTRIBUTE_BYTE: u8 = b'|';
/// Byte prefix before a push type.
pub const PUSH_BYTE: u8 = b'>';
/// Byte prefix before a NULL value.
pub const NULL_BYTE: u8 = b'_';
/// Byte used to separate the verbatim string from the format prefix.
pub const VERBATIM_FORMAT_BYTE: u8 = b':';
/// Byte representation of a chunked string.
pub const CHUNKED_STRING_BYTE: u8 = b';';
/// Byte used to signify the end of a stream.
pub const END_STREAM_BYTE: u8 = b'.';

/// Byte prefix on a streamed type, following the byte that identifies the type.
pub const STREAMED_LENGTH_BYTE: u8 = b'?';
/// The terminating bytes when a streaming operation is done from a blob string.
pub const END_STREAM_STRING_BYTES: &'static str = ";0\r\n";
/// The terminating bytes when a streaming operation is done from an aggregate type.
pub const END_STREAM_AGGREGATE_BYTES: &'static str = ".\r\n";

/// The binary representation of NULL in RESP3.
pub const NULL: &'static str = "_\r\n";

/// Byte representation of positive infinity.
pub const INFINITY: &'static str = "inf";
/// Byte representation of negative infinity.
pub const NEG_INFINITY: &'static str = "-inf";

/// Byte representation of HELLO.
pub const HELLO: &'static str = "HELLO";
/// Byte representation of `true`.
pub const BOOL_TRUE_BYTES: &'static str = "#t\r\n";
/// Byte representation of `false`.
pub const BOOL_FALSE_BYTES: &'static str = "#f\r\n";
/// Byte representation of an empty space.
pub const EMPTY_SPACE: &'static str = " ";
/// Byte representation of `AUTH`.
pub const AUTH: &'static str = "AUTH";

pub use crate::utils::{PATTERN_PUBSUB_PREFIX, PUBSUB_PREFIX, PUBSUB_PUSH_PREFIX};

/// A map struct for frames.
#[cfg(not(feature = "index-map"))]
pub type FrameMap = HashMap<Frame, Frame>;
/// A set struct for frames.
#[cfg(not(feature = "index-map"))]
pub type FrameSet = HashSet<Frame>;
/// A map struct for frames.
#[cfg(feature = "index-map")]
pub type FrameMap = IndexMap<Frame, Frame>;
/// A set struct for frames.
#[cfg(feature = "index-map")]
pub type FrameSet = IndexSet<Frame>;

/// Additional information returned alongside a frame.
pub type Attributes = FrameMap;

/// Enum describing the byte ordering for numbers and doubles when cast to byte slices.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ByteOrder {
  BigEndian,
  LittleEndian,
}

/// The RESP version used in the `HELLO` request.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RespVersion {
  RESP2,
  RESP3,
}

impl RespVersion {
  pub fn to_byte(&self) -> u8 {
    match *self {
      RespVersion::RESP2 => b'2',
      RespVersion::RESP3 => b'3',
    }
  }
}

/// Authentication information used in the `HELLO` request.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Auth {
  pub username: Cow<'static, str>,
  pub password: Cow<'static, str>,
}

impl Auth {
  /// Create an [Auth] struct using the "default" user with the provided password.
  pub fn from_password<S: Into<String>>(password: S) -> Auth {
    Auth {
      username: Cow::Borrowed("default"),
      password: Cow::Owned(password.into()),
    }
  }
}

/// The format of a verbatim string frame.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum VerbatimStringFormat {
  Text,
  Markdown,
}

impl VerbatimStringFormat {
  pub(crate) fn to_str(&self) -> &'static str {
    match *self {
      VerbatimStringFormat::Text => "txt",
      VerbatimStringFormat::Markdown => "mkd",
    }
  }

  pub(crate) fn encode_len(&self) -> usize {
    match *self {
      // the colon suffix is included here
      VerbatimStringFormat::Text => 4,
      VerbatimStringFormat::Markdown => 4,
    }
  }
}

/// The type of frame without any associated data.
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd, Copy)]
pub enum FrameKind {
  Array,
  BlobString,
  SimpleString,
  SimpleError,
  Number,
  Null,
  Double,
  Boolean,
  BlobError,
  VerbatimString,
  Map,
  Set,
  Attribute,
  Push,
  Hello,
  BigNumber,
  ChunkedString,
  EndStream,
}

impl FrameKind {
  /// Whether or not the frame is an aggregate type (array, set, map).
  pub fn is_aggregate_type(&self) -> bool {
    match *self {
      FrameKind::Array | FrameKind::Set | FrameKind::Map => true,
      _ => false,
    }
  }

  /// Whether or not the frame is an aggregate type or blob string.
  pub fn is_streaming_type(&self) -> bool {
    match *self {
      FrameKind::Array | FrameKind::Set | FrameKind::Map | FrameKind::BlobString => true,
      _ => false,
    }
  }

  /// A function used to differentiate data types that may have the same inner binary representation when hashing a `Frame`.
  pub fn hash_prefix(&self) -> &'static str {
    use self::FrameKind::*;

    match *self {
      Array => "a",
      BlobString => "b",
      SimpleString => "s",
      SimpleError => "e",
      Number => "n",
      Null => "N",
      Double => "d",
      Boolean => "B",
      BlobError => "E",
      VerbatimString => "v",
      Map => "m",
      Set => "S",
      Attribute => "A",
      Push => "p",
      Hello => "h",
      BigNumber => "bn",
      ChunkedString => "cs",
      EndStream => "es",
    }
  }

  /// Attempt to detect the type of the frame from the first byte.
  pub fn from_byte(d: u8) -> Option<FrameKind> {
    use self::FrameKind::*;

    match d {
      SIMPLE_STRING_BYTE => Some(SimpleString),
      SIMPLE_ERROR_BYTE => Some(SimpleError),
      NUMBER_BYTE => Some(Number),
      DOUBLE_BYTE => Some(Double),
      BLOB_STRING_BYTE => Some(BlobString),
      BLOB_ERROR_BYTE => Some(BlobError),
      BOOLEAN_BYTE => Some(Boolean),
      VERBATIM_STRING_BYTE => Some(VerbatimString),
      BIG_NUMBER_BYTE => Some(BigNumber),
      ARRAY_BYTE => Some(Array),
      MAP_BYTE => Some(Map),
      SET_BYTE => Some(Set),
      ATTRIBUTE_BYTE => Some(Attribute),
      PUSH_BYTE => Some(Push),
      NULL_BYTE => Some(Null),
      CHUNKED_STRING_BYTE => Some(ChunkedString),
      END_STREAM_BYTE => Some(EndStream),
      _ => None,
    }
  }

  /// Read the byte prefix for the associated frame type.
  pub fn to_byte(&self) -> u8 {
    use self::FrameKind::*;

    match *self {
      SimpleString => SIMPLE_STRING_BYTE,
      SimpleError => SIMPLE_ERROR_BYTE,
      Number => NUMBER_BYTE,
      Double => DOUBLE_BYTE,
      BlobString => BLOB_STRING_BYTE,
      BlobError => BLOB_ERROR_BYTE,
      Boolean => BOOLEAN_BYTE,
      VerbatimString => VERBATIM_STRING_BYTE,
      BigNumber => BIG_NUMBER_BYTE,
      Array => ARRAY_BYTE,
      Map => MAP_BYTE,
      Set => SET_BYTE,
      Attribute => ATTRIBUTE_BYTE,
      Push => PUSH_BYTE,
      Null => NULL_BYTE,
      ChunkedString => CHUNKED_STRING_BYTE,
      EndStream => END_STREAM_BYTE,
      Hello => panic!("HELLO does not have a byte prefix."),
    }
  }

  /// Whether or not the frame type is for a `HELLO` frame.
  ///
  /// `HELLO` is encoded differently than other frames so this is used to prevent panics in [to_byte](Self::to_byte).
  pub fn is_hello(&self) -> bool {
    match *self {
      FrameKind::Hello => true,
      _ => false,
    }
  }
}

/// An enum describing the possible data types in RESP3 along with the corresponding Rust data type to represent the payload.
///
/// <https://github.com/antirez/RESP3/blob/master/spec.md>
#[derive(Clone, Debug)]
pub enum Frame {
  /// A binary-safe blob.
  BlobString {
    data: Vec<u8>,
    attributes: Option<Attributes>,
  },
  /// A binary-safe blob representing an error.
  BlobError {
    data: Vec<u8>,
    attributes: Option<Attributes>,
  },
  /// A small non binary-safe string.
  SimpleString {
    data: String,
    attributes: Option<Attributes>,
  },
  /// A small non binary-safe string representing an error.
  SimpleError {
    data: String,
    attributes: Option<Attributes>,
  },
  /// A boolean type.
  Boolean { data: bool, attributes: Option<Attributes> },
  /// A null type.
  Null,
  /// A signed 64 bit integer.
  Number { data: i64, attributes: Option<Attributes> },
  /// A signed 64 bit floating point number.
  Double { data: f64, attributes: Option<Attributes> },
  /// A large number not representable as a `Number` or `Double`.
  ///
  /// This library does not attempt to parse this, nor does it offer any utilities to do so.
  BigNumber {
    data: Vec<u8>,
    attributes: Option<Attributes>,
  },
  /// A binary-safe string to be displayed without any escaping or filtering.
  VerbatimString {
    data: Vec<u8>,
    format: VerbatimStringFormat,
    attributes: Option<Attributes>,
  },
  /// An array of frames, arbitrarily nested.
  Array {
    data: Vec<Frame>,
    attributes: Option<Attributes>,
  },
  /// An unordered map of key-value pairs.
  ///
  /// According to the spec keys can be any other RESP3 data type. However, it doesn't make sense to implement `Hash` for certain Rust data types like
  /// `HashMap`, `Vec`, `HashSet`, etc, so this library limits the possible data types for keys to only those that can be hashed in a semi-sane way.
  ///
  /// For example, attempting to create a `Frame::Map<HashMap<Frame::Set<HashSet<Frame>>, Frame::Foo>>` from bytes will panic.
  Map {
    data: FrameMap,
    attributes: Option<Attributes>,
  },
  /// An unordered collection of other frames with a uniqueness constraint.
  Set {
    data: FrameSet,
    attributes: Option<Attributes>,
  },
  /// Out-of-band data to be returned to the caller if necessary.
  Push {
    data: Vec<Frame>,
    attributes: Option<Attributes>,
  },
  /// A special frame type used when first connecting to the server to describe the protocol version and optional credentials.
  Hello { version: RespVersion, auth: Option<Auth> },
  /// One chunk of a streaming string.
  ChunkedString(Vec<u8>),
}

impl Hash for Frame {
  fn hash<H: Hasher>(&self, state: &mut H) {
    use self::Frame::*;
    self.kind().hash_prefix().hash(state);

    match *self {
      BlobString { ref data, .. } => data.hash(state),
      SimpleString { ref data, .. } => data.hash(state),
      SimpleError { ref data, .. } => data.hash(state),
      Number { ref data, .. } => data.hash(state),
      Null => NULL.hash(state),
      Double { ref data, .. } => data.to_string().hash(state),
      Boolean { ref data, .. } => data.hash(state),
      BlobError { ref data, .. } => data.hash(state),
      VerbatimString {
        ref data, ref format, ..
      } => {
        format.hash(state);
        data.hash(state);
      }
      ChunkedString(ref data) => data.hash(state),
      BigNumber { ref data, .. } => data.hash(state),
      _ => panic!("Invalid RESP3 data type to use as hash key."),
    };
  }
}

impl PartialEq for Frame {
  fn eq(&self, other: &Self) -> bool {
    use self::Frame::*;

    match *self {
      ChunkedString(ref b) => match *other {
        ChunkedString(ref _b) => b == _b,
        _ => false,
      },
      Array {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          Array {
            ref data,
            attributes: _,
          } => data == _data && _attributes == _attributes,
          _ => false,
        }
      }
      BlobString {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          BlobString {
            ref data,
            attributes: _,
          } => data == _data && _attributes == _attributes,
          _ => false,
        }
      }
      SimpleString {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          SimpleString {
            ref data,
            attributes: _,
          } => data == _data && _attributes == _attributes,
          _ => false,
        }
      }
      SimpleError {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          SimpleError {
            ref data,
            attributes: _,
          } => data == _data && _attributes == _attributes,
          _ => false,
        }
      }
      Number {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          Number {
            ref data,
            attributes: _,
          } => data == _data && _attributes == _attributes,
          _ => false,
        }
      }
      Null => match *other {
        Null => true,
        _ => false,
      },
      Boolean {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          Boolean {
            ref data,
            attributes: _,
          } => data == _data && _attributes == _attributes,
          _ => false,
        }
      }
      Double {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          Double {
            ref data,
            attributes: _,
          } => data == _data && _attributes == _attributes,
          _ => false,
        }
      }
      BlobError {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          BlobError {
            ref data,
            attributes: _,
          } => data == _data && _attributes == _attributes,
          _ => false,
        }
      }
      VerbatimString {
        ref data,
        ref format,
        ref attributes,
      } => {
        let (_data, _format, _attributes) = (data, format, attributes);
        match *other {
          VerbatimString {
            ref data,
            ref format,
            ref attributes,
          } => _data == data && _format == format && attributes == _attributes,
          _ => false,
        }
      }
      Map {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          Map {
            ref data,
            attributes: _,
          } => data == _data && _attributes == _attributes,
          _ => false,
        }
      }
      Set {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          Set {
            ref data,
            attributes: _,
          } => data == _data && _attributes == _attributes,
          _ => false,
        }
      }
      Push {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          Push {
            ref data,
            attributes: _,
          } => data == _data && _attributes == _attributes,
          _ => false,
        }
      }
      Hello { ref version, ref auth } => {
        let (_version, _auth) = (version, auth);
        match *other {
          Hello { ref version, ref auth } => _version == version && _auth == auth,
          _ => false,
        }
      }
      BigNumber {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          BigNumber {
            ref data,
            attributes: _,
          } => data == _data && _attributes == _attributes,
          _ => false,
        }
      }
    }
  }
}

impl Eq for Frame {}

impl TryFrom<(FrameKind, Vec<u8>)> for Frame {
  type Error = RedisProtocolError;

  fn try_from((kind, value): (FrameKind, Vec<u8>)) -> Result<Self, Self::Error> {
    let frame = match kind {
      FrameKind::BlobString => Frame::BlobString {
        data: value,
        attributes: None,
      },
      FrameKind::BlobError => Frame::BlobError {
        data: value,
        attributes: None,
      },
      FrameKind::BigNumber => Frame::BigNumber {
        data: value,
        attributes: None,
      },
      FrameKind::ChunkedString => Frame::ChunkedString(value),
      _ => {
        return Err(RedisProtocolError::new(
          RedisProtocolErrorKind::Unknown,
          "Cannot convert to Frame.",
        ))
      }
    };

    Ok(frame)
  }
}

impl TryFrom<(FrameKind, Vec<Frame>)> for Frame {
  type Error = RedisProtocolError;

  fn try_from((kind, data): (FrameKind, Vec<Frame>)) -> Result<Self, Self::Error> {
    let frame = match kind {
      FrameKind::Array => Frame::Array { data, attributes: None },
      FrameKind::Push => Frame::Push { data, attributes: None },
      _ => {
        return Err(RedisProtocolError::new(
          RedisProtocolErrorKind::Unknown,
          "Cannot convert to Frame.",
        ))
      }
    };

    Ok(frame)
  }
}

impl TryFrom<(FrameKind, VecDeque<Frame>)> for Frame {
  type Error = RedisProtocolError;

  fn try_from((kind, value): (FrameKind, VecDeque<Frame>)) -> Result<Self, Self::Error> {
    let data: Vec<Frame> = value.into_iter().collect();

    let frame = match kind {
      FrameKind::Array => Frame::Array { data, attributes: None },
      FrameKind::Push => Frame::Push { data, attributes: None },
      _ => {
        return Err(RedisProtocolError::new(
          RedisProtocolErrorKind::Unknown,
          "Cannot convert to Frame.",
        ))
      }
    };

    Ok(frame)
  }
}

impl TryFrom<HashMap<Frame, Frame>> for Frame {
  type Error = RedisProtocolError;

  fn try_from(value: HashMap<Frame, Frame>) -> Result<Self, Self::Error> {
    Ok(Frame::Map {
      data: resp3_utils::hashmap_to_frame_map(value),
      attributes: None,
    })
  }
}

impl TryFrom<HashSet<Frame>> for Frame {
  type Error = RedisProtocolError;

  fn try_from(value: HashSet<Frame>) -> Result<Self, Self::Error> {
    Ok(Frame::Set {
      data: resp3_utils::hashset_to_frame_set(value),
      attributes: None,
    })
  }
}

#[cfg(feature = "index-map")]
impl TryFrom<IndexMap<Frame, Frame>> for Frame {
  type Error = RedisProtocolError;

  fn try_from(value: IndexMap<Frame, Frame>) -> Result<Self, Self::Error> {
    Ok(Frame::Map {
      data: value,
      attributes: None,
    })
  }
}

#[cfg(feature = "index-map")]
impl TryFrom<IndexSet<Frame>> for Frame {
  type Error = RedisProtocolError;

  fn try_from(value: IndexSet<Frame>) -> Result<Self, Self::Error> {
    Ok(Frame::Set {
      data: value,
      attributes: None,
    })
  }
}

impl From<i64> for Frame {
  fn from(value: i64) -> Self {
    Frame::Number {
      data: value,
      attributes: None,
    }
  }
}

impl From<bool> for Frame {
  fn from(value: bool) -> Self {
    Frame::Boolean {
      data: value,
      attributes: None,
    }
  }
}

impl TryFrom<f64> for Frame {
  type Error = RedisProtocolError;

  fn try_from(value: f64) -> Result<Self, Self::Error> {
    if value.is_nan() {
      Err(RedisProtocolError::new(
        RedisProtocolErrorKind::Unknown,
        "Cannot cast NaN to double.",
      ))
    } else {
      Ok(Frame::Double {
        data: value,
        attributes: None,
      })
    }
  }
}

impl TryFrom<(FrameKind, String)> for Frame {
  type Error = RedisProtocolError;

  fn try_from((kind, value): (FrameKind, String)) -> Result<Self, Self::Error> {
    let frame = match kind {
      FrameKind::SimpleError => Frame::SimpleError {
        data: value,
        attributes: None,
      },
      FrameKind::SimpleString => Frame::SimpleString {
        data: value,
        attributes: None,
      },
      FrameKind::BlobError => Frame::BlobError {
        data: value.into_bytes(),
        attributes: None,
      },
      FrameKind::BlobString => Frame::BlobString {
        data: value.into_bytes(),
        attributes: None,
      },
      FrameKind::BigNumber => Frame::BigNumber {
        data: value.into_bytes(),
        attributes: None,
      },
      FrameKind::ChunkedString => Frame::ChunkedString(value.into_bytes()),
      _ => {
        return Err(RedisProtocolError::new(
          RedisProtocolErrorKind::Unknown,
          "Cannot convert to Frame.",
        ))
      }
    };

    Ok(frame)
  }
}

impl<'a> TryFrom<(FrameKind, &'a str)> for Frame {
  type Error = RedisProtocolError;

  fn try_from((kind, value): (FrameKind, &'a str)) -> Result<Self, Self::Error> {
    (kind, value.to_owned()).try_into()
  }
}

impl Frame {
  /// Whether or not the frame can be used as a key in a `HashMap` or `HashSet`.
  ///
  /// Not all frame types can be hashed, and trying to do so can panic. This function can be used to handle this gracefully.
  pub fn can_hash(&self) -> bool {
    match self.kind() {
      FrameKind::BlobString
      | FrameKind::BlobError
      | FrameKind::SimpleString
      | FrameKind::SimpleError
      | FrameKind::Number
      | FrameKind::Double
      | FrameKind::Boolean
      | FrameKind::Null
      | FrameKind::ChunkedString
      | FrameKind::EndStream
      | FrameKind::VerbatimString
      | FrameKind::BigNumber => true,
      _ => false,
    }
  }

  /// Read the attributes attached to the frame.
  pub fn attributes(&self) -> Option<&Attributes> {
    let attributes = match *self {
      Frame::Array { ref attributes, .. } => attributes,
      Frame::Push { ref attributes, .. } => attributes,
      Frame::BlobString { ref attributes, .. } => attributes,
      Frame::BlobError { ref attributes, .. } => attributes,
      Frame::BigNumber { ref attributes, .. } => attributes,
      Frame::Boolean { ref attributes, .. } => attributes,
      Frame::Number { ref attributes, .. } => attributes,
      Frame::Double { ref attributes, .. } => attributes,
      Frame::VerbatimString { ref attributes, .. } => attributes,
      Frame::SimpleError { ref attributes, .. } => attributes,
      Frame::SimpleString { ref attributes, .. } => attributes,
      Frame::Set { ref attributes, .. } => attributes,
      Frame::Map { ref attributes, .. } => attributes,
      Frame::Null | Frame::ChunkedString(_) | Frame::Hello { .. } => return None,
    };

    attributes.as_ref()
  }

  /// Take the attributes off this frame.
  pub fn take_attributes(&mut self) -> Option<Attributes> {
    let attributes = match *self {
      Frame::Array { ref mut attributes, .. } => attributes,
      Frame::Push { ref mut attributes, .. } => attributes,
      Frame::BlobString { ref mut attributes, .. } => attributes,
      Frame::BlobError { ref mut attributes, .. } => attributes,
      Frame::BigNumber { ref mut attributes, .. } => attributes,
      Frame::Boolean { ref mut attributes, .. } => attributes,
      Frame::Number { ref mut attributes, .. } => attributes,
      Frame::Double { ref mut attributes, .. } => attributes,
      Frame::VerbatimString { ref mut attributes, .. } => attributes,
      Frame::SimpleError { ref mut attributes, .. } => attributes,
      Frame::SimpleString { ref mut attributes, .. } => attributes,
      Frame::Set { ref mut attributes, .. } => attributes,
      Frame::Map { ref mut attributes, .. } => attributes,
      Frame::Null | Frame::ChunkedString(_) | Frame::Hello { .. } => return None,
    };

    attributes.take()
  }

  /// Read a mutable reference to any attributes attached to the frame.
  pub fn attributes_mut(&mut self) -> Option<&mut Attributes> {
    let attributes = match *self {
      Frame::Array { ref mut attributes, .. } => attributes,
      Frame::Push { ref mut attributes, .. } => attributes,
      Frame::BlobString { ref mut attributes, .. } => attributes,
      Frame::BlobError { ref mut attributes, .. } => attributes,
      Frame::BigNumber { ref mut attributes, .. } => attributes,
      Frame::Boolean { ref mut attributes, .. } => attributes,
      Frame::Number { ref mut attributes, .. } => attributes,
      Frame::Double { ref mut attributes, .. } => attributes,
      Frame::VerbatimString { ref mut attributes, .. } => attributes,
      Frame::SimpleError { ref mut attributes, .. } => attributes,
      Frame::SimpleString { ref mut attributes, .. } => attributes,
      Frame::Set { ref mut attributes, .. } => attributes,
      Frame::Map { ref mut attributes, .. } => attributes,
      Frame::Null | Frame::ChunkedString(_) | Frame::Hello { .. } => return None,
    };

    attributes.as_mut()
  }

  /// Attempt to add attributes to the frame, extending the existing attributes if needed.
  pub fn add_attributes(&mut self, attributes: Attributes) -> Result<(), RedisProtocolError> {
    let _attributes = match *self {
      Frame::Array { ref mut attributes, .. } => attributes,
      Frame::Push { ref mut attributes, .. } => attributes,
      Frame::BlobString { ref mut attributes, .. } => attributes,
      Frame::BlobError { ref mut attributes, .. } => attributes,
      Frame::BigNumber { ref mut attributes, .. } => attributes,
      Frame::Boolean { ref mut attributes, .. } => attributes,
      Frame::Number { ref mut attributes, .. } => attributes,
      Frame::Double { ref mut attributes, .. } => attributes,
      Frame::VerbatimString { ref mut attributes, .. } => attributes,
      Frame::SimpleError { ref mut attributes, .. } => attributes,
      Frame::SimpleString { ref mut attributes, .. } => attributes,
      Frame::Set { ref mut attributes, .. } => attributes,
      Frame::Map { ref mut attributes, .. } => attributes,
      Frame::Null | Frame::ChunkedString(_) | Frame::Hello { .. } => {
        return Err(RedisProtocolError::new(
          RedisProtocolErrorKind::Unknown,
          format!("{:?} cannot have attributes.", self.kind()),
        ))
      }
    };

    if let Some(_attributes) = _attributes.as_mut() {
      _attributes.extend(attributes.into_iter());
    } else {
      *_attributes = Some(attributes);
    }

    Ok(())
  }

  /// Create a new `Frame` that terminates a stream.
  pub fn new_end_stream() -> Self {
    Frame::ChunkedString(vec![])
  }

  /// A context-aware length function that returns the length of the inner frame contents.
  ///
  /// This does not return the encoded length, but rather the length of the contents of the frame such as the number of elements in an array, the size of any inner buffers, etc.
  ///
  /// Note: `Null` has a length of 0 and `Hello`, `Number`, `Double`, and `Boolean` have a length of 1.
  ///
  /// See [encode_len](Self::encode_len) to read the number of bytes necessary to encode the frame.
  pub fn len(&self) -> usize {
    use self::Frame::*;

    match *self {
      Array { ref data, .. } | Push { ref data, .. } => data.len(),
      BlobString { ref data, .. }
      | BlobError { ref data, .. }
      | BigNumber { ref data, .. }
      | ChunkedString(ref data) => data.len(),
      SimpleString { ref data, .. } | SimpleError { ref data, .. } => data.len(),
      Number { .. } | Double { .. } | Boolean { .. } => 1,
      Null => 0,
      VerbatimString { ref data, .. } => data.len(),
      Map { ref data, .. } => data.len(),
      Set { ref data, .. } => data.len(),
      Hello { .. } => 1,
    }
  }

  /// Replace `self` with Null, returning the original value.
  pub fn take(&mut self) -> Frame {
    mem::replace(self, Frame::Null)
  }

  /// Read the associated `FrameKind`.
  pub fn kind(&self) -> FrameKind {
    use self::Frame::*;

    match *self {
      Array { .. } => FrameKind::Array,
      BlobString { .. } => FrameKind::BlobString,
      SimpleString { .. } => FrameKind::SimpleString,
      SimpleError { .. } => FrameKind::SimpleError,
      Number { .. } => FrameKind::Number,
      Null => FrameKind::Null,
      Double { .. } => FrameKind::Double,
      BlobError { .. } => FrameKind::BlobError,
      VerbatimString { .. } => FrameKind::VerbatimString,
      Boolean { .. } => FrameKind::Boolean,
      Map { .. } => FrameKind::Map,
      Set { .. } => FrameKind::Set,
      Push { .. } => FrameKind::Push,
      Hello { .. } => FrameKind::Hello,
      BigNumber { .. } => FrameKind::BigNumber,
      ChunkedString(ref inner) => {
        if inner.is_empty() {
          FrameKind::EndStream
        } else {
          FrameKind::ChunkedString
        }
      }
    }
  }

  /// Whether or not the frame is a `Null` variant.
  pub fn is_null(&self) -> bool {
    match *self {
      Frame::Null => true,
      _ => false,
    }
  }

  /// Whether or not the frame represents an array of frames.
  pub fn is_array(&self) -> bool {
    match *self {
      Frame::Array { .. } => true,
      _ => false,
    }
  }

  /// Whether or not the frame represents data pushed to the client from the server.
  pub fn is_push(&self) -> bool {
    match *self {
      Frame::Push { .. } => true,
      _ => false,
    }
  }

  /// Whether or not the frame is a boolean value.
  pub fn is_boolean(&self) -> bool {
    match *self {
      Frame::Boolean { .. } => true,
      _ => false,
    }
  }

  /// Whether or not the frame represents an error.
  pub fn is_error(&self) -> bool {
    match *self {
      Frame::BlobError { .. } | Frame::SimpleError { .. } => true,
      _ => false,
    }
  }

  /// Whether or not the frame is an array, map, or set.
  pub fn is_aggregate_type(&self) -> bool {
    match *self {
      Frame::Map { .. } | Frame::Set { .. } | Frame::Array { .. } => true,
      _ => false,
    }
  }

  /// Whether or not the frame represents a `BlobString` or `BlobError`.
  pub fn is_blob(&self) -> bool {
    match *self {
      Frame::BlobString { .. } | Frame::BlobError { .. } => true,
      _ => false,
    }
  }

  /// Whether or not the frame represents a chunked string.
  pub fn is_chunked_string(&self) -> bool {
    match *self {
      Frame::ChunkedString(_) => true,
      _ => false,
    }
  }

  /// Whether or not the frame is an empty chunked string, signifying the end of a chunked string stream.
  pub fn is_end_stream_frame(&self) -> bool {
    match *self {
      Frame::ChunkedString(ref s) => s.is_empty(),
      _ => false,
    }
  }

  /// Whether or not the frame is a verbatim string.
  pub fn is_verbatim_string(&self) -> bool {
    match *self {
      Frame::VerbatimString { .. } => true,
      _ => false,
    }
  }

  /// If the frame is a verbatim string then read the associated format.
  pub fn verbatim_string_format(&self) -> Option<&VerbatimStringFormat> {
    match *self {
      Frame::VerbatimString { ref format, .. } => Some(format),
      _ => None,
    }
  }

  /// Read the frame as a string slice if it can be parsed as a UTF-8 string without allocating.
  ///
  /// Numbers and Doubles will not be cast to a string since that would require allocating.
  pub fn as_str(&self) -> Option<&str> {
    match *self {
      Frame::SimpleError { ref data, .. } | Frame::SimpleString { ref data, .. } => Some(data),
      Frame::BlobError { ref data, .. } | Frame::BlobString { ref data, .. } | Frame::BigNumber { ref data, .. } => {
        str::from_utf8(data).ok()
      }
      Frame::VerbatimString { ref data, .. } => str::from_utf8(data).ok(),
      Frame::ChunkedString(ref data) => str::from_utf8(data).ok(),
      _ => None,
    }
  }

  /// Read the frame as a `String` if it can be parsed as a UTF-8 string.
  pub fn to_string(&self) -> Option<String> {
    match *self {
      Frame::SimpleError { ref data, .. } | Frame::SimpleString { ref data, .. } => Some(data.to_owned()),
      Frame::BlobError { ref data, .. } | Frame::BlobString { ref data, .. } | Frame::BigNumber { ref data, .. } => {
        String::from_utf8(data.to_vec()).ok()
      }
      Frame::VerbatimString { ref data, .. } => String::from_utf8(data.to_vec()).ok(),
      Frame::ChunkedString(ref b) => String::from_utf8(b.to_vec()).ok(),
      Frame::Double { ref data, .. } => Some(data.to_string()),
      Frame::Number { ref data, .. } => Some(data.to_string()),
      _ => None,
    }
  }

  /// Attempt to read the frame as a byte slice.
  ///
  /// Number and Double will not be returned as a byte slice. Use [number_or_double_as_bytes](Self::number_or_double_as_bytes) instead.
  pub fn as_bytes(&self) -> Option<&[u8]> {
    match *self {
      Frame::SimpleError { ref data, .. } | Frame::SimpleString { ref data, .. } => Some(data.as_bytes()),
      Frame::BlobError { ref data, .. } | Frame::BlobString { ref data, .. } | Frame::BigNumber { ref data, .. } => {
        Some(data)
      }
      Frame::VerbatimString { ref data, .. } => Some(data),
      Frame::ChunkedString(ref b) => Some(b),
      _ => None,
    }
  }

  /// Attempt the read the frame as bytes if the inner type is an `i64` or `f64`.
  pub fn number_or_double_as_bytes(&self, ordering: ByteOrder) -> Option<[u8; 8]> {
    match *self {
      Frame::Double { ref data, .. } => Some(match ordering {
        ByteOrder::BigEndian => data.to_be_bytes(),
        ByteOrder::LittleEndian => data.to_le_bytes(),
      }),
      Frame::Number { ref data, .. } => Some(match ordering {
        ByteOrder::BigEndian => data.to_be_bytes(),
        ByteOrder::LittleEndian => data.to_le_bytes(),
      }),
      _ => None,
    }
  }

  /// Attempt to read the frame as an `i64`.
  pub fn as_i64(&self) -> Option<i64> {
    match *self {
      Frame::Number { ref data, .. } => Some(*data),
      Frame::Double { ref data, .. } => Some(*data as i64),
      Frame::BlobString { ref data, .. } => str::from_utf8(data).ok().and_then(|s| s.parse::<i64>().ok()),
      Frame::SimpleString { ref data, .. } => data.parse::<i64>().ok(),
      _ => None,
    }
  }

  /// Attempt to read the frame as an `f64`.
  pub fn as_f64(&self) -> Option<f64> {
    match *self {
      Frame::Double { ref data, .. } => Some(*data),
      Frame::Number { ref data, .. } => Some(*data as f64),
      Frame::BlobString { ref data, .. } => str::from_utf8(data).ok().and_then(|s| s.parse::<f64>().ok()),
      Frame::SimpleString { ref data, .. } => data.parse::<f64>().ok(),
      _ => None,
    }
  }

  /// Whether or not the frame represents a MOVED or ASK error.
  pub fn is_moved_or_ask_error(&self) -> bool {
    match *self {
      Frame::SimpleError { ref data, .. } => utils::is_cluster_error(data),
      _ => false,
    }
  }

  /// Attempt to parse the frame as a cluster redirection error.
  pub fn to_redirection(&self) -> Option<Redirection> {
    match *self {
      Frame::SimpleError { ref data, .. } => utils::read_cluster_error(data),
      _ => None,
    }
  }

  /// Whether or not the frame represents a publish-subscribe message, but not a pattern publish-subscribe message.
  pub fn is_normal_pubsub(&self) -> bool {
    if let Frame::Push { ref data, .. } = *self {
      resp3_utils::is_normal_pubsub(data)
    } else {
      false
    }
  }

  /// Whether or not the frame represents a message on a publish-subscribe channel.
  pub fn is_pubsub_message(&self) -> bool {
    if let Frame::Push { ref data, .. } = *self {
      resp3_utils::is_normal_pubsub(data) || resp3_utils::is_pattern_pubsub(data)
    } else {
      false
    }
  }

  /// Whether or not the frame represents a message on a publish-subscribe channel matched against a pattern subscription.
  pub fn is_pattern_pubsub_message(&self) -> bool {
    if let Frame::Push { ref data, .. } = *self {
      resp3_utils::is_pattern_pubsub(data)
    } else {
      false
    }
  }

  /// Attempt to parse the frame as a publish-subscribe message, returning the `(channel, message)` tuple
  /// if successful, or the original frame if the inner data is not a publish-subscribe message.
  pub fn parse_as_pubsub(self) -> Result<(Frame, Frame), Self> {
    if self.is_pubsub_message() {
      if let Frame::Push { mut data, .. } = self {
        // array len checked in `is_pubsub_message`
        let message = data.pop().unwrap();
        let channel = data.pop().unwrap();

        Ok((channel, message))
      } else {
        warn!("Invalid pubsub frame. Expected a Push frame.");
        Err(self)
      }
    } else {
      Err(self)
    }
  }

  /// Attempt to read the number of bytes needed to encode the frame.
  pub fn encode_len(&self) -> Result<usize, RedisProtocolError> {
    resp3_utils::encode_len(self).map_err(|e| e.into())
  }
}

/// A helper struct for reading and managing streaming data types.
///
/// ```rust edition2018
/// use redis_protocol::resp3::decode::streaming::decode;
///
/// fn main() {
///   let parts = vec!["*?\r\n", ":1\r\n", ":2\r\n:3\r\n", ".\r\n"];
///
///   let (frame, _) = decode(parts[0].as_bytes()).unwrap().unwrap();
///   assert!(frame.is_streaming());
///   let mut streaming = frame.into_streaming_frame().unwrap();
///   println!("Reading streaming {:?}", streaming.kind);
///
///   let (frame, _) = decode(parts[1].as_bytes()).unwrap().unwrap();
///   assert!(frame.is_complete());
///   // add frames to the buffer until we reach the terminating byte sequence
///   streaming.add_frame(frame.into_complete_frame().unwrap());
///
///   let (frame, _) = decode(parts[2].as_bytes()).unwrap().unwrap();
///   assert!(frame.is_complete());
///   streaming.add_frame(frame.into_complete_frame().unwrap());
///
///   let (frame, _) = decode(parts[3].as_bytes()).unwrap().unwrap();
///   assert!(frame.is_complete());
///   streaming.add_frame(frame.into_complete_frame().unwrap());
///
///   assert!(streaming.is_finished());
///   // convert the buffer into one frame
///   let result = streaming.into_frame().unwrap();
///
///   // Frame::Array { data: [1, 2, 3], attributes: None }
///   println!("{:?}", result);
/// }
/// ```
#[derive(Debug, Eq, PartialEq)]
pub struct StreamedFrame {
  /// The internal buffer of frames and attributes.
  buffer: VecDeque<Frame>,
  /// Any leading attributes before the stream starts.
  pub attributes: Option<Attributes>,
  /// The data type being streamed.  
  pub kind: FrameKind,
}

impl StreamedFrame {
  /// Create a new `StreamedFrame` from the first section of data in a streaming response.
  pub fn new(kind: FrameKind) -> Self {
    let buffer = VecDeque::new();
    StreamedFrame {
      buffer,
      kind,
      attributes: None,
    }
  }

  /// Convert the internal buffer into one frame matching `self.kind`, clearing the internal buffer.
  pub fn into_frame(&mut self) -> Result<Frame, RedisProtocolError> {
    if !self.kind.is_streaming_type() {
      // try to catch invalid type errors early so the caller can modify the frame before we clear the buffer
      return Err(RedisProtocolError::new(
        RedisProtocolErrorKind::DecodeError,
        "Only blob strings, sets, maps, and arrays can be streamed.",
      ));
    }

    if self.is_finished() {
      // the last frame is an empty chunked string when the stream is finished
      let _ = self.buffer.pop_back();
    }
    let buffer = mem::replace(&mut self.buffer, VecDeque::new());
    let attributes = self.attributes.take();

    let frame = match self.kind {
      FrameKind::BlobString => resp3_utils::reconstruct_blobstring(buffer, attributes)?,
      FrameKind::Map => resp3_utils::reconstruct_map(buffer, attributes)?,
      FrameKind::Set => resp3_utils::reconstruct_set(buffer, attributes)?,
      FrameKind::Array => resp3_utils::reconstruct_array(buffer, attributes)?,
      _ => {
        return Err(RedisProtocolError::new(
          RedisProtocolErrorKind::DecodeError,
          "Streaming frames only supported for blob strings, maps, sets, and arrays.",
        ))
      }
    };

    Ok(frame)
  }

  /// Add a frame to the internal buffer.
  pub fn add_frame(&mut self, data: Frame) {
    self.buffer.push_back(data);
  }

  /// Whether or not the last frame represents the terminating sequence at the end of a frame stream.
  pub fn is_finished(&self) -> bool {
    self.buffer.back().map(|f| f.is_end_stream_frame()).unwrap_or(false)
  }
}

/// Wrapper enum around a decoded frame that supports streaming frames.
#[derive(Debug, Eq, PartialEq)]
pub enum DecodedFrame {
  Streaming(StreamedFrame),
  Complete(Frame),
}

impl DecodedFrame {
  /// Add attributes to the decoded frame, if possible.
  pub fn add_attributes(&mut self, attributes: Attributes) -> Result<(), RedisProtocolError> {
    let _ = match *self {
      DecodedFrame::Streaming(ref mut inner) => inner.attributes = Some(attributes),
      DecodedFrame::Complete(ref mut inner) => inner.add_attributes(attributes)?,
    };

    Ok(())
  }

  /// Convert the decoded frame to a complete frame, returning an error if a streaming variant is found.
  pub fn into_complete_frame(self) -> Result<Frame, RedisProtocolError> {
    match self {
      DecodedFrame::Complete(frame) => Ok(frame),
      DecodedFrame::Streaming(_) => Err(RedisProtocolError::new(
        RedisProtocolErrorKind::DecodeError,
        "Expected complete frame.",
      )),
    }
  }

  /// Convert the decoded frame into a streaming frame, returning an error if a complete variant is found.
  pub fn into_streaming_frame(self) -> Result<StreamedFrame, RedisProtocolError> {
    match self {
      DecodedFrame::Streaming(frame) => Ok(frame),
      DecodedFrame::Complete(_) => Err(RedisProtocolError::new(
        RedisProtocolErrorKind::DecodeError,
        "Expected streamed frame.",
      )),
    }
  }

  /// Whether or not the decoded frame starts a stream.
  pub fn is_streaming(&self) -> bool {
    match *self {
      DecodedFrame::Streaming(_) => true,
      _ => false,
    }
  }

  /// Whether or not the decoded frame is a complete frame.
  pub fn is_complete(&self) -> bool {
    !self.is_streaming()
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::resp3::utils::new_map;

  #[test]
  fn should_convert_basic_streaming_buffer_to_frame() {
    let mut streaming_buf = StreamedFrame::new(FrameKind::BlobString);
    streaming_buf.add_frame((FrameKind::ChunkedString, "foo").try_into().unwrap());
    streaming_buf.add_frame((FrameKind::ChunkedString, "bar").try_into().unwrap());
    streaming_buf.add_frame((FrameKind::ChunkedString, "baz").try_into().unwrap());
    streaming_buf.add_frame(Frame::new_end_stream());
    let frame = streaming_buf
      .into_frame()
      .expect("Failed to build frame from chunked stream");

    assert_eq!(frame.as_str(), Some("foobarbaz"));
  }

  #[test]
  fn should_convert_basic_streaming_buffer_to_frame_with_attributes() {
    let mut attributes = new_map(None);
    attributes.insert((FrameKind::SimpleString, "a").try_into().unwrap(), 1.into());
    attributes.insert((FrameKind::SimpleString, "b").try_into().unwrap(), 2.into());
    attributes.insert((FrameKind::SimpleString, "c").try_into().unwrap(), 3.into());

    let mut streaming_buf = StreamedFrame::new(FrameKind::BlobString);
    streaming_buf.attributes = Some(attributes.clone());

    streaming_buf.add_frame((FrameKind::ChunkedString, "foo").try_into().unwrap());
    streaming_buf.add_frame((FrameKind::ChunkedString, "bar").try_into().unwrap());
    streaming_buf.add_frame((FrameKind::ChunkedString, "baz").try_into().unwrap());
    streaming_buf.add_frame(Frame::new_end_stream());

    let frame = streaming_buf
      .into_frame()
      .expect("Failed to build frame from chunked stream");

    assert_eq!(frame.as_str(), Some("foobarbaz"));
    assert_eq!(frame.attributes(), Some(&attributes));
  }
}