wetstring/src/request_response.rs

43 lines
1.3 KiB
Rust

use std::net::SocketAddr;
/// A trait for an object that can store the state of a partially parsed request.
/// The incoming byte stream can be parsed incrementally, and the request can be converted
/// to an error response if the incoming bytes are invalid.
pub trait RequestParser {
type Req: Request;
type Res: Response;
fn new(client_address: SocketAddr) -> Self;
fn push_bytes(&mut self, bytes: &[u8]);
fn try_parse(&mut self) -> RequestParseResult<Self::Req, Self::Res>;
}
/// A trait for objects that represent a valid network request.
pub trait Request {}
/// A trait for objects that represent the response to a network request.
pub trait Response {
fn to_bytes(self) -> Vec<u8>;
}
pub enum RequestParseResult<Req: Request, Res: Response> {
/// The request is waiting for the client to send more data before the
/// request can either be discarded as erroneous or parsed.
Incomplete,
/// Sufficient data has been received from the client, and the request
/// has been successfully parsed.
Complete(Req),
/// An error has been encountered in the received data, and a response
/// has been generated to be returned to the client.
Invalid(Res),
}
pub trait RequestProcessor {
type Req: Request;
type Res: Response;
fn process_request(&self, request: &Self::Req) -> Self::Res;
}