Value

Enum Value 

Source
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§

Source§

impl Debug for Value

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Value

Source§

fn eq(&self, other: &Value) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<&str> for Value

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(source: &str) -> ValueResult<'_>

Performs the conversion.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.