Skip to content

Commit

Permalink
rename parse_one_command to parse_one_request
Browse files Browse the repository at this point in the history
documenting comments
  • Loading branch information
Keksoj committed Jul 3, 2023
1 parent 361850a commit b26d34c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions bin/src/command/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use sozu_command_lib::{
buffer::fixed::Buffer,
config::Config,
logging,
parser::parse_several_commands,
parser::parse_several_requests,
proto::command::{
request::RequestType, response_content::ContentType, AggregatedMetrics, AvailableMetrics,
CertificatesWithFingerprints, ClusterHashes, ClusterInformations, FrontendFilters,
Expand Down Expand Up @@ -213,7 +213,7 @@ impl CommandServer {
}

let mut offset = 0usize;
match parse_several_commands::<WorkerRequest>(buffer.data()) {
match parse_several_requests::<WorkerRequest>(buffer.data()) {
Ok((i, requests)) => {
if !i.is_empty() {
debug!("could not parse {} bytes", i.len());
Expand Down
2 changes: 1 addition & 1 deletion command/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! ```
//!
//! `config.toml` is parsed to `FileConfig`, a structure that itself contains a lot of substructures
//! whose names end start with `File-`, like `FileHttpFrontendConfig` for instance.
//! whose names start with `File-` and end with `-Config`, like `FileHttpFrontendConfig` for instance.
//!
//! The instance of `FileConfig` is then passed to a `ConfigBuilder` that populates a final `Config`
//! with listeners and clusters.
Expand Down
3 changes: 3 additions & 0 deletions command/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod certificate;
pub mod channel;
/// parse TOML config and generate requests from it
pub mod config;
/// parse Requests
pub mod parser;
/// Contains Rust types generated by [`prost`](https://docs.rs/prost/latest/prost/)
/// using the protobuf definition in `command.proto`.
Expand Down Expand Up @@ -58,6 +59,7 @@ pub mod parser;
///
/// A bit cumbersome, but it is the only way to benefit from protobuf in Rust.
pub mod proto;
/// File descriptor readiness
pub mod ready;
/// Helper functions around types received by Sōzu
pub mod request;
Expand All @@ -67,4 +69,5 @@ pub mod response;
pub mod scm_socket;
/// A representation of Sōzu's state
pub mod state;
/// A writer used for logging
pub mod writer;
12 changes: 7 additions & 5 deletions command/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ impl nom::error::ParseError<&[u8]> for CustomError {
}
}

pub fn parse_one_command<'a, T>(input: &'a [u8]) -> IResult<&[u8], T, CustomError>
/// Parse a single Request or WorkerRequest
pub fn parse_one_request<'a, T>(input: &'a [u8]) -> IResult<&[u8], T, CustomError>
where
T: serde::de::Deserialize<'a>,
{
Expand All @@ -63,11 +64,12 @@ where
Ok((next_input, command))
}

pub fn parse_several_commands<'a, T>(input: &'a [u8]) -> IResult<&[u8], Vec<T>, CustomError>
/// Parse a Requests or WorkerRequests using nom
pub fn parse_several_requests<'a, T>(input: &'a [u8]) -> IResult<&[u8], Vec<T>, CustomError>
where
T: serde::de::Deserialize<'a>,
{
many0(parse_one_command)(input)
many0(parse_one_request)(input)
}

#[cfg(test)]
Expand Down Expand Up @@ -97,7 +99,7 @@ mod test {
let empty_vec: Vec<u8> = vec![];

assert_eq!(
parse_one_command(bytes).unwrap(),
parse_one_request(bytes).unwrap(),
(&empty_vec[..], worker_request)
)
}
Expand Down Expand Up @@ -128,7 +130,7 @@ mod test {

let bytes_to_parse = &serialized_requests.as_bytes();

let parsed_requests = parse_several_commands(bytes_to_parse).unwrap();
let parsed_requests = parse_several_requests(bytes_to_parse).unwrap();

println!("parsed commands: {parsed_requests:?}");

Expand Down
2 changes: 2 additions & 0 deletions command/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ impl From<TcpFrontend> for RequestTcpFrontend {
}
}

/// A backend, as used *within* Sōzu
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Backend {
pub cluster_id: String,
Expand Down Expand Up @@ -223,6 +224,7 @@ struct StatePath {

pub type MessageId = String;

/// A response as sent by a worker
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WorkerResponse {
pub id: MessageId,
Expand Down
1 change: 1 addition & 0 deletions command/src/writer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::io::{self, Error, ErrorKind, Write};

/// A multiline writer used for logging
pub struct MultiLineWriter<W: Write> {
inner: Option<W>,
buf: Vec<u8>,
Expand Down

0 comments on commit b26d34c

Please sign in to comment.