From 08e87ca01f7bc8b8bf1ae72e1e4c442adfa3356b Mon Sep 17 00:00:00 2001 From: Mario Date: Mon, 27 May 2024 15:59:10 +0200 Subject: [PATCH] refactor: [#670] new print_response function from trait implemented --- src/console/clients/udp/app.rs | 40 ++++------------------------------ 1 file changed, 4 insertions(+), 36 deletions(-) diff --git a/src/console/clients/udp/app.rs b/src/console/clients/udp/app.rs index 9621cec5..1675fd9e 100644 --- a/src/console/clients/udp/app.rs +++ b/src/console/clients/udp/app.rs @@ -60,15 +60,14 @@ use std::net::{SocketAddr, ToSocketAddrs}; use std::str::FromStr; use anyhow::Context; -use aquatic_udp_protocol::Response::{self, AnnounceIpv4, AnnounceIpv6, Connect, Error, Scrape}; -use aquatic_udp_protocol::{Port, TransactionId}; +use aquatic_udp_protocol::{Port, Response, TransactionId}; use clap::{Parser, Subcommand}; use log::{debug, LevelFilter}; use torrust_tracker_primitives::info_hash::InfoHash as TorrustInfoHash; use url::Url; use crate::console::clients::udp::checker; -use crate::console::clients::udp::responses::{AnnounceResponseDto, ConnectResponseDto, ErrorResponseDto, ScrapeResponseDto}; +use crate::console::clients::udp::responses::{DtoToJson, ResponseDto}; const ASSIGNED_BY_OS: u16 = 0; const RANDOM_TRANSACTION_ID: i32 = -888_840_697; @@ -117,7 +116,8 @@ pub async fn run() -> anyhow::Result<()> { } => handle_scrape(&tracker_socket_addr, &info_hashes).await?, }; - print_response(response) + let response_dto: ResponseDto = response.into(); + response_dto.print_response() } fn setup_logging(level: LevelFilter) { @@ -169,38 +169,6 @@ async fn handle_scrape(tracker_socket_addr: &SocketAddr, info_hashes: &[TorrustI .await } -fn print_response(response: Response) -> anyhow::Result<()> { - match response { - Connect(response) => { - let pretty_json = serde_json::to_string_pretty(&ConnectResponseDto::from(response)) - .context("connect response JSON serialization")?; - println!("{pretty_json}"); - } - AnnounceIpv4(response) => { - let pretty_json = serde_json::to_string_pretty(&AnnounceResponseDto::from(response)) - .context("announce IPv4 response JSON serialization")?; - println!("{pretty_json}"); - } - AnnounceIpv6(response) => { - let pretty_json = serde_json::to_string_pretty(&AnnounceResponseDto::from(response)) - .context("announce IPv6 response JSON serialization")?; - println!("{pretty_json}"); - } - Scrape(response) => { - let pretty_json = - serde_json::to_string_pretty(&ScrapeResponseDto::from(response)).context("scrape response JSON serialization")?; - println!("{pretty_json}"); - } - Error(response) => { - let pretty_json = - serde_json::to_string_pretty(&ErrorResponseDto::from(response)).context("error response JSON serialization")?; - println!("{pretty_json}"); - } - }; - - Ok(()) -} - fn parse_socket_addr(tracker_socket_addr_str: &str) -> anyhow::Result { debug!("Tracker socket address: {tracker_socket_addr_str:#?}");