Skip to content

Commit

Permalink
refactor: [torrust#670] new print_response function from trait implem…
Browse files Browse the repository at this point in the history
…ented
  • Loading branch information
mario-nt authored and da2ce7 committed Jun 20, 2024
1 parent cb6742c commit 58516c5
Showing 1 changed file with 6 additions and 36 deletions.
42 changes: 6 additions & 36 deletions src/console/clients/udp/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,17 @@ use std::num::NonZeroU16;
use std::time::Duration;

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 torrust_tracker_primitives::info_hash::InfoHash;
use tracing::Level;

use crate::console::clients::udp::checker;
use crate::console::clients::udp::responses::{AnnounceResponseDto, ConnectResponseDto, ErrorResponseDto, ScrapeResponseDto};

use crate::console::clients::{parse_info_hash, parse_socket_addr, DEFAULT_TIMEOUT_SEC};

use super::responses::{DtoToJson as _, ResponseDto};

const RANDOM_TRANSACTION_ID: i32 = -888_840_697;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -116,7 +117,8 @@ pub async fn run() -> anyhow::Result<()> {
Command::Scrape { info_hashes } => handle_scrape(&args.addr, &timeout, &info_hashes).await?,
};

print_response(response)
let response_dto: ResponseDto = response.into();
response_dto.print_response()
}

async fn handle_announce(addr: &SocketAddr, timeout: &Duration, info_hash: &InfoHash) -> anyhow::Result<Response> {
Expand Down Expand Up @@ -148,35 +150,3 @@ async fn handle_scrape(addr: &SocketAddr, timeout: &Duration, info_hashes: &[Inf
.await
.context("failed to handle scrape")
}

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(())
}

0 comments on commit 58516c5

Please sign in to comment.