Add functionality to extract a single query response
This commit is contained in:
parent
ea67af5a29
commit
113d02cbfc
3 changed files with 44 additions and 0 deletions
|
@ -11,6 +11,12 @@ pub enum Status {
|
||||||
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct Responses(pub Vec<Response>);
|
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)]
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
@ -22,3 +28,27 @@ pub struct Response {
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub result: Option<Value>,
|
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()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -16,4 +16,5 @@ pub mod object;
|
||||||
pub mod patch;
|
pub mod patch;
|
||||||
pub mod replace;
|
pub mod replace;
|
||||||
pub mod set;
|
pub mod set;
|
||||||
|
pub mod single;
|
||||||
pub mod value;
|
pub mod value;
|
||||||
|
|
13
src/sql/value/single.rs
Normal file
13
src/sql/value/single.rs
Normal 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue