From 113d02cbfc46cdacf18b95558114d763bb300f26 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 14 Feb 2022 23:21:12 +0000 Subject: [PATCH] Add functionality to extract a single query response --- src/dbs/response.rs | 30 ++++++++++++++++++++++++++++++ src/sql/value/mod.rs | 1 + src/sql/value/single.rs | 13 +++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 src/sql/value/single.rs diff --git a/src/dbs/response.rs b/src/dbs/response.rs index aa6f014e..20a49e70 100644 --- a/src/dbs/response.rs +++ b/src/dbs/response.rs @@ -11,6 +11,12 @@ pub enum Status { #[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] pub struct Responses(pub Vec); +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, } + +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 { + match self.status { + Status::Ok => Ok(self.result.unwrap()), + Status::Err => Err(self.detail.unwrap()), + } + } +} diff --git a/src/sql/value/mod.rs b/src/sql/value/mod.rs index 5917d96f..115d0961 100644 --- a/src/sql/value/mod.rs +++ b/src/sql/value/mod.rs @@ -16,4 +16,5 @@ pub mod object; pub mod patch; pub mod replace; pub mod set; +pub mod single; pub mod value; diff --git a/src/sql/value/single.rs b/src/sql/value/single.rs new file mode 100644 index 00000000..a3a95dab --- /dev/null +++ b/src/sql/value/single.rs @@ -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, + } + } +}