Skip to content

Commit

Permalink
Merge pull request #233 from pleshevskiy/master
Browse files Browse the repository at this point in the history
Fix clippy warnings
  • Loading branch information
valeriansaliou authored Nov 9, 2021
2 parents f4aa5f5 + 4ae1d6c commit e78599a
Show file tree
Hide file tree
Showing 93 changed files with 461 additions and 493 deletions.
114 changes: 53 additions & 61 deletions src/channel/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use hashbrown::HashMap;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use std::fmt;
use std::path::Path;
use std::str::{self, SplitWhitespace};
use std::vec::Vec;
Expand All @@ -33,6 +34,26 @@ pub enum ChannelCommandError {
InvalidMetaValue((String, String)),
}

impl fmt::Display for ChannelCommandError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match self {
ChannelCommandError::UnknownCommand => write!(f, "unknown_command"),
ChannelCommandError::NotFound => write!(f, "not_found"),
ChannelCommandError::QueryError => write!(f, "query_error"),
ChannelCommandError::InternalError => write!(f, "internal_error"),
ChannelCommandError::ShuttingDown => write!(f, "shutting_down"),
ChannelCommandError::PolicyReject(reason) => write!(f, "policy_reject({})", reason),
ChannelCommandError::InvalidFormat(format) => write!(f, "invalid_format({})", format),
ChannelCommandError::InvalidMetaKey(ref data) => {
write!(f, "invalid_meta_key({}[{}])", data.0, data.1)
}
ChannelCommandError::InvalidMetaValue(ref data) => {
write!(f, "invalid_meta_value({}[{}])", data.0, data.1)
}
}
}
}

#[derive(PartialEq)]
pub enum ChannelCommandResponse {
Void,
Expand Down Expand Up @@ -62,8 +83,8 @@ const TEXT_PART_ESCAPE: char = '\\';
const META_PART_GROUP_OPEN: char = '(';
const META_PART_GROUP_CLOSE: char = ')';

static BACKUP_KV_PATH: &'static str = "kv";
static BACKUP_FST_PATH: &'static str = "fst";
static BACKUP_KV_PATH: &str = "kv";
static BACKUP_FST_PATH: &str = "fst";

lazy_static! {
pub static ref COMMANDS_MODE_SEARCH: Vec<&'static str> =
Expand Down Expand Up @@ -91,26 +112,6 @@ lazy_static! {
.collect();
}

impl ChannelCommandError {
pub fn to_string(&self) -> String {
match *self {
ChannelCommandError::UnknownCommand => String::from("unknown_command"),
ChannelCommandError::NotFound => String::from("not_found"),
ChannelCommandError::QueryError => String::from("query_error"),
ChannelCommandError::InternalError => String::from("internal_error"),
ChannelCommandError::ShuttingDown => String::from("shutting_down"),
ChannelCommandError::PolicyReject(reason) => format!("policy_reject({})", reason),
ChannelCommandError::InvalidFormat(format) => format!("invalid_format({})", format),
ChannelCommandError::InvalidMetaKey(ref data) => {
format!("invalid_meta_key({}[{}])", data.0, data.1)
}
ChannelCommandError::InvalidMetaValue(ref data) => {
format!("invalid_meta_value({}[{}])", data.0, data.1)
}
}
}
}

impl ChannelCommandResponse {
pub fn to_args(&self) -> ChannelCommandResponseArgs {
// Convert internal response to channel response arguments; this either gives 'RESPONSE' \
Expand Down Expand Up @@ -181,9 +182,9 @@ impl ChannelCommandBase {
// Parse text parts and nest them together
let mut text_raw = String::new();

while let Some(text_part) = parts.next() {
for text_part in parts {
if !text_raw.is_empty() {
text_raw.push_str(" ");
text_raw.push(' ');
}

text_raw.push_str(text_part);
Expand Down Expand Up @@ -315,14 +316,14 @@ impl ChannelCommandBase {

pub fn commit_ok_operation(query_builder: QueryBuilderResult) -> ChannelResult {
query_builder
.and_then(|query| StoreOperationDispatch::dispatch(query))
.and_then(|_| Ok(vec![ChannelCommandResponse::Ok]))
.and_then(StoreOperationDispatch::dispatch)
.map(|_| vec![ChannelCommandResponse::Ok])
.or(Err(ChannelCommandError::QueryError))
}

pub fn commit_result_operation(query_builder: QueryBuilderResult) -> ChannelResult {
query_builder
.and_then(|query| StoreOperationDispatch::dispatch(query))
.and_then(StoreOperationDispatch::dispatch)
.or(Err(ChannelCommandError::QueryError))
.and_then(|result| {
if let Some(result_inner) = result {
Expand All @@ -333,10 +334,10 @@ impl ChannelCommandBase {
})
}

pub fn commit_pending_operation<'a>(
pub fn commit_pending_operation(
query_type: &'static str,
query_id: &str,
query_builder: QueryBuilderResult<'a>,
query_builder: QueryBuilderResult,
) -> ChannelResult {
// Idea: this could be made asynchronous in the future, if there are some latency issues \
// on large Sonic deployments. The idea would be to have a number of worker threads for \
Expand All @@ -348,16 +349,16 @@ impl ChannelCommandBase {
// consumer via a worker thread pool.

query_builder
.and_then(|query| StoreOperationDispatch::dispatch(query))
.and_then(|results| {
Ok(vec![
.and_then(StoreOperationDispatch::dispatch)
.map(|results| {
vec![
ChannelCommandResponse::Pending(query_id.to_string()),
ChannelCommandResponse::Event(
query_type,
query_id.to_string(),
results.unwrap_or(String::new()),
results.unwrap_or_default(),
),
])
]
})
.or(Err(ChannelCommandError::QueryError))
}
Expand All @@ -371,6 +372,12 @@ impl ChannelCommandBase {
}
}

type QueryMetaData = (
Option<QuerySearchLimit>,
Option<QuerySearchOffset>,
Option<QueryGenericLang>,
);

impl ChannelCommandSearch {
pub fn dispatch_query(mut parts: SplitWhitespace) -> ChannelResult {
match (
Expand Down Expand Up @@ -512,14 +519,7 @@ impl ChannelCommandSearch {

fn handle_query_meta(
meta_result: MetaPartsResult,
) -> Result<
(
Option<QuerySearchLimit>,
Option<QuerySearchOffset>,
Option<QueryGenericLang>,
),
ChannelCommandError,
> {
) -> Result<QueryMetaData, ChannelCommandError> {
match meta_result {
Ok((meta_key, meta_value)) => {
debug!("handle query meta: {} = {}", meta_key, meta_value);
Expand All @@ -531,8 +531,7 @@ impl ChannelCommandSearch {
Ok((Some(query_limit_parsed), None, None))
} else {
Err(ChannelCommandBase::make_error_invalid_meta_value(
&meta_key,
&meta_value,
meta_key, meta_value,
))
}
}
Expand All @@ -542,8 +541,7 @@ impl ChannelCommandSearch {
Ok((None, Some(query_offset_parsed), None))
} else {
Err(ChannelCommandBase::make_error_invalid_meta_value(
&meta_key,
&meta_value,
meta_key, meta_value,
))
}
}
Expand All @@ -553,19 +551,17 @@ impl ChannelCommandSearch {
Ok((None, None, Some(query_lang_parsed)))
} else {
Err(ChannelCommandBase::make_error_invalid_meta_value(
&meta_key,
&meta_value,
meta_key, meta_value,
))
}
}
_ => Err(ChannelCommandBase::make_error_invalid_meta_key(
&meta_key,
&meta_value,
meta_key, meta_value,
)),
}
}
Err(err) => Err(ChannelCommandBase::make_error_invalid_meta_key(
&err.0, &err.1,
err.0, err.1,
)),
}
}
Expand All @@ -584,19 +580,17 @@ impl ChannelCommandSearch {
Ok(Some(suggest_limit_parsed))
} else {
Err(ChannelCommandBase::make_error_invalid_meta_value(
&meta_key,
&meta_value,
meta_key, meta_value,
))
}
}
_ => Err(ChannelCommandBase::make_error_invalid_meta_key(
&meta_key,
&meta_value,
meta_key, meta_value,
)),
}
}
Err(err) => Err(ChannelCommandBase::make_error_invalid_meta_key(
&err.0, &err.1,
err.0, err.1,
)),
}
}
Expand Down Expand Up @@ -767,19 +761,17 @@ impl ChannelCommandIngest {
Ok(Some(query_lang_parsed))
} else {
Err(ChannelCommandBase::make_error_invalid_meta_value(
&meta_key,
&meta_value,
meta_key, meta_value,
))
}
}
_ => Err(ChannelCommandBase::make_error_invalid_meta_key(
&meta_key,
&meta_value,
meta_key, meta_value,
)),
}
}
Err(err) => Err(ChannelCommandBase::make_error_invalid_meta_key(
&err.0, &err.1,
err.0, err.1,
)),
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/channel/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const BUFFER_SIZE: usize = 20000;
const MAX_LINE_SIZE: usize = BUFFER_SIZE + LINE_END_GAP + 1;
const TCP_TIMEOUT_NON_ESTABLISHED: u64 = 10;
const PROTOCOL_REVISION: u8 = 1;
const BUFFER_LINE_SEPARATOR: u8 = '\n' as u8;
const BUFFER_LINE_SEPARATOR: u8 = b'\n';

lazy_static! {
static ref CONNECTED_BANNER: String = format!(
Expand Down Expand Up @@ -192,6 +192,7 @@ impl ChannelHandle {
}

fn ensure_start(mut stream: &TcpStream) -> Result<ChannelMode, ChannelHandleError> {
#[allow(clippy::never_loop)]
loop {
let mut read = [0; MAX_LINE_SIZE];

Expand Down
4 changes: 2 additions & 2 deletions src/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ impl ChannelMessage {
// Process response for issued command
let response_args_groups: Vec<ChannelCommandResponseArgs>;

if *CHANNEL_AVAILABLE.read().unwrap() != true {
if !(*CHANNEL_AVAILABLE.read().unwrap()) {
// Server going down, reject command
response_args_groups =
vec![ChannelCommandResponse::Err(ChannelCommandError::ShuttingDown).to_args()];
} else {
// Handle response arguments to issued command
response_args_groups = match M::handle(&message) {
response_args_groups = match M::handle(message) {
Ok(resp_groups) => resp_groups
.iter()
.map(|resp| match resp {
Expand Down
Loading

0 comments on commit e78599a

Please sign in to comment.