diff --git a/src/console/clients/udp/app.rs b/src/console/clients/udp/app.rs index 1675fd9ed..b4d08b26d 100644 --- a/src/console/clients/udp/app.rs +++ b/src/console/clients/udp/app.rs @@ -67,7 +67,8 @@ use torrust_tracker_primitives::info_hash::InfoHash as TorrustInfoHash; use url::Url; use crate::console::clients::udp::checker; -use crate::console::clients::udp::responses::{DtoToJson, ResponseDto}; +use crate::console::clients::udp::responses::dto::ResponseDto; +use crate::console::clients::udp::responses::json::ToJson; const ASSIGNED_BY_OS: u16 = 0; const RANDOM_TRANSACTION_ID: i32 = -888_840_697; @@ -117,7 +118,11 @@ pub async fn run() -> anyhow::Result<()> { }; let response_dto: ResponseDto = response.into(); - response_dto.print_response() + let response_json = response_dto.to_json_string()?; + + print!("{response_json}"); + + Ok(()) } fn setup_logging(level: LevelFilter) { diff --git a/src/console/clients/udp/responses.rs b/src/console/clients/udp/responses/dto.rs similarity index 90% rename from src/console/clients/udp/responses.rs rename to src/console/clients/udp/responses/dto.rs index 83e4da506..989231061 100644 --- a/src/console/clients/udp/responses.rs +++ b/src/console/clients/udp/responses/dto.rs @@ -1,27 +1,10 @@ //! Aquatic responses are not serializable. These are the serializable wrappers. use std::net::{Ipv4Addr, Ipv6Addr}; -use anyhow::Context; use aquatic_udp_protocol::Response::{self}; use aquatic_udp_protocol::{AnnounceResponse, ConnectResponse, ErrorResponse, Ipv4AddrBytes, Ipv6AddrBytes, ScrapeResponse}; use serde::Serialize; -pub trait DtoToJson { - /// # Errors - /// - /// Will return an error if serialization fails. - /// - fn print_response(&self) -> anyhow::Result<()> - where - Self: Serialize, - { - let pretty_json = serde_json::to_string_pretty(self).context("response JSON serialization")?; - println!("{pretty_json}"); - - Ok(()) - } -} - #[derive(Serialize)] pub enum ResponseDto { Connect(ConnectResponseDto), @@ -43,8 +26,6 @@ impl From for ResponseDto { } } -impl DtoToJson for ResponseDto {} - #[derive(Serialize)] pub struct ConnectResponseDto { transaction_id: i32, diff --git a/src/console/clients/udp/responses/json.rs b/src/console/clients/udp/responses/json.rs new file mode 100644 index 000000000..1e25acac2 --- /dev/null +++ b/src/console/clients/udp/responses/json.rs @@ -0,0 +1,24 @@ +use anyhow::Context; +use serde::Serialize; + +use super::dto::ResponseDto; + +pub trait ToJson { + /// + /// Returns a string with the JSON serialized version of the response + /// + /// # Errors + /// + /// Will return an error if serialization fails. + /// + fn to_json_string(&self) -> anyhow::Result + where + Self: Serialize, + { + let pretty_json = serde_json::to_string_pretty(self).context("response JSON serialization")?; + + Ok(pretty_json) + } +} + +impl ToJson for ResponseDto {} diff --git a/src/console/clients/udp/responses/mod.rs b/src/console/clients/udp/responses/mod.rs new file mode 100644 index 000000000..e6d2e5e51 --- /dev/null +++ b/src/console/clients/udp/responses/mod.rs @@ -0,0 +1,2 @@ +pub mod dto; +pub mod json;