Make create
API more flexible (#4797)
This commit is contained in:
parent
439ab99e15
commit
9c155fd317
3 changed files with 76 additions and 37 deletions
|
@ -78,17 +78,6 @@ where
|
|||
into_future! {execute_opt}
|
||||
}
|
||||
|
||||
impl<'r, Client, R> IntoFuture for Create<'r, Client, Vec<R>>
|
||||
where
|
||||
Client: Connection,
|
||||
R: DeserializeOwned,
|
||||
{
|
||||
type Output = Result<Option<R>>;
|
||||
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<R>>
|
||||
where
|
||||
C: Connection,
|
||||
{
|
||||
/// Sets content of a record
|
||||
pub fn content<D>(self, data: D) -> Content<'r, C, Option<R>>
|
||||
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,
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<R>(&self, resource: impl IntoResource<R>) -> Create<C, R> {
|
||||
pub fn create<R>(&self, resource: impl CreateResource<R>) -> Create<C, R> {
|
||||
Create {
|
||||
client: Cow::Borrowed(self),
|
||||
resource: resource.into_resource(),
|
||||
|
|
|
@ -315,6 +315,11 @@ pub trait IntoResource<Output> {
|
|||
fn into_resource(self) -> Result<Resource>;
|
||||
}
|
||||
|
||||
/// A trait for types which can be used as a resource selection for a query that returns an `Option`.
|
||||
pub trait CreateResource<Output> {
|
||||
fn into_resource(self) -> Result<Resource>;
|
||||
}
|
||||
|
||||
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<Value> for Resource {
|
||||
fn into_resource(self) -> Result<Resource> {
|
||||
Ok(self)
|
||||
|
@ -413,3 +420,70 @@ impl<R> IntoResource<Vec<R>> for () {
|
|||
Ok(Resource::Unspecified)
|
||||
}
|
||||
}
|
||||
|
||||
// CreateResource
|
||||
|
||||
impl CreateResource<Value> for Resource {
|
||||
fn into_resource(self) -> Result<Resource> {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<R> CreateResource<Option<R>> for Object {
|
||||
fn into_resource(self) -> Result<Resource> {
|
||||
Ok(self.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<R> CreateResource<Option<R>> for RecordId {
|
||||
fn into_resource(self) -> Result<Resource> {
|
||||
Ok(self.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<R> CreateResource<Option<R>> for &RecordId {
|
||||
fn into_resource(self) -> Result<Resource> {
|
||||
Ok(self.clone().into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<R, T, I> CreateResource<Option<R>> for (T, I)
|
||||
where
|
||||
T: Into<String>,
|
||||
I: Into<RecordIdKey>,
|
||||
{
|
||||
fn into_resource(self) -> Result<Resource> {
|
||||
Ok(self.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, R> CreateResource<Option<R>> for Table<T>
|
||||
where
|
||||
T: Into<String>,
|
||||
{
|
||||
fn into_resource(self) -> Result<Resource> {
|
||||
let t = self.0.into();
|
||||
Ok(t.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<R> CreateResource<Option<R>> for &str {
|
||||
fn into_resource(self) -> Result<Resource> {
|
||||
no_colon(self)?;
|
||||
Ok(self.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<R> CreateResource<Option<R>> for String {
|
||||
fn into_resource(self) -> Result<Resource> {
|
||||
no_colon(&self)?;
|
||||
Ok(self.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<R> CreateResource<Option<R>> for &String {
|
||||
fn into_resource(self) -> Result<Resource> {
|
||||
no_colon(self)?;
|
||||
Ok(self.into())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue