pub enum Value {
Nil,
Integer(i64),
Error(String),
String(String),
Array(Vec<Value>),
}
Expand description
A wrapper type for a RESP value.
This enum implements the TryFrom
trait (TryFrom<&str>
), to provide
on-the-fly parsing and validation of RESP strings.
§Examples
use io_resp::{
Node::{self, NIL, SIZE, ARRAY, ERROR, INTEGER, UNKNOWN, SIMPLE_STRING, BULK_STRING},
Value::{self, Nil, Error, Array, String, Integer},
Error as VError,
ValueResult,
};
assert_eq!( // Empty RESP
"".try_into() as ValueResult,
Err(VError::Unexpected {node: UNKNOWN, index: 0}));
assert_eq!( // Unterminated number: missing "\r\n"
":0".try_into() as ValueResult,
Err(VError::Unexpected {node: INTEGER, index: 2}));
assert_eq!( // Not enough elements in the array
"*2\r\n$-1\r\n".try_into() as ValueResult,
Err(VError::Size {node: ARRAY, index: 9}));
assert_eq!( // Longer bulk string: got more that 2-bytes
"$2\r\nHello\r\n".try_into() as ValueResult,
Err(VError::Size {node: BULK_STRING, index: 6}));
assert_eq!( // Sorter bulk string: shorter by 1-byte (capital A acute is 2-bytes)
"$3\r\nÂ\r\n".try_into() as ValueResult,
Err(VError::Size {node: BULK_STRING, index: 7}));
// JSON: null
assert_eq!(
Value::try_from("$-1\r\n"),
Ok(Nil)
);
// JSON: 10
assert_eq!(
Value::try_from(":10\r\n"),
Ok(Integer(10))
);
// JSON: "Nina Simone"
assert_eq!(
Value::try_from("+Nina Simone\r\n"),
Ok(String("Nina Simone".into()))
);
// JSON: "Lorem ipsum...\r\nDolor sit amet..."
assert_eq!(
Value::try_from("$33\r\nLorem ipsum...\r\nDolor sit amet...\r\n"),
Ok(String("Lorem ipsum...\r\nDolor sit amet...".into()))
);
// JavaScript: [null, 447, new Error("Oh oh!"), "Hourly", "Si vis pacem,\r\npara bellum"]
assert_eq!(
Value::try_from("*5\r\n$-1\r\n:447\r\n-Oh oh!\r\n+Hourly\r\n$26\r\nSi vis pacem,\r\npara bellum\r\n"),
Ok(Array(vec![
Nil,
Integer(447),
Error("Oh oh!".into()),
String("Hourly".into()),
String("Si vis pacem,\r\npara bellum".into())
]))
);
// NOTE: Even recursive arrays - we leave that for you to try out.
Variants§
Nil
Denote the absence of value.
Integer(i64)
Denote and integer value, wrapped as singleton tuple.
Error(String)
Denote an error, wrapped as descriptive message string.
String(String)
Denote a string value, wrapped as singleton tuple.
Array(Vec<Value>)
Denote a non-nil list of values, wrapped as singleton vector of Value.
Trait Implementations§
impl StructuralPartialEq for Value
Auto Trait Implementations§
impl Freeze for Value
impl RefUnwindSafe for Value
impl Send for Value
impl Sync for Value
impl Unpin for Value
impl UnwindSafe for Value
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more