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 committed Jun 3, 2024
1 parent 74f4cb0 commit 5a529cc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
9 changes: 7 additions & 2 deletions src/console/clients/udp/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
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 5a529cc

Please sign in to comment.