Use blanket implementations for response traits

next
Sayan Nandan 4 years ago
parent 7db7ebbe34
commit 423c9b37aa
No known key found for this signature in database
GPG Key ID: C31EFD7DDA12AEE0

@ -38,8 +38,20 @@ pub enum ClientResult {
impl fmt::Display for ClientResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use ClientResult::*;
use RespCodes::*;
match self {
RespCode(r, _) => r.fmt(f),
RespCode(r, _) => match r {
Okay => Ok(()),
NotFound => writeln!(f, "ERROR: Couldn't find the key"),
OverwriteError => writeln!(f, "ERROR: Existing values cannot be overwritten"),
InvalidMetaframe => writeln!(f, "ERROR: Invalid metaframe"),
ArgumentError => writeln!(f, "ERROR: The command is not in the correct format"),
ServerError => writeln!(f, "ERROR: The server had an internal error"),
OtherError(e) => match e {
None => writeln!(f, "ERROR: Some unknown error occurred"),
Some(e) => writeln!(f, "ERROR: {}", e),
},
},
InvalidResponse(_) => write!(f, "ERROR: The server sent an invalid response"),
Response(_, _) => unimplemented!(),
Empty(_) => write!(f, ""),

@ -77,9 +77,14 @@ impl Connection {
}
return;
}
ClientResult::RespCode(r, f) => {
self.buffer.advance(f);
eprint!("{}", r);
x @ ClientResult::RespCode(_, _) => {
match x {
ClientResult::RespCode(_, f) => {
self.buffer.advance(f);
}
_ => unimplemented!(),
}
eprint!("{}", x);
return;
}
ClientResult::InvalidResponse(_) => {

@ -76,26 +76,6 @@ pub enum RespCodes {
OtherError(Option<String>),
}
impl fmt::Display for RespCodes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use RespCodes::*;
match self {
Okay => Ok(()),
NotFound => writeln!(f, "ERROR: Couldn't find the key"),
OverwriteError => writeln!(f, "ERROR: Existing values cannot be overwritten"),
InvalidMetaframe => writeln!(f, "ERROR: Invalid metaframe"),
ArgumentError => writeln!(f, "ERROR: The command is not in the correct format"),
ServerError => writeln!(f, "ERROR: The server had an internal error"),
OtherError(e) => match e {
None => writeln!(f, "ERROR: Some unknown error occurred"),
Some(e) => writeln!(f, "ERROR: {}", e),
},
}
}
}
impl Error for RespCodes {}
impl From<RespCodes> for u8 {
fn from(rcode: RespCodes) -> u8 {
use RespCodes::*;

@ -140,8 +140,8 @@ mod builders {
fn into_pre_resp(self) -> PRTuple;
}
/// Trait implementors should return a data group in the response packet. It have
/// a structure, which has the following general format:
/// Trait implementors should return a data group in the response packet.
/// It should have a structure, which has the following general format:
/// ```text
/// &<n>\n
/// <symbol>item[0]\n
@ -165,16 +165,12 @@ mod builders {
fn into_response(self) -> Vec<u8>;
}
impl PreResp for String {
impl<T> PreResp for T
where
T: ToString,
{
fn into_pre_resp(self) -> PRTuple {
let df_bytes = [&[b'+'], self.as_bytes(), &[b'\n']].concat();
(df_bytes.len() - 1, df_bytes)
}
}
impl PreResp for &str {
fn into_pre_resp(self) -> PRTuple {
let df_bytes = [&[b'+'], self.as_bytes(), &[b'\n']].concat();
let df_bytes = [&[b'+'], self.to_string().as_bytes(), &[b'\n']].concat();
(df_bytes.len() - 1, df_bytes)
}
}
@ -235,23 +231,17 @@ mod builders {
(metalayout, dataframe)
}
}
// For responses which just have one String
impl IntoRespGroup for String {
fn into_resp_group(self) -> RespTuple {
let metalayout =
[&[b'#', b'2', b'#'], (self.len() + 1).to_string().as_bytes()].concat();
let dataframe =
[&[b'&', b'1', b'\n'], &[b'+'][..], self.as_bytes(), &[b'\n']].concat();
(metalayout, dataframe)
}
}
// For responses which just have one str
impl IntoRespGroup for &str {
// For responses which just have one value
impl<T> IntoRespGroup for T
where
T: ToString,
{
fn into_resp_group(self) -> RespTuple {
let st = self.to_string();
let metalayout =
[&[b'#', b'2', b'#'], (self.len() + 1).to_string().as_bytes()].concat();
[&[b'#', b'2', b'#'], (st.len() + 1).to_string().as_bytes()].concat();
let dataframe =
[&[b'&', b'1', b'\n'], &[b'+'][..], self.as_bytes(), &[b'\n']].concat();
[&[b'&', b'1', b'\n'], &[b'+'][..], st.as_bytes(), &[b'\n']].concat();
(metalayout, dataframe)
}
}
@ -354,26 +344,13 @@ mod builders {
}
}
// For an entire response which only comprises of a string
impl IntoResponse for String {
fn into_response(self) -> Vec<u8> {
let (metalayout, dataframe) = self.into_resp_group();
let metaline = [
&[b'*', b'!'],
dataframe.len().to_string().as_bytes(),
&[b'!'],
metalayout.len().to_string().as_bytes(),
&[b'\n'],
]
.concat();
[metaline, metalayout, dataframe].concat()
}
}
// For an entire response which only comprises of a string
impl IntoResponse for &str {
// For an entire response which only comprises of a single value
impl<T> IntoResponse for T
where
T: ToString,
{
fn into_response(self) -> Vec<u8> {
let (metalayout, dataframe) = self.into_resp_group();
let (metalayout, dataframe) = self.to_string().into_resp_group();
let metaline = [
&[b'*', b'!'],
dataframe.len().to_string().as_bytes(),

Loading…
Cancel
Save