2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::value::Value;
|
2021-03-29 15:43:37 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2022-01-27 08:15:20 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "UPPERCASE")]
|
|
|
|
pub enum Status {
|
|
|
|
Ok,
|
|
|
|
Err,
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Responses(pub Vec<Response>);
|
|
|
|
|
2022-02-14 23:21:12 +00:00
|
|
|
impl Responses {
|
|
|
|
pub fn first(mut self) -> Response {
|
|
|
|
self.0.remove(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Response {
|
2022-01-27 08:21:04 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub sql: Option<String>,
|
2021-03-29 15:43:37 +00:00
|
|
|
pub time: String,
|
2022-01-27 08:15:20 +00:00
|
|
|
pub status: Status,
|
2021-03-29 15:43:37 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2022-01-13 17:36:41 +00:00
|
|
|
pub detail: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub result: Option<Value>,
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
2022-02-14 23:21:12 +00:00
|
|
|
|
|
|
|
impl Response {
|
|
|
|
// Check if response succeeded
|
|
|
|
pub fn is_ok(&self) -> bool {
|
|
|
|
match self.status {
|
|
|
|
Status::Ok => true,
|
|
|
|
Status::Err => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if response failed
|
|
|
|
pub fn is_err(&self) -> bool {
|
|
|
|
match self.status {
|
|
|
|
Status::Ok => false,
|
|
|
|
Status::Err => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Retrieve the response as a result
|
|
|
|
pub fn output(self) -> Result<Value, String> {
|
|
|
|
match self.status {
|
|
|
|
Status::Ok => Ok(self.result.unwrap()),
|
|
|
|
Status::Err => Err(self.detail.unwrap()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|