Add functionality to extract a single query response

This commit is contained in:
Tobie Morgan Hitchcock 2022-02-14 23:21:12 +00:00
parent ea67af5a29
commit 113d02cbfc
3 changed files with 44 additions and 0 deletions

View file

@ -11,6 +11,12 @@ pub enum Status {
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct Responses(pub Vec<Response>);
impl Responses {
pub fn first(mut self) -> Response {
self.0.remove(0)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Response {
#[serde(skip_serializing_if = "Option::is_none")]
@ -22,3 +28,27 @@ pub struct Response {
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<Value>,
}
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()),
}
}
}

View file

@ -16,4 +16,5 @@ pub mod object;
pub mod patch;
pub mod replace;
pub mod set;
pub mod single;
pub mod value;

13
src/sql/value/single.rs Normal file
View file

@ -0,0 +1,13 @@
use crate::sql::value::Value;
impl Value {
pub fn single(&self) -> &Self {
match self {
Value::Array(v) => match v.value.first() {
None => &Value::None,
Some(v) => v,
},
v => v,
}
}
}