From 9fc0e0cd57aacd08fc4f1974ef817f51b50798ac Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 27 Mar 2024 21:39:40 +0100 Subject: [PATCH] delint --- src/actor.rs | 10 +++++----- src/actor_cache.rs | 2 +- src/digest.rs | 2 +- src/endpoint.rs | 12 +++++------- src/main.rs | 17 +++++++---------- src/relay.rs | 15 +++++++-------- src/state.rs | 2 +- 7 files changed, 27 insertions(+), 33 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index b2e5693..49a82dc 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -126,11 +126,11 @@ impl Actor { id: self.uri(), name: Some(match &self.kind { ActorKind::TagRelay(tag) => - format!("#{}", tag), + format!("#{tag}"), ActorKind::InstanceRelay(instance) => instance.to_string(), ActorKind::LanguageRelay(language) => - format!("in {}", language), + format!("in {language}"), }), icon: Some(activitypub::Media { media_type: Some("Image".to_string()), @@ -149,11 +149,11 @@ impl Actor { }, preferred_username: Some(match &self.kind { ActorKind::TagRelay(tag) => - format!("tag-{}", tag), + format!("tag-{tag}"), ActorKind::InstanceRelay(instance) => - format!("instance-{}", instance), + format!("instance-{instance}"), ActorKind::LanguageRelay(language) => - format!("language-{}", language), + format!("language-{language}"), }), } } diff --git a/src/actor_cache.rs b/src/actor_cache.rs index efdb292..b320d80 100644 --- a/src/actor_cache.rs +++ b/src/actor_cache.rs @@ -72,7 +72,7 @@ impl ActorCache { .expect("queues.remove"); let queue_len = queue.len(); let mut notified = 0usize; - for tx in queue.into_iter() { + for tx in queue { if let Ok(()) = tx.send(result.clone()) { notified += 1; } diff --git a/src/digest.rs b/src/digest.rs index 32d6eab..877cfe2 100644 --- a/src/digest.rs +++ b/src/digest.rs @@ -3,7 +3,7 @@ use http_digest_headers::{DigestHeader, DigestMethod}; pub fn generate_header(body: &[u8]) -> Result { let mut digest_header = DigestHeader::new() .with_method(DigestMethod::SHA256, body) - .map(|h| format!("{}", h)) + .map(|h| format!("{h}")) .map_err(|_| ())?; // mastodon expects uppercase algo name diff --git a/src/endpoint.rs b/src/endpoint.rs index 969d4fa..88921ff 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -37,12 +37,10 @@ where async fn from_request(req: Request, state: &S) -> Result { // validate content-type - let content_type = if let Some(content_type) = req.headers() + let Some(content_type) = req.headers() .get(CONTENT_TYPE) .and_then(|value| value.to_str().ok()) - .and_then(|value| value.split(';').next()) { - content_type - } else { + .and_then(|value| value.split(';').next()) else { return Err((StatusCode::UNSUPPORTED_MEDIA_TYPE, "No content-type".to_string())); }; if ! (content_type.starts_with("application/json") || @@ -57,7 +55,7 @@ where .ok_or((StatusCode::BAD_REQUEST, "No signed headers".to_string()))?; for header in SIGNATURE_HEADERS_REQUIRED { if !signature_headers.iter().any(|h| h == header) { - return Err((StatusCode::BAD_REQUEST, format!("Header {:?} not signed", header))); + return Err((StatusCode::BAD_REQUEST, format!("Header {header:?} not signed"))); } } @@ -75,10 +73,10 @@ where digest_header = digest_header.replace('+', "-") .replace('/', "_"); let digest: DigestHeader = digest_header.parse() - .map_err(|e| (StatusCode::BAD_REQUEST, format!("Cannot parse Digest: header: {}", e)))?; + .map_err(|e| (StatusCode::BAD_REQUEST, format!("Cannot parse Digest: header: {e}")))?; // read body let bytes = Bytes::from_request(req, state).await - .map_err(|e| (StatusCode::BAD_REQUEST, format!("Body: {}", e)))?; + .map_err(|e| (StatusCode::BAD_REQUEST, format!("Body: {e}")))?; // validate digest if ! digest.verify(&bytes).unwrap_or(false) { return Err((StatusCode::BAD_REQUEST, "Digest didn't match".to_string())); diff --git a/src/main.rs b/src/main.rs index c407850..590021b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,12 +39,9 @@ fn track_request(method: &'static str, controller: &'static str, result: &'stati async fn webfinger( Query(params): Query>, ) -> Response { - let resource = match params.get("resource") { - Some(resource) => resource, - None => { - track_request("GET", "webfinger", "invalid"); - return StatusCode::NOT_FOUND.into_response(); - }, + let Some(resource) = params.get("resource") else { + track_request("GET", "webfinger", "invalid"); + return StatusCode::NOT_FOUND.into_response(); }; let Some(target) = Actor::from_uri(resource) else { track_request("GET", "webfinger", "not_found"); @@ -176,7 +173,7 @@ async fn post_relay( tracing::error!("post_relay bad action: {e:?}"); return ( StatusCode::BAD_REQUEST, - format!("Bad action: {:?}", e) + format!("Bad action: {e:?}") ).into_response(); } }; @@ -275,7 +272,7 @@ async fn post_relay( tracing::error!("del_follow: {}", e); track_request("POST", "relay", "unfollow_error"); (StatusCode::INTERNAL_SERVER_ERROR, - format!("{}", e) + format!("{e}") ).into_response() } } @@ -288,7 +285,7 @@ async fn post_relay( } } -/// An empty ActivityStreams outbox just to satisfy the spec +/// An empty `ActivityStreams` outbox just to satisfy the spec async fn outbox() -> Response { Json(json!({ "@context": "https://www.w3.org/ns/activitystreams", @@ -342,7 +339,7 @@ async fn nodeinfo(axum::extract::State(state): axum::extract::State) -> R })).into_response() } -/// Expected by AodeRelay +/// Expected by `AodeRelay` async fn instanceinfo() -> Response { Json(json!({ "title": env!("CARGO_PKG_NAME"), diff --git a/src/relay.rs b/src/relay.rs index 06f1d6b..bdd0f60 100644 --- a/src/relay.rs +++ b/src/relay.rs @@ -111,8 +111,8 @@ fn spawn_worker(client: Arc) -> Sender { let mut last_request = None; while let Some(Job { post_url, actor_id, key_id, private_key, body, inbox_url }) = rx.next().await { - if errors > 0 && last_request.map_or(false, |last_request| - Instant::now() - last_request < Duration::from_secs(10) * errors + if errors > 0 && last_request.map_or(false, |last_request: Instant| + last_request.elapsed() < Duration::from_secs(10) * errors ) { // there have been errors, skip for time proportional // to the number of subsequent errors @@ -162,13 +162,12 @@ pub fn spawn( continue; } }; - let post_url = match post.url { - Some(ref url) => Arc::new(url.to_string()), + let post_url = if let Some(url) = post.url { + Arc::new(url.to_string()) + } else { // skip reposts - None => { - increment_counter!("relay_posts_total", "action" => "skip"); - continue; - } + increment_counter!("relay_posts_total", "action" => "skip"); + continue; }; let mut seen_actors = HashSet::new(); let mut seen_inboxes = HashSet::new(); diff --git a/src/state.rs b/src/state.rs index f12ddfc..530baa1 100644 --- a/src/state.rs +++ b/src/state.rs @@ -31,7 +31,7 @@ impl State { database, redis: redis.map(|(connection, in_topic)| (connection, Arc::new(in_topic))), client: Arc::new(client), - actor_cache: Default::default(), + actor_cache: ActorCache::default(), hostname: Arc::new(config.hostname), priv_key, pub_key,