Skip to content

Commit

Permalink
Replace derive_more derives with manual implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
svix-jplatte committed Mar 7, 2024
1 parent 5ed75db commit 615fe0d
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 79 deletions.
1 change: 0 additions & 1 deletion crates/aide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ serde_json = "1"
thiserror = "1"
tracing = "0"
aide-macros = { version = "0.7.0", path = "../aide-macros", optional = true }
derive_more = "0.99.17"

bytes = { version = "1", optional = true }
http = { version = "1.0.0", optional = true }
Expand Down
55 changes: 36 additions & 19 deletions crates/aide/src/helpers/no_api.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
use derive_more::{AsMut, AsRef, Deref, DerefMut, From};
use std::ops::{Deref, DerefMut};

use serde::{Deserialize, Serialize};

use crate::{OperationInput, OperationOutput};

/// Allows non [`OperationInput`] or [`OperationOutput`] types to be used in aide handlers with a default empty documentation.
/// Allows non [`OperationInput`] or [`OperationOutput`] types to be used in aide handlers with a default empty documentation.
/// For types that already implement [`OperationInput`] or [`OperationOutput`] it overrides the documentation and hides it.
/// ```ignore
/// pub async fn my_sqlx_tx_endpoint(
/// NoApi(mut tx): NoApi<Tx<sqlx::Any>> // allows usage of the TX
/// ) -> NoApi<Json<YourResult>> // Hides the API of the return type
/// # {}
/// ```
#[derive(
Copy,
Clone,
Debug,
Ord,
PartialOrd,
Eq,
PartialEq,
Hash,
Serialize,
Deserialize,
Deref,
DerefMut,
AsRef,
AsMut,
From,
)]
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct NoApi<T>(pub T);

impl<T> NoApi<T> {
Expand All @@ -37,6 +22,38 @@ impl<T> NoApi<T> {
}
}

impl<T> Deref for NoApi<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T> DerefMut for NoApi<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<T> AsRef<T> for NoApi<T> {
fn as_ref(&self) -> &T {
&self.0
}
}

impl<T> AsMut<T> for NoApi<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.0
}
}

impl<T> From<T> for NoApi<T> {
fn from(value: T) -> Self {
Self(value)
}
}

impl<T> OperationInput for NoApi<T> {}

impl<T> OperationOutput for NoApi<T> {
Expand Down
70 changes: 38 additions & 32 deletions crates/aide/src/helpers/use_api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::marker::PhantomData;
use std::{
marker::PhantomData,
ops::{Deref, DerefMut},
};

use derive_more::{AsMut, AsRef, Deref, DerefMut};
use serde::{Deserialize, Serialize};

use crate::gen::GenContext;
Expand All @@ -25,36 +27,8 @@ impl<T> IntoApi for T {

/// Allows non [`OperationInput`] or [`OperationOutput`] types to be used in aide handlers with the api documentation of [A].
/// For types that already implement [`OperationInput`] or [`OperationOutput`] it overrides the documentation with the provided one.
#[derive(
Copy,
Clone,
Debug,
Ord,
PartialOrd,
Eq,
PartialEq,
Hash,
Serialize,
Deserialize,
Deref,
DerefMut,
AsRef,
AsMut,
)]
pub struct UseApi<T, A>(
#[as_ref]
#[as_mut]
#[deref]
#[deref_mut]
pub T,
pub PhantomData<A>,
);

impl<T, A> From<T> for UseApi<T, A> {
fn from(value: T) -> Self {
Self(value, Default::default())
}
}
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct UseApi<T, A>(pub T, pub PhantomData<A>);

impl<T, A> UseApi<T, A> {
/// Unwraps [Self] into its inner type
Expand All @@ -63,6 +37,38 @@ impl<T, A> UseApi<T, A> {
}
}

impl<T, A> Deref for UseApi<T, A> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T, A> DerefMut for UseApi<T, A> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<T, A> AsRef<T> for UseApi<T, A> {
fn as_ref(&self) -> &T {
&self.0
}
}

impl<T, A> AsMut<T> for UseApi<T, A> {
fn as_mut(&mut self) -> &mut T {
&mut self.0
}
}

impl<T, A> From<T> for UseApi<T, A> {
fn from(value: T) -> Self {
Self(value, Default::default())
}
}

impl<T, A> OperationInput for UseApi<T, A>
where
A: OperationInput,
Expand Down
72 changes: 45 additions & 27 deletions crates/aide/src/helpers/with_api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::marker::PhantomData;
use std::{
marker::PhantomData,
ops::{Deref, DerefMut},
};

use derive_more::{AsMut, AsRef, Deref, DerefMut};
use serde::{Deserialize, Serialize};

use crate::gen::GenContext;
Expand Down Expand Up @@ -62,33 +64,11 @@ pub trait ApiOverride {
type Target;
}

/// Allows non [`OperationInput`] or [`OperationOutput`] types to be used in aide handlers with a provided documentation.
/// Allows non [`OperationInput`] or [`OperationOutput`] types to be used in aide handlers with a provided documentation.
/// For types that already implement [`OperationInput`] or [`OperationOutput`] it overrides the documentation with the provided one.
/// See [`ApiOverride`] on how to implement such an override
#[derive(
Copy,
Clone,
Debug,
Ord,
PartialOrd,
Eq,
PartialEq,
Hash,
Serialize,
Deserialize,
Deref,
DerefMut,
AsRef,
AsMut,
)]
pub struct WithApi<T>(
#[as_ref]
#[as_mut]
#[deref]
#[deref_mut]
pub T::Target,
pub PhantomData<T>,
)
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct WithApi<T>(pub T::Target, pub PhantomData<T>)
where
T: ApiOverride;

Expand All @@ -102,6 +82,44 @@ where
}
}

impl<T> Deref for WithApi<T>
where
T: ApiOverride,
{
type Target = T::Target;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T> DerefMut for WithApi<T>
where
T: ApiOverride,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<T> AsRef<T::Target> for WithApi<T>
where
T: ApiOverride,
{
fn as_ref(&self) -> &T::Target {
&self.0
}
}

impl<T> AsMut<T::Target> for WithApi<T>
where
T: ApiOverride,
{
fn as_mut(&mut self) -> &mut T::Target {
&mut self.0
}
}

impl<T> OperationInput for WithApi<T>
where
T: OperationInput + ApiOverride,
Expand Down

0 comments on commit 615fe0d

Please sign in to comment.