Skip to content

Commit

Permalink
Merge pull request #20 from vbrandl/fix/clippy
Browse files Browse the repository at this point in the history
Fix clippy lints
  • Loading branch information
vbrandl authored Apr 13, 2024
2 parents 4890507 + d0f694e commit 94eff36
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ jobs:
run: cargo fmt --all -- --check

- name: Clippy Linting
run: cargo clippy --all-features -- -Dclippy::all -Dclippy::pedantic
- name: Clippy Test Linting
run: cargo clippy --tests -- -Dclippy::all -Dclippy::pedantic
run: cargo clippy --all-features --tests --examples -- -Dclippy::all -Dclippy::pedantic

test:
name: Run Tests
Expand All @@ -62,7 +60,7 @@ jobs:
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}

- name: Run Tests
run: cargo test
run: cargo test --all-features

semver-compat:
name: Check semver compatibilty
Expand Down Expand Up @@ -99,4 +97,4 @@ jobs:
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}

- name: Build Examples
run: cargo build --examples
run: cargo build --examples --all-features
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ version = "4.1.0"
[dependencies]
actix-web = { version = "4.1.0", default-features = false }
futures = "0.3.4"
uuid = { version = "1.1.2", default-features = false, features = ["v4"], optional = true}
uuid = { version = "1.8.0", default-features = false, optional = true }

[dev-dependencies]
actix-web = "4.1.0"
bytes = "1.2.1"

[features]
default = [ "uuid-generator" ]
uuid-generator = [ "uuid" ]
uuid-generator = [ "uuid/v4" ]
uuid-v7-generator = [ "uuid/v7", "uuid/std" ]
2 changes: 1 addition & 1 deletion examples/actix-web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use actix_web::{get, App, HttpServer, Responder};

#[get("/")]
async fn show_request_id(id: RequestId) -> impl Responder {
format!("{}", id.as_str())
id.to_string()
}

#[actix_web::main] // or #[tokio::main]
Expand Down
50 changes: 46 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! used to track errors in an application.
use std::{
fmt,
fmt::{self, Display},
pin::Pin,
task::{Context, Poll},
};
Expand Down Expand Up @@ -32,12 +32,13 @@ pub enum Error {

/// Configuration setting to decide weather the request id from the incoming request header should
/// be used, if present or if a new one should be generated in any case.
#[derive(Clone, Copy, PartialEq, Eq)]
#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub enum IdReuse {
/// Reuse the incoming request id.
UseIncoming,
/// Ignore the incoming request id and generate a random one, even if the request supplied an
/// id.
#[default]
IgnoreIncoming,
}

Expand All @@ -62,6 +63,12 @@ pub struct RequestIdentifier {
#[derive(Clone)]
pub struct RequestId(HeaderValue);

impl Display for RequestId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}

impl ResponseError for Error {}

impl fmt::Display for Error {
Expand Down Expand Up @@ -93,12 +100,22 @@ impl RequestIdentifier {
/// Create a default middleware using [`DEFAULT_HEADER`](./constant.DEFAULT_HEADER.html) as the header name and
/// UUID v4 for ID generation.
#[must_use]
#[cfg(feature = "uuid-generator")]
pub fn with_uuid() -> Self {
Self::default()
}

/// Create a default middleware using [`DEFAULT_HEADER`](./constant.DEFAULT_HEADER.html) as the header name and
/// UUID v7 for ID generation.
#[must_use]
#[cfg(feature = "uuid-v7-generator")]
pub fn with_uuid_v7() -> Self {
Self::with_generator(uuid_v7_generator)
}

/// Create a middleware using a custom header name and UUID v4 for ID generation.
#[must_use]
#[cfg(feature = "uuid-generator")]
pub fn with_header(header_name: &'static str) -> Self {
Self {
header_name,
Expand All @@ -122,7 +139,8 @@ impl RequestIdentifier {
pub fn with_generator(id_generator: Generator) -> Self {
Self {
id_generator,
..Default::default()
header_name: DEFAULT_HEADER,
use_incoming_id: IdReuse::default(),
}
}

Expand All @@ -149,24 +167,34 @@ impl RequestIdentifier {
}
}

#[cfg(feature = "uuid-generator")]
impl Default for RequestIdentifier {
fn default() -> Self {
Self {
header_name: DEFAULT_HEADER,
id_generator: default_generator,
use_incoming_id: IdReuse::IgnoreIncoming,
use_incoming_id: IdReuse::default(),
}
}
}

/// Default UUID v4 based ID generator.
#[cfg(feature = "uuid-generator")]
fn default_generator() -> HeaderValue {
let uuid = Uuid::new_v4();
HeaderValue::from_str(&uuid.to_string())
// This unwrap can never fail since UUID v4 generated IDs are ASCII-only
.unwrap()
}

#[cfg(feature = "uuid-v7-generator")]
fn uuid_v7_generator() -> HeaderValue {
let uuid = Uuid::now_v7();
HeaderValue::from_str(&uuid.to_string())
// This unwrap can never fail since UUID v7 generated IDs are ASCII-only
.unwrap()
}

impl<S, B> Transform<S, ServiceRequest> for RequestIdentifier
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = ActixError>,
Expand Down Expand Up @@ -286,6 +314,20 @@ mod tests {
assert_eq!(uid, body);
}

#[cfg(feature = "uuid-v7-generator")]
#[actix_web::test]
async fn uuid_v7() {
let resp = test_get(RequestIdentifier::with_uuid_v7()).await;
let uid = resp
.headers()
.get(HeaderName::from_static(DEFAULT_HEADER))
.map(|v| v.to_str().unwrap().to_string())
.unwrap();
let body: Bytes = test::read_body(resp).await;
let body = String::from_utf8_lossy(&body);
assert_eq!(uid, body);
}

#[actix_web::test]
async fn deterministic_identifier() {
let resp = test_get(RequestIdentifier::with_generator(|| {
Expand Down

0 comments on commit 94eff36

Please # to comment.