2022-05-01 23:35:01 +00:00
|
|
|
use crate::err::Error;
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::value::Value;
|
2022-05-01 23:35:01 +00:00
|
|
|
use serde::ser::SerializeStruct;
|
|
|
|
use serde::Serialize;
|
|
|
|
use std::time::Duration;
|
2021-03-29 15:43:37 +00:00
|
|
|
|
2022-05-01 23:35:01 +00:00
|
|
|
#[derive(Debug)]
|
2021-03-29 15:43:37 +00:00
|
|
|
pub struct Response {
|
2022-01-27 08:21:04 +00:00
|
|
|
pub sql: Option<String>,
|
2022-05-01 23:35:01 +00:00
|
|
|
pub time: Duration,
|
|
|
|
pub result: Result<Value, Error>,
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
2022-02-14 23:21:12 +00:00
|
|
|
|
|
|
|
impl Response {
|
2022-05-01 23:35:01 +00:00
|
|
|
// Return the transaction speed
|
|
|
|
pub fn speed(&self) -> String {
|
|
|
|
format!("{:?}", self.time)
|
2022-02-14 23:21:12 +00:00
|
|
|
}
|
2022-05-01 23:35:01 +00:00
|
|
|
// Retrieve the response as a result
|
|
|
|
pub fn output(&self) -> Result<&Value, &Error> {
|
|
|
|
match &self.result {
|
|
|
|
Ok(v) => Ok(v),
|
|
|
|
Err(e) => Err(e),
|
2022-02-14 23:21:12 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-01 23:35:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Response {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
|
|
|
match &self.result {
|
|
|
|
Ok(v) => match v {
|
|
|
|
Value::None => match &self.sql {
|
|
|
|
Some(s) => {
|
|
|
|
let mut val = serializer.serialize_struct("Response", 3)?;
|
|
|
|
val.serialize_field("sql", s.as_str())?;
|
|
|
|
val.serialize_field("time", self.speed().as_str())?;
|
|
|
|
val.serialize_field("status", "OK")?;
|
|
|
|
val.end()
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let mut val = serializer.serialize_struct("Response", 2)?;
|
|
|
|
val.serialize_field("time", self.speed().as_str())?;
|
|
|
|
val.serialize_field("status", "OK")?;
|
|
|
|
val.end()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
v => match &self.sql {
|
|
|
|
Some(s) => {
|
|
|
|
let mut val = serializer.serialize_struct("Response", 4)?;
|
|
|
|
val.serialize_field("sql", s.as_str())?;
|
|
|
|
val.serialize_field("time", self.speed().as_str())?;
|
|
|
|
val.serialize_field("status", "OK")?;
|
|
|
|
val.serialize_field("result", v)?;
|
|
|
|
val.end()
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let mut val = serializer.serialize_struct("Response", 3)?;
|
|
|
|
val.serialize_field("time", self.speed().as_str())?;
|
|
|
|
val.serialize_field("status", "OK")?;
|
|
|
|
val.serialize_field("result", v)?;
|
|
|
|
val.end()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Err(e) => match &self.sql {
|
|
|
|
Some(s) => {
|
|
|
|
let mut val = serializer.serialize_struct("Response", 4)?;
|
|
|
|
val.serialize_field("sql", s.as_str())?;
|
|
|
|
val.serialize_field("time", self.speed().as_str())?;
|
|
|
|
val.serialize_field("status", "ERR")?;
|
|
|
|
val.serialize_field("detail", e)?;
|
|
|
|
val.end()
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let mut val = serializer.serialize_struct("Response", 3)?;
|
|
|
|
val.serialize_field("time", self.speed().as_str())?;
|
|
|
|
val.serialize_field("status", "ERR")?;
|
|
|
|
val.serialize_field("detail", e)?;
|
|
|
|
val.end()
|
|
|
|
}
|
|
|
|
},
|
2022-02-14 23:21:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|