Skip to content

Commit 74526bf

Browse files
committed
Add the binstr and str types
1 parent a06bf80 commit 74526bf

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/actions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ implement_actions!(
173173
Response::Item(Element::RespCode(RespCode::Okay)) => {}
174174
}
175175
/// Get the value of a key
176-
fn get(key: impl IntoSkyhashBytes + 's) -> Vec<u8> {
176+
fn get(key: impl IntoSkyhashBytes + 's) -> String {
177177
{ Query::from("get").arg(key)}
178-
Response::Item(Element::String(st)) => st
178+
Response::Item(Element::Str(st)) => st
179179
}
180180
/// Get the length of a key
181181
fn keylen(key: impl IntoSkyhashBytes + 's) -> usize {

src/deserializer.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ pub(super) struct Parser<'a> {
6565
pub enum Element {
6666
/// Arrays can be nested! Their `<tsymbol>` is `&`
6767
Array(Vec<Element>),
68-
/// A binary/unicode string value; `<tsymbol>` is `+`
69-
String(Vec<u8>),
68+
/// An unicode string value; `<tsymbol>` is `+`
69+
Str(String),
70+
/// A binary string (`?`)
71+
Binstr(Vec<u8>),
7072
/// An unsigned integer value; `<tsymbol>` is `:`
7173
UnsignedInt(u64),
7274
/// A non-recursive String array; tsymbol: `_`
@@ -279,20 +281,23 @@ impl<'a> Parser<'a> {
279281
Err(ParseError::NotEnough)
280282
}
281283
}
282-
/// The cursor should have passed the `+` tsymbol
283-
fn parse_next_string(&mut self) -> ParseResult<Vec<u8>> {
284-
let our_string_chunk = self.__get_next_element()?;
285-
let our_string = Vec::from(our_string_chunk);
284+
/// The cursor should have passed the `?` tsymbol
285+
fn parse_next_binstr(&mut self) -> ParseResult<Vec<u8>> {
286+
let our_string_chunk = self.__get_next_element()?.to_owned();
286287
if self.will_cursor_give_linefeed()? {
287-
// there is a lf after the end of the string; great!
288+
// there is a lf after the end of the binary string; great!
288289
// let's skip that now
289290
self.incr_cursor();
290291
// let's return our string
291-
Ok(our_string)
292+
Ok(our_string_chunk)
292293
} else {
293294
Err(ParseError::UnexpectedByte)
294295
}
295296
}
297+
/// The cursor should have passed the `+` tsymbol
298+
fn parse_next_string(&mut self) -> ParseResult<String> {
299+
Ok(String::from_utf8_lossy(&self.parse_next_binstr()?).to_string())
300+
}
296301
/// The cursor should have passed the `:` tsymbol
297302
fn parse_next_u64(&mut self) -> ParseResult<u64> {
298303
let our_u64_chunk = self.__get_next_element()?;
@@ -323,7 +328,8 @@ impl<'a> Parser<'a> {
323328
// but advance the cursor before doing that
324329
self.incr_cursor();
325330
let ret = match *tsymbol {
326-
b'+' => Element::String(self.parse_next_string()?),
331+
b'?' => Element::Binstr(self.parse_next_binstr()?),
332+
b'+' => Element::Str(self.parse_next_string()?),
327333
b':' => Element::UnsignedInt(self.parse_next_u64()?),
328334
b'&' => Element::Array(self.parse_next_array()?),
329335
b'!' => Element::RespCode(self.parse_next_respcode()?),
@@ -347,7 +353,7 @@ impl<'a> Parser<'a> {
347353
// good, there is a tsymbol; move the cursor ahead
348354
self.incr_cursor();
349355
let ret = match *tsymbol {
350-
b'+' => self.parse_next_string()?,
356+
b'+' => self.parse_next_binstr()?,
351357
_ => return Err(ParseError::UnknownDatatype),
352358
};
353359
array.push(ret);

0 commit comments

Comments
 (0)