From 9c155fd317359ece19285a2c5602601534e46e87 Mon Sep 17 00:00:00 2001 From: Rushmore Mushambi Date: Tue, 17 Sep 2024 14:47:36 +0100 Subject: [PATCH] Make `create` API more flexible (#4797) --- sdk/src/api/method/create.rs | 36 ------------------ sdk/src/api/method/mod.rs | 3 +- sdk/src/api/opt/resource.rs | 74 ++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 37 deletions(-) diff --git a/sdk/src/api/method/create.rs b/sdk/src/api/method/create.rs index ea05d82e..7b0e431b 100644 --- a/sdk/src/api/method/create.rs +++ b/sdk/src/api/method/create.rs @@ -78,17 +78,6 @@ where into_future! {execute_opt} } -impl<'r, Client, R> IntoFuture for Create<'r, Client, Vec> -where - Client: Connection, - R: DeserializeOwned, -{ - type Output = Result>; - type IntoFuture = BoxFuture<'r, Self::Output>; - - into_future! {execute_opt} -} - impl<'r, C> Create<'r, C, Value> where C: Connection, @@ -138,28 +127,3 @@ where }) } } - -impl<'r, C, R> Create<'r, C, Vec> -where - C: Connection, -{ - /// Sets content of a record - pub fn content(self, data: D) -> Content<'r, C, Option> - where - D: Serialize + 'static, - { - Content::from_closure(self.client, || { - let content = to_core_value(data)?; - - let data = match content { - CoreValue::None | CoreValue::Null => None, - content => Some(content), - }; - - Ok(Command::Create { - what: self.resource?, - data, - }) - }) - } -} diff --git a/sdk/src/api/method/mod.rs b/sdk/src/api/method/mod.rs index b15f8e3f..74466191 100644 --- a/sdk/src/api/method/mod.rs +++ b/sdk/src/api/method/mod.rs @@ -92,6 +92,7 @@ pub use use_db::UseDb; pub use use_ns::UseNs; pub use version::Version; +use super::opt::CreateResource; use super::opt::IntoResource; /// A alias for an often used type of future returned by async methods in this library. @@ -749,7 +750,7 @@ where /// # Ok(()) /// # } /// ``` - pub fn create(&self, resource: impl IntoResource) -> Create { + pub fn create(&self, resource: impl CreateResource) -> Create { Create { client: Cow::Borrowed(self), resource: resource.into_resource(), diff --git a/sdk/src/api/opt/resource.rs b/sdk/src/api/opt/resource.rs index 40a51e26..9ea01c7e 100644 --- a/sdk/src/api/opt/resource.rs +++ b/sdk/src/api/opt/resource.rs @@ -315,6 +315,11 @@ pub trait IntoResource { fn into_resource(self) -> Result; } +/// A trait for types which can be used as a resource selection for a query that returns an `Option`. +pub trait CreateResource { + fn into_resource(self) -> Result; +} + fn no_colon(a: &str) -> Result<()> { if a.contains(':') { return Err(Error::TableColonId { @@ -325,6 +330,8 @@ fn no_colon(a: &str) -> Result<()> { Ok(()) } +// IntoResource + impl IntoResource for Resource { fn into_resource(self) -> Result { Ok(self) @@ -413,3 +420,70 @@ impl IntoResource> for () { Ok(Resource::Unspecified) } } + +// CreateResource + +impl CreateResource for Resource { + fn into_resource(self) -> Result { + Ok(self) + } +} + +impl CreateResource> for Object { + fn into_resource(self) -> Result { + Ok(self.into()) + } +} + +impl CreateResource> for RecordId { + fn into_resource(self) -> Result { + Ok(self.into()) + } +} + +impl CreateResource> for &RecordId { + fn into_resource(self) -> Result { + Ok(self.clone().into()) + } +} + +impl CreateResource> for (T, I) +where + T: Into, + I: Into, +{ + fn into_resource(self) -> Result { + Ok(self.into()) + } +} + +impl CreateResource> for Table +where + T: Into, +{ + fn into_resource(self) -> Result { + let t = self.0.into(); + Ok(t.into()) + } +} + +impl CreateResource> for &str { + fn into_resource(self) -> Result { + no_colon(self)?; + Ok(self.into()) + } +} + +impl CreateResource> for String { + fn into_resource(self) -> Result { + no_colon(&self)?; + Ok(self.into()) + } +} + +impl CreateResource> for &String { + fn into_resource(self) -> Result { + no_colon(self)?; + Ok(self.into()) + } +}