Skip to content

Commit 330a8f5

Browse files
committed
resp: Add RList for decoding lists in responses
1 parent 7e50a47 commit 330a8f5

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/query.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,18 +395,18 @@ const LIST_SYM_OPEN: u8 = 0x09;
395395
const LIST_SYM_CLOSE: u8 = ']' as u8;
396396

397397
/// A list type representing a Skyhash list type, used in parameter lists
398-
pub struct List<'a, T: SQParam> {
398+
pub struct QList<'a, T: SQParam> {
399399
l: &'a [T],
400400
}
401401

402-
impl<'a, T: SQParam> List<'a, T> {
402+
impl<'a, T: SQParam> QList<'a, T> {
403403
/// create a new list
404404
pub fn new(l: &'a [T]) -> Self {
405405
Self { l }
406406
}
407407
}
408408

409-
impl<'a, T: SQParam> SQParam for List<'a, T> {
409+
impl<'a, T: SQParam> SQParam for QList<'a, T> {
410410
fn append_param(&self, q: &mut Vec<u8>) -> usize {
411411
q.push(LIST_SYM_OPEN);
412412
for param in self.l {
@@ -420,7 +420,7 @@ impl<'a, T: SQParam> SQParam for List<'a, T> {
420420
#[test]
421421
fn list_param() {
422422
let data = vec!["hello", "giant", "world"];
423-
let list = List::new(&data);
423+
let list = QList::new(&data);
424424
let q = query!(
425425
"insert into apps.social(?, ?, ?)",
426426
"username",

src/response.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,50 @@ impl<T: FromRow> Deref for Rows<T> {
405405
&self.0
406406
}
407407
}
408+
409+
/// A list received from a response
410+
pub struct RList<T: FromValue = Value>(Vec<T>);
411+
412+
impl<T: FromValue> RList<T> {
413+
/// Returns the values of the list
414+
pub fn into_values(self) -> Vec<T> {
415+
self.0
416+
}
417+
}
418+
419+
impl<T: FromValue> Deref for RList<T> {
420+
type Target = [T];
421+
fn deref(&self) -> &Self::Target {
422+
&self.0
423+
}
424+
}
425+
426+
impl<T: FromValue> FromValue for RList<T> {
427+
fn from_value(v: Value) -> ClientResult<Self> {
428+
match v {
429+
Value::List(l) => {
430+
let mut ret = Vec::new();
431+
for value in l {
432+
ret.push(T::from_value(value)?);
433+
}
434+
Ok(Self(ret))
435+
}
436+
_ => Err(Error::ParseError(ParseError::TypeMismatch)),
437+
}
438+
}
439+
}
440+
441+
#[test]
442+
fn resp_list_parse() {
443+
let response_list = Response::Row(Row::new(vec![
444+
Value::String("sayan".to_owned()),
445+
Value::List(vec![
446+
Value::String("c".to_owned()),
447+
Value::String("assembly".to_owned()),
448+
Value::String("rust".to_owned()),
449+
]),
450+
]));
451+
let (name, languages) = response_list.parse::<(String, RList<String>)>().unwrap();
452+
assert_eq!(name, "sayan");
453+
assert_eq!(languages.as_ref(), vec!["c", "assembly", "rust"]);
454+
}

0 commit comments

Comments
 (0)