From bf7f4d5381203d5c27b2811d62c5b1781533aa2b Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 19 Nov 2025 17:01:36 +0000 Subject: fix some clippy fmt warnings --- src/config.rs | 6 ++--- src/http/landing.rs | 15 ++++++----- src/http/mod.rs | 11 ++++---- src/http/websocket.rs | 24 ++++++++--------- src/nostr/events.rs | 74 ++++++++++++++++++++------------------------------- 5 files changed, 57 insertions(+), 73 deletions(-) (limited to 'src') diff --git a/src/config.rs b/src/config.rs index 252873d..b4dd853 100644 --- a/src/config.rs +++ b/src/config.rs @@ -19,10 +19,8 @@ impl Config { dotenvy::dotenv().ok(); Ok(Config { - domain: env::var("NGIT_DOMAIN") - .unwrap_or_else(|_| "localhost:8080".to_string()), - owner_npub: env::var("NGIT_OWNER_NPUB") - .context("NGIT_OWNER_NPUB must be set")?, + domain: env::var("NGIT_DOMAIN").unwrap_or_else(|_| "localhost:8080".to_string()), + owner_npub: env::var("NGIT_OWNER_NPUB").context("NGIT_OWNER_NPUB must be set")?, relay_name: env::var("NGIT_RELAY_NAME") .unwrap_or_else(|_| "ngit-grasp relay".to_string()), relay_description: env::var("NGIT_RELAY_DESCRIPTION") diff --git a/src/http/landing.rs b/src/http/landing.rs index 35e49e5..976ec50 100644 --- a/src/http/landing.rs +++ b/src/http/landing.rs @@ -1,7 +1,6 @@ /// Landing Page Handler -/// +/// /// Serves the HTML landing page or upgrades to WebSocket for Nostr relay connections. - use actix_web::{web, HttpRequest, HttpResponse, Result}; use nostr_relay_builder::LocalRelay; @@ -16,12 +15,16 @@ pub async fn handle( ) -> Result { // Check if this is a WebSocket upgrade request if let Some(upgrade) = req.headers().get("upgrade") { - if upgrade.to_str().unwrap_or("").eq_ignore_ascii_case("websocket") { + if upgrade + .to_str() + .unwrap_or("") + .eq_ignore_ascii_case("websocket") + { // Delegate to WebSocket handler return crate::http::websocket::handle(req, stream, relay).await; } } - + // Otherwise, serve the landing page let html = format!( include_str!("../../templates/landing.html"), @@ -30,8 +33,8 @@ pub async fn handle( domain = config.domain, bind_address = config.bind_address, ); - + Ok(HttpResponse::Ok() .content_type("text/html; charset=utf-8") .body(html)) -} \ No newline at end of file +} diff --git a/src/http/mod.rs b/src/http/mod.rs index 286e8ff..b434c69 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -1,7 +1,6 @@ /// HTTP Server Module -/// +/// /// Provides actix-web HTTP server with WebSocket upgrade support for the Nostr relay. - pub mod landing; pub mod websocket; @@ -13,9 +12,9 @@ use crate::config::Config; /// Start the HTTP server with integrated Nostr relay pub async fn run_server(config: Config, relay: LocalRelay) -> anyhow::Result<()> { let bind_addr = config.bind_address.clone(); - + tracing::info!("Starting HTTP server on {}", bind_addr); - + HttpServer::new(move || { App::new() .app_data(web::Data::new(config.clone())) @@ -26,6 +25,6 @@ pub async fn run_server(config: Config, relay: LocalRelay) -> anyhow::Result<()> .bind(&bind_addr)? .run() .await?; - + Ok(()) -} \ No newline at end of file +} diff --git a/src/http/websocket.rs b/src/http/websocket.rs index 7af847a..0171013 100644 --- a/src/http/websocket.rs +++ b/src/http/websocket.rs @@ -1,8 +1,7 @@ /// WebSocket Handler -/// +/// /// Handles WebSocket upgrade requests and passes connections to the Nostr relay. - -use actix_web::{web, HttpRequest, HttpResponse, Result, Error}; +use actix_web::{web, Error, HttpRequest, HttpResponse, Result}; use actix_ws::Message; use futures_util::StreamExt; use nostr_relay_builder::LocalRelay; @@ -14,19 +13,20 @@ pub async fn handle( relay: web::Data, ) -> Result { let (response, mut session, mut msg_stream) = actix_ws::handle(&req, stream)?; - - let peer_addr = req.peer_addr() + + let peer_addr = req + .peer_addr() .unwrap_or_else(|| "0.0.0.0:0".parse().unwrap()); - + tracing::debug!("WebSocket connection from {}", peer_addr); - + // Spawn task to handle the WebSocket connection // TODO: Will use relay.take_connection() for full Nostr relay integration let _relay = relay.get_ref().clone(); actix_web::rt::spawn(async move { // Create a channel to communicate between actix-ws and relay let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel(); - + // Spawn task to send messages from relay to client let mut session_clone = session.clone(); actix_web::rt::spawn(async move { @@ -36,7 +36,7 @@ pub async fn handle( } } }); - + // Handle incoming messages from client while let Some(Ok(msg)) = msg_stream.next().await { match msg { @@ -65,9 +65,9 @@ pub async fn handle( Message::Nop => {} } } - + tracing::debug!("WebSocket connection closed for {}", peer_addr); }); - + Ok(response) -} \ No newline at end of file +} diff --git a/src/nostr/events.rs b/src/nostr/events.rs index 88aefed..21dd2dd 100644 --- a/src/nostr/events.rs +++ b/src/nostr/events.rs @@ -1,12 +1,11 @@ /// NIP-34 Git Repository Event Handling -/// -/// This module handles Git repository announcements (kind 30617) and +/// +/// This module handles Git repository announcements (kind 30617) and /// repository state announcements (kind 30618) according to NIP-34 and GRASP-01. /// /// Reference: /// - NIP-34: https://nips.nostr.com/34 /// - GRASP-01: https://gitworkshop.dev/danconwaydev.com/grasp/01.md - use anyhow::{anyhow, Result}; use nostr_sdk::{Event, Kind, TagKind, ToBech32}; @@ -138,8 +137,8 @@ impl RepositoryAnnouncement { } /// Check if this announcement lists the service (both clone and relay) - /// - /// GRASP-01 requirement: MUST reject announcements that do not list + /// + /// GRASP-01 requirement: MUST reject announcements that do not list /// the service in both `clone` and `relays` tags unless implementing GRASP-05. pub fn lists_service(&self, domain: &str) -> bool { self.has_clone_url(domain) && self.has_relay(domain) @@ -278,19 +277,19 @@ impl RepositoryState { } /// Validate a repository announcement according to GRASP-01 -/// +/// /// Returns Ok(()) if valid, Err with reason if invalid. pub fn validate_announcement(event: &Event, domain: &str) -> Result<()> { // Must be kind 30617 if event.kind != Kind::from(KIND_REPOSITORY_ANNOUNCEMENT) { - return Err(anyhow!("Invalid kind: expected {}", KIND_REPOSITORY_ANNOUNCEMENT)); + return Err(anyhow!( + "Invalid kind: expected {}", + KIND_REPOSITORY_ANNOUNCEMENT + )); } // Must have identifier - let has_identifier = event - .tags - .iter() - .any(|t| t.kind() == TagKind::d()); + let has_identifier = event.tags.iter().any(|t| t.kind() == TagKind::d()); if !has_identifier { return Err(anyhow!("Missing required 'd' tag (identifier)")); } @@ -298,7 +297,7 @@ pub fn validate_announcement(event: &Event, domain: &str) -> Result<()> { // Parse full announcement to validate structure let announcement = RepositoryAnnouncement::from_event(event.clone())?; - // GRASP-01: MUST reject announcements that do not list the service + // GRASP-01: MUST reject announcements that do not list the service // in both `clone` and `relays` tags unless implementing GRASP-05 if !announcement.lists_service(domain) { return Err(anyhow!( @@ -313,7 +312,7 @@ pub fn validate_announcement(event: &Event, domain: &str) -> Result<()> { } /// Validate a repository state announcement according to GRASP-01 -/// +/// /// Returns Ok(()) if valid, Err with reason if invalid. pub fn validate_state(event: &Event) -> Result<()> { // Must be kind 30618 @@ -322,10 +321,7 @@ pub fn validate_state(event: &Event) -> Result<()> { } // Must have identifier - let has_identifier = event - .tags - .iter() - .any(|t| t.kind() == TagKind::d()); + let has_identifier = event.tags.iter().any(|t| t.kind() == TagKind::d()); if !has_identifier { return Err(anyhow!("Missing required 'd' tag (identifier)")); } @@ -352,7 +348,7 @@ mod tests { relays: Vec<&str>, ) -> Event { use nostr_sdk::Tag; - + let mut tags = vec![Tag::custom( nostr_sdk::TagKind::d(), vec![identifier.to_string()], @@ -372,18 +368,15 @@ mod tests { )); } - EventBuilder::new( - Kind::from(KIND_REPOSITORY_ANNOUNCEMENT), - "Test repository", - ) - .tags(tags) - .sign_with_keys(keys) - .unwrap() + EventBuilder::new(Kind::from(KIND_REPOSITORY_ANNOUNCEMENT), "Test repository") + .tags(tags) + .sign_with_keys(keys) + .unwrap() } fn create_state_event(keys: &Keys, identifier: &str, branches: Vec<(&str, &str)>) -> Event { use nostr_sdk::Tag; - + let mut tags = vec![Tag::custom( nostr_sdk::TagKind::d(), vec![identifier.to_string()], @@ -392,10 +385,7 @@ mod tests { for (branch, commit) in branches { tags.push(Tag::custom( nostr_sdk::TagKind::Custom("ref".into()), - vec![ - format!("refs/heads/{}", branch), - commit.to_string(), - ], + vec![format!("refs/heads/{}", branch), commit.to_string()], )); } @@ -428,12 +418,9 @@ mod tests { #[test] fn test_parse_announcement_missing_identifier() { let keys = create_test_keys(); - let event = EventBuilder::new( - Kind::from(KIND_REPOSITORY_ANNOUNCEMENT), - "Test repository", - ) - .sign_with_keys(&keys) - .unwrap(); + let event = EventBuilder::new(Kind::from(KIND_REPOSITORY_ANNOUNCEMENT), "Test repository") + .sign_with_keys(&keys) + .unwrap(); let result = RepositoryAnnouncement::from_event(event); assert!(result.is_err()); @@ -539,7 +526,7 @@ mod tests { #[test] fn test_announcement_maintainers() { use nostr_sdk::Tag; - + let keys = create_test_keys(); let maintainer_keys = create_test_keys(); @@ -558,13 +545,10 @@ mod tests { // Add maintainer tags.push(Tag::public_key(maintainer_keys.public_key())); - let event = EventBuilder::new( - Kind::from(KIND_REPOSITORY_ANNOUNCEMENT), - "Test repository", - ) - .tags(tags) - .sign_with_keys(&keys) - .unwrap(); + let event = EventBuilder::new(Kind::from(KIND_REPOSITORY_ANNOUNCEMENT), "Test repository") + .tags(tags) + .sign_with_keys(&keys) + .unwrap(); let announcement = RepositoryAnnouncement::from_event(event).unwrap(); assert_eq!(announcement.maintainers.len(), 1); @@ -573,7 +557,7 @@ mod tests { #[test] fn test_state_with_tags() { use nostr_sdk::Tag; - + let keys = create_test_keys(); let mut tags = vec![Tag::custom( nostr_sdk::TagKind::d(), -- cgit v1.2.3