Skip to content

Commit f9c716a

Browse files
committed
Use a separate Array enumeration
1 parent 376c05e commit f9c716a

File tree

5 files changed

+53
-26
lines changed

5 files changed

+53
-26
lines changed

.github/workflows/test.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ jobs:
1212
steps:
1313
- uses: actions/checkout@v2
1414
- name: Build
15-
run: cargo build --verbose --all-features
15+
run: |
16+
cargo build --verbose --no-defeault-features --features sync
17+
cargo build --verbose --no-defeault-features --features sync,ssl
18+
cargo build --verbose --no-defeault-features --features sync,sslv
19+
cargo build --verbose --no-defeault-features --features async
20+
cargo build --verbose --no-defeault-features --features async,aio-ssl
21+
cargo build --verbose --no-defeault-features --features async,aio-sslv
22+
cargo build --verbose --no-defeault-features --features dbg
23+
cargo build --verbose --no-defeault-features --features const-gen
1624
- name: Run tests
1725
run: cargo test --all-features

src/actions.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub const ERR_SNAPSHOT_DISABLED: &str = "err-snapshot-disabled";
6161

6262
/// Errors while running actions
6363
#[derive(Debug)]
64+
#[non_exhaustive]
6465
pub enum ActionError {
6566
/// The server sent data but we failed to parse it
6667
ParseError,
@@ -190,16 +191,16 @@ implement_actions!(
190191
/// Do note that the order might be completely meaningless
191192
fn lskeys(count: usize) -> Vec<FlatElement> {
192193
{ Query::from("lskeys").arg(count)}
193-
Response::Item(Element::FlatArray(arr)) => arr
194+
Response::Item(Element::Array(Array::Flat(arr))) => arr
194195
}
195196
/// Get multiple keys
196197
///
197198
/// This returns a vector of [`Element`]s which either contain the values
198199
/// as strings or contains `Not Found (Code: 1)` response codes
199200
fn mget(keys: impl IntoSkyhashAction+ 's) -> Array {
200201
{ Query::from("mget").arg(keys)}
201-
Response::Item(Element::BinArray(brr)) => Array::Bin(brr),
202-
Response::Item(Element::StrArray(srr)) => Array::Str(srr)
202+
Response::Item(Element::Array(Array::Bin(brr))) => Array::Bin(brr),
203+
Response::Item(Element::Array(Array::Str(srr))) => Array::Str(srr)
203204
}
204205
/// Creates a snapshot
205206
///
@@ -259,8 +260,8 @@ implement_actions!(
259260
/// Consumes the provided keys if they exist
260261
fn mpop(keys: impl IntoSkyhashAction + 's) -> Array {
261262
{ Query::from("mpop").arg(keys)}
262-
Response::Item(Element::BinArray(brr)) => Array::Bin(brr),
263-
Response::Item(Element::StrArray(srr)) => Array::Str(srr)
263+
Response::Item(Element::Array(Array::Bin(brr))) => Array::Bin(brr),
264+
Response::Item(Element::Array(Array::Str(srr))) => Array::Str(srr)
264265
}
265266
/// Deletes all the provided keys if they exist or doesn't do anything at all. This method
266267
/// will return true if all the provided keys were deleted, else it will return false

src/deserializer.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
//! by Sayan Nandan and this is the first client implementation of the protocol
2828
//!
2929
30+
use crate::types::Array;
3031
use crate::types::FlatElement;
3132
use crate::RespCode;
3233
use std::hint::unreachable_unchecked;
@@ -65,24 +66,19 @@ pub(super) struct Parser<'a> {
6566
/// This enum represents the data types supported by the Skyhash Protocol
6667
pub enum Element {
6768
/// Arrays can be nested! Their `<tsymbol>` is `&`
68-
Array(Vec<Element>),
69+
Array(Array),
6970
/// An unicode string value; `<tsymbol>` is `+`
7071
Str(String),
7172
/// A binary string (`?`)
7273
Binstr(Vec<u8>),
7374
/// An unsigned integer value; `<tsymbol>` is `:`
7475
UnsignedInt(u64),
75-
/// A non-recursive array; tsymbol: `_`
76-
FlatArray(Vec<FlatElement>),
7776
/// A response code
7877
RespCode(RespCode),
79-
/// An array of unicode strings
80-
StrArray(Vec<Option<String>>),
81-
/// An array of binary strings
82-
BinArray(Vec<Option<Vec<u8>>>),
8378
}
8479

8580
#[derive(Debug, PartialEq)]
81+
#[non_exhaustive]
8682
/// # Parser Errors
8783
///
8884
/// Several errors can arise during parsing and this enum accounts for them
@@ -375,16 +371,16 @@ impl<'a> Parser<'a> {
375371
b'?' => Element::Binstr(self.parse_next_binstr()?),
376372
b'+' => Element::Str(self.parse_next_string()?),
377373
b':' => Element::UnsignedInt(self.parse_next_u64()?),
378-
b'&' => Element::Array(self.parse_next_array()?),
374+
b'&' => Element::Array(Array::Recursive(self.parse_next_array()?)),
379375
b'!' => Element::RespCode(self.parse_next_respcode()?),
380376
b'@' => {
381377
// hmmm, a typed array; let's check the tsymbol
382378
if let Some(array_type) = self.buffer.get(self.cursor) {
383379
// got tsymbol, let's skip it
384380
self.incr_cursor();
385381
match array_type {
386-
b'+' => Element::StrArray(self.parse_next_typed_array_str()?),
387-
b'?' => Element::BinArray(self.parse_next_typed_array_bin()?),
382+
b'+' => Element::Array(Array::Str(self.parse_next_typed_array_str()?)),
383+
b'?' => Element::Array(Array::Bin(self.parse_next_typed_array_bin()?)),
388384
_ => return Err(ParseError::UnknownDatatype),
389385
}
390386
} else {
@@ -393,7 +389,7 @@ impl<'a> Parser<'a> {
393389
return Err(ParseError::NotEnough);
394390
}
395391
}
396-
b'_' => Element::FlatArray(self.parse_next_flat_array()?),
392+
b'_' => Element::Array(Array::Flat(self.parse_next_flat_array()?)),
397393
_ => return Err(ParseError::UnknownDatatype),
398394
};
399395
Ok(ret)
@@ -537,11 +533,11 @@ fn test_typed_str_array() {
537533
assert_eq!(forward, typed_array_packet.len());
538534
assert_eq!(
539535
parsed,
540-
RawResponse::SimpleQuery(Element::StrArray(vec![
536+
RawResponse::SimpleQuery(Element::Array(Array::Str(vec![
541537
Some("the".to_owned()),
542538
Some("cat".to_owned()),
543539
Some("meowed".to_owned())
544-
]))
540+
])))
545541
);
546542
}
547543

@@ -552,11 +548,11 @@ fn test_typed_bin_array() {
552548
assert_eq!(forward, typed_array_packet.len());
553549
assert_eq!(
554550
parsed,
555-
RawResponse::SimpleQuery(Element::BinArray(vec![
551+
RawResponse::SimpleQuery(Element::Array(Array::Bin(vec![
556552
Some(Vec::from("the")),
557553
Some(Vec::from("cat")),
558554
Some(Vec::from("meowed"))
559-
]))
555+
])))
560556
);
561557
}
562558

@@ -567,11 +563,11 @@ fn test_typed_bin_array_null() {
567563
assert_eq!(forward, typed_array_packet.len());
568564
assert_eq!(
569565
parsed,
570-
RawResponse::SimpleQuery(Element::BinArray(vec![
566+
RawResponse::SimpleQuery(Element::Array(Array::Bin(vec![
571567
Some(Vec::from("the")),
572568
Some(Vec::from("cat")),
573569
None
574-
]))
570+
])))
575571
);
576572
}
577573

@@ -582,10 +578,10 @@ fn test_typed_str_array_null() {
582578
assert_eq!(forward, typed_array_packet.len());
583579
assert_eq!(
584580
parsed,
585-
RawResponse::SimpleQuery(Element::StrArray(vec![
581+
RawResponse::SimpleQuery(Element::Array(Array::Str(vec![
586582
Some("the".to_owned()),
587583
Some("cat".to_owned()),
588584
None
589-
]))
585+
])))
590586
);
591587
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ pub mod error {
549549
}
550550
);
551551
#[derive(Debug)]
552+
#[non_exhaustive]
552553
/// An error originating from the Skyhash protocol
553554
pub enum SkyhashError {
554555
/// The server sent an invalid response
@@ -560,6 +561,7 @@ pub mod error {
560561
}
561562

562563
#[derive(Debug)]
564+
#[non_exhaustive]
563565
/// A standard error type for the client driver
564566
pub enum Error {
565567
/// An I/O error occurred

src/types.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@
7676
//!
7777
//! ```
7878
79-
use crate::RespCode;
79+
use crate::Element;
8080
use crate::Query;
81+
use crate::RespCode;
8182

8283
/// Anything that implements this trait can be turned into a [`String`]. This trait is implemented
8384
/// for most primitive types by default using [`std`]'s [`ToString`] trait.
@@ -273,21 +274,40 @@ impl<T: IntoSkyhashBytes> GetIterator<T> for &Vec<T> {
273274
}
274275

275276
/// Array types
277+
#[derive(Debug, PartialEq)]
278+
#[non_exhaustive]
276279
pub enum Array {
280+
//// A binary array (typed array tsymbol `?`, `@` base tsymbol)
277281
Bin(Vec<Option<Vec<u8>>>),
282+
/// An unicode string array (typed array tsymbol `+`, `@` base tsymbol)
278283
Str(Vec<Option<String>>),
284+
/// A non-recursive 'flat' array (tsymbol `_`)
285+
Flat(Vec<FlatElement>),
286+
/// A recursive array (tsymbol `&`)
287+
Recursive(Vec<Element>),
279288
}
280289

281290
/// String types
291+
#[derive(Debug, PartialEq)]
292+
#[non_exhaustive]
282293
pub enum Str {
294+
/// An unicode string
283295
Unicode(String),
296+
/// A binary string (blob)
284297
Binary(Vec<u8>),
285298
}
286299

287300
#[derive(Debug, PartialEq)]
301+
#[non_exhaustive]
302+
/// A _flat_ element. This corresponds to the types that can be present
303+
/// in a flat array as defined by the Skyhash protocol
288304
pub enum FlatElement {
305+
/// An unicode string
289306
String(String),
307+
/// A binary string (blob)
290308
Binstr(Vec<u8>),
309+
/// A response code
291310
RespCode(RespCode),
311+
/// An unsigned integer
292312
UnsignedInt(u64),
293313
}

0 commit comments

Comments
 (0)