Skip to content

Commit

Permalink
refactor: [torrust#670] new mod for responses logic and refactors to …
Browse files Browse the repository at this point in the history
…json serialization trait
  • Loading branch information
mario-nt authored and da2ce7 committed Jun 20, 2024
1 parent 3c84732 commit 19a3c5e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
11 changes: 7 additions & 4 deletions src/console/clients/udp/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,10 @@ use torrust_tracker_primitives::info_hash::InfoHash;
use tracing::Level;

use crate::console::clients::udp::checker;

use crate::console::clients::udp::responses::dto::ResponseDto;
use crate::console::clients::udp::responses::json::ToJson;
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 @@ -118,7 +117,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(())
}

async fn handle_announce(addr: &SocketAddr, timeout: &Duration, info_hash: &InfoHash) -> anyhow::Result<Response> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -43,8 +26,6 @@ impl From<Response> for ResponseDto {
}
}

impl DtoToJson for ResponseDto {}

#[derive(Serialize)]
pub struct ConnectResponseDto {
transaction_id: i32,
Expand Down
24 changes: 24 additions & 0 deletions src/console/clients/udp/responses/json.rs
Original file line number Diff line number Diff line change
@@ -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<String>
where
Self: Serialize,
{
let pretty_json = serde_json::to_string_pretty(self).context("response JSON serialization")?;

Ok(pretty_json)
}
}

impl ToJson for ResponseDto {}
2 changes: 2 additions & 0 deletions src/console/clients/udp/responses/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod dto;
pub mod json;

0 comments on commit 19a3c5e

Please sign in to comment.