Skip to content

Commit

Permalink
refactor: update derive_more to v1
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Aug 9, 2024
1 parent 4cdd918 commit 7e9759f
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 33 deletions.
3 changes: 3 additions & 0 deletions actix-web-lab/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Implement `Clone` for `extract::Path<T: Clone>`.
- `header::CacheControl` now `Deref`s to a slice instead of a `Vec`.

## 0.21.0

- Remove use of `async-trait` on `RequestSignatureScheme` trait.
Expand Down
2 changes: 1 addition & 1 deletion actix-web-lab/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ arc-swap = "1.1"
bytes = "1"
bytestring = "1"
csv = "1.1"
derive_more = { version = "0.99.8", features = ["nightly"] }
derive_more = { version = "1", features = ["display", "error"] }
futures-core = "0.3.17"
futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
http = "0.2.7"
Expand Down
4 changes: 2 additions & 2 deletions actix-web-lab/src/body_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ where
T: FromRequest + 'static,
T::Error: fmt::Debug + fmt::Display,
{
#[display(fmt = "Wrapped extractor error: {_0}")]
#[display("Wrapped extractor error: {_0}")]
Extractor(T::Error),

#[display(fmt = "Body was too large")]
#[display("Body was too large")]
Overflow,
}

Expand Down
14 changes: 6 additions & 8 deletions actix-web-lab/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use std::{
task::{ready, Context, Poll},
};

use actix_web::{
dev, http::StatusCode, web, Error, FromRequest, HttpMessage, HttpRequest, ResponseError,
};
use actix_web::{dev, http::StatusCode, web, FromRequest, HttpMessage, HttpRequest, ResponseError};
use derive_more::{Display, Error};
use futures_core::Stream as _;
use tracing::debug;
Expand Down Expand Up @@ -93,7 +91,7 @@ impl<const LIMIT: usize> Bytes<LIMIT> {

/// See [here](#extractor) for example of usage as an extractor.
impl<const LIMIT: usize> FromRequest for Bytes<LIMIT> {
type Error = Error;
type Error = actix_web::Error;
type Future = BytesExtractFut<LIMIT>;

#[inline]
Expand All @@ -112,7 +110,7 @@ pub struct BytesExtractFut<const LIMIT: usize> {
}

impl<const LIMIT: usize> Future for BytesExtractFut<LIMIT> {
type Output = Result<Bytes<LIMIT>, Error>;
type Output = actix_web::Result<Bytes<LIMIT>>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
Expand Down Expand Up @@ -219,15 +217,15 @@ impl<const LIMIT: usize> Future for BytesBody<LIMIT> {
#[non_exhaustive]
pub enum BytesPayloadError {
/// Payload size is bigger than allowed & content length header set. (default: 4MiB)
#[display(fmt = "Payload ({length} bytes) is larger than allowed (limit: {limit} bytes).")]
#[display("Payload ({length} bytes) is larger than allowed (limit: {limit} bytes).")]
OverflowKnownLength { length: usize, limit: usize },

/// Payload size is bigger than allowed but no content length header set. (default: 4MiB)
#[display(fmt = "Payload has exceeded limit ({limit} bytes).")]
#[display("Payload has exceeded limit ({limit} bytes).")]
Overflow { limit: usize },

/// Payload error.
#[display(fmt = "Error that occur during reading payload: {_0}")]
#[display("Error that occur during reading payload: {_0}")]
Payload(actix_web::error::PayloadError),
}

Expand Down
10 changes: 5 additions & 5 deletions actix-web-lab/src/cache_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ use std::{fmt, str};
use actix_http::{
error::ParseError,
header::{
fmt_comma_delimited, from_comma_delimited, Header, HeaderName, HeaderValue,
self, fmt_comma_delimited, from_comma_delimited, Header, HeaderName, HeaderValue,
InvalidHeaderValue, TryIntoHeaderValue,
},
HttpMessage,
};
use actix_web::http::header;
use derive_more::{Deref, DerefMut};

/// The `Cache-Control` header, defined in [RFC 7234 §5.2].
///
Expand Down Expand Up @@ -65,11 +63,13 @@ use derive_more::{Deref, DerefMut};
///
/// [RFC 7234 §5.2]: https://datatracker.ietf.org/doc/html/rfc7234#section-5.2
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
#[derive(Debug, Clone, PartialEq, Eq, Deref, DerefMut)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CacheControl(pub Vec<CacheDirective>);

impl_more::forward_deref_and_mut!(CacheControl => [CacheDirective]);

impl fmt::Display for CacheControl {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt_comma_delimited(f, &self.0[..])
}
}
Expand Down
6 changes: 4 additions & 2 deletions actix-web-lab/src/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
use actix_web::{HttpRequest, HttpResponse, Responder};
use bytes::Bytes;
use derive_more::{Deref, DerefMut, Display};
use derive_more::Display;
use mime::Mime;
use once_cell::sync::Lazy;
use serde::Serialize;

static CBOR_MIME: Lazy<Mime> = Lazy::new(|| "application/cbor".parse().unwrap());

/// CBOR responder.
#[derive(Debug, Deref, DerefMut, Display)]
#[derive(Debug, Display)]
pub struct Cbor<T>(pub T);

impl_more::impl_deref_and_mut!(<T> in Cbor<T> => T);

impl<T: Serialize> Responder for Cbor<T> {
type Body = Bytes;

Expand Down
10 changes: 7 additions & 3 deletions actix-web-lab/src/msgpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use actix_web::{HttpRequest, HttpResponse, Responder};
use bytes::Bytes;
use derive_more::{Deref, DerefMut, Display};
use derive_more::Display;
use mime::Mime;
use once_cell::sync::Lazy;
use serde::Serialize;
Expand All @@ -12,9 +12,11 @@ static MSGPACK_MIME: Lazy<Mime> = Lazy::new(|| "application/msgpack".parse().unw
/// MessagePack responder.
///
/// If you require the fields to be named, use [`MessagePackNamed`].
#[derive(Debug, Deref, DerefMut, Display)]
#[derive(Debug, Display)]
pub struct MessagePack<T>(pub T);

impl_more::impl_deref_and_mut!(<T> in MessagePack<T> => T);

impl<T: Serialize> Responder for MessagePack<T> {
type Body = Bytes;

Expand All @@ -29,9 +31,11 @@ impl<T: Serialize> Responder for MessagePack<T> {
}

/// MessagePack responder with named fields.
#[derive(Debug, Deref, DerefMut, Display)]
#[derive(Debug, Display)]
pub struct MessagePackNamed<T>(pub T);

impl_more::impl_deref_and_mut!(<T> in MessagePackNamed<T> => T);

impl<T: Serialize> Responder for MessagePackNamed<T> {
type Body = Bytes;

Expand Down
9 changes: 6 additions & 3 deletions actix-web-lab/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use actix_web::{
error::{Error, ErrorNotFound},
FromRequest, HttpRequest,
};
use derive_more::{AsRef, Display, From};
use derive_more::Display;
use serde::de;
use tracing::debug;

Expand Down Expand Up @@ -53,7 +53,7 @@ use tracing::debug;
/// format!("Welcome {}!", info.name)
/// }
/// ```
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, AsRef, Display, From)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Display)]
pub struct Path<T>(pub T);

impl<T> Path<T> {
Expand All @@ -63,6 +63,9 @@ impl<T> Path<T> {
}
}

impl_more::impl_as_ref!(Path<T> => T);
impl_more::impl_from!(<T> in T => Path<T>);

/// See [here](#Examples) for example of usage as an extractor.
impl<T> FromRequest for Path<T>
where
Expand Down Expand Up @@ -98,7 +101,7 @@ mod tests {
use super::*;

#[derive(Deserialize, Debug, Display)]
#[display(fmt = "MyStruct({key}, {value})")]
#[display("MyStruct({key}, {value})")]
struct MyStruct {
key: String,
value: String,
Expand Down
4 changes: 2 additions & 2 deletions actix-web-lab/src/request_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ where
S::Error: fmt::Debug + fmt::Display,
{
/// Inner extractor error.
#[display(fmt = "Inner extractor error: {_0}")]
#[display("Inner extractor error: {_0}")]
Extractor(T::Error),

/// Signature calculation error.
#[display(fmt = "Signature calculation error: {_0}")]
#[display("Signature calculation error: {_0}")]
Signature(S::Error),
}

Expand Down
12 changes: 7 additions & 5 deletions actix-web-lab/src/thin_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::any::type_name;

use actix_utils::future::{ready, Ready};
use actix_web::{dev::Payload, error, FromRequest, HttpRequest};
use derive_more::{AsMut, AsRef, Deref, DerefMut};
use tracing::log;

/// Application data wrapper and extractor for cheaply-cloned types.
///
Expand Down Expand Up @@ -38,22 +36,26 @@ use tracing::log;
/// .service(web::resource("/").get(index))
/// # ;
/// ```
#[derive(Debug, Clone, AsRef, AsMut, Deref, DerefMut)]
#[derive(Debug, Clone)]
pub struct ThinData<T>(pub T);

impl_more::impl_as_ref!(ThinData<T> => T);
impl_more::impl_as_mut!(ThinData<T> => T);
impl_more::impl_deref_and_mut!(<T> in ThinData<T> => T);

impl<T: Clone + 'static> FromRequest for ThinData<T> {
type Error = actix_web::Error;
type Future = Ready<Result<Self, Self::Error>>;

#[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ready(req.app_data::<Self>().cloned().ok_or_else(|| {
log::debug!(
tracing::debug!(
"Failed to extract `ThinData<{}>` for `{}` handler. For the ThinData extractor to work \
correctly, wrap the data with `ThinData()` and pass it to `App::app_data()`. \
Ensure that types align in both the set and retrieve calls.",
type_name::<T>(),
req.match_name().unwrap_or_else(|| req.path())
req.match_name().unwrap_or(req.path())
);

error::ErrorInternalServerError(
Expand Down
6 changes: 4 additions & 2 deletions actix-web-lab/src/x_forwarded_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use actix_http::{
HttpMessage,
};
use actix_web::FromRequest;
use derive_more::{Deref, DerefMut, Display};
use derive_more::Display;
use http::uri::PathAndQuery;

/// Conventional `X-Forwarded-Prefix` header.
Expand Down Expand Up @@ -41,9 +41,11 @@ pub const X_FORWARDED_PREFIX: HeaderName = HeaderName::from_static("x-forwarded-
/// ```
///
/// [RFC 7234 §5.2]: https://datatracker.ietf.org/doc/html/rfc7234#section-5.2
#[derive(Debug, Clone, PartialEq, Eq, Deref, DerefMut, Display)]
#[derive(Debug, Clone, PartialEq, Eq, Display)]
pub struct XForwardedPrefix(pub PathAndQuery);

impl_more::impl_deref_and_mut!(XForwardedPrefix => PathAndQuery);

impl TryIntoHeaderValue for XForwardedPrefix {
type Error = InvalidHeaderValue;

Expand Down
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ downgrade-for-msrv:
clippy toolchain="":
cargo {{ toolchain }} clippy --workspace --all-targets --all-features

# Run tests on all crates in workspace using specified (or default) toolchain and watch for changes.
clippy-watch toolchain="":
cargo watch -- just clippy {{ toolchain }}

# Run tests on all crates in workspace using its MSRV.
test-msrv: downgrade-for-msrv (test msrv_rustup)

Expand Down

0 comments on commit 7e9759f

Please sign in to comment.