Crate sawp_resp

source ·
Expand description

A RESP protocol parser. Given bytes and a sawp::parser::Direction, it will attempt to parse the bytes and return a Message. The parser will inform the caller about what went wrong if no message is returned (see sawp::parser::Parse for details on possible return types).

The following protocol references were used to create this module:

RESP Protocol Specification

§Example

use sawp::parser::{Direction, Parse};
use sawp::error::Error;
use sawp::error::ErrorKind;
use sawp_resp::{Resp, Message};

fn parse_bytes(input: &[u8]) -> std::result::Result<&[u8], Error> {
    let resp = Resp {};
    let mut bytes = input;
    while bytes.len() > 0 {
        // If we know that this is a request or response, change the Direction
        // for a more accurate parsing
        match resp.parse(bytes, Direction::Unknown) {
            // The parser succeeded and returned the remaining bytes and the parsed RESP message
            Ok((rest, Some(message))) => {
                println!("Resp message: {:?}", message);
                bytes = rest;
            }
            // The parser recognized that this might be RESP and made some progress,
            // but more bytes are needed
            Ok((rest, None)) => return Ok(rest),
            // The parser was unable to determine whether this was RESP or not and more
            // bytes are needed
            Err(Error { kind: ErrorKind::Incomplete(_) }) => return Ok(bytes),
            // The parser determined that this was not RESP
            Err(e) => return Err(e)
        }
    }

    Ok(bytes)
}

Structs§

Enums§

Constants§