use super::error::DriverError;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "snake_case")]
pub enum Response {
Ok {
#[serde(skip_serializing_if = "Option::is_none")]
data: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
count: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
tx_id: Option<String>,
},
Error {
error: DriverError,
},
Pong {
timestamp: i64,
},
Batch {
responses: Vec<Response>,
},
}
impl Response {
pub fn ok(data: Value) -> Self {
Response::Ok {
data: Some(data),
count: None,
tx_id: None,
}
}
pub fn ok_count(count: usize) -> Self {
Response::Ok {
data: None,
count: Some(count),
tx_id: None,
}
}
pub fn ok_empty() -> Self {
Response::Ok {
data: None,
count: None,
tx_id: None,
}
}
pub fn ok_tx(tx_id: String) -> Self {
Response::Ok {
data: None,
count: None,
tx_id: Some(tx_id),
}
}
pub fn error(err: DriverError) -> Self {
Response::Error { error: err }
}
pub fn pong() -> Self {
Response::Pong {
timestamp: chrono::Utc::now().timestamp_millis(),
}
}
}