Skip to content

Commit 5a529cc

Browse files
committed
refactor: [#670] new mod for responses logic and refactors to json serialization trait
1 parent 74f4cb0 commit 5a529cc

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

src/console/clients/udp/app.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ use torrust_tracker_primitives::info_hash::InfoHash as TorrustInfoHash;
6767
use url::Url;
6868

6969
use crate::console::clients::udp::checker;
70-
use crate::console::clients::udp::responses::{DtoToJson, ResponseDto};
70+
use crate::console::clients::udp::responses::dto::ResponseDto;
71+
use crate::console::clients::udp::responses::json::ToJson;
7172

7273
const ASSIGNED_BY_OS: u16 = 0;
7374
const RANDOM_TRANSACTION_ID: i32 = -888_840_697;
@@ -117,7 +118,11 @@ pub async fn run() -> anyhow::Result<()> {
117118
};
118119

119120
let response_dto: ResponseDto = response.into();
120-
response_dto.print_response()
121+
let response_json = response_dto.to_json_string()?;
122+
123+
print!("{response_json}");
124+
125+
Ok(())
121126
}
122127

123128
fn setup_logging(level: LevelFilter) {
Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
11
//! Aquatic responses are not serializable. These are the serializable wrappers.
22
use std::net::{Ipv4Addr, Ipv6Addr};
33

4-
use anyhow::Context;
54
use aquatic_udp_protocol::Response::{self};
65
use aquatic_udp_protocol::{AnnounceResponse, ConnectResponse, ErrorResponse, Ipv4AddrBytes, Ipv6AddrBytes, ScrapeResponse};
76
use serde::Serialize;
87

9-
pub trait DtoToJson {
10-
/// # Errors
11-
///
12-
/// Will return an error if serialization fails.
13-
///
14-
fn print_response(&self) -> anyhow::Result<()>
15-
where
16-
Self: Serialize,
17-
{
18-
let pretty_json = serde_json::to_string_pretty(self).context("response JSON serialization")?;
19-
println!("{pretty_json}");
20-
21-
Ok(())
22-
}
23-
}
24-
258
#[derive(Serialize)]
269
pub enum ResponseDto {
2710
Connect(ConnectResponseDto),
@@ -43,8 +26,6 @@ impl From<Response> for ResponseDto {
4326
}
4427
}
4528

46-
impl DtoToJson for ResponseDto {}
47-
4829
#[derive(Serialize)]
4930
pub struct ConnectResponseDto {
5031
transaction_id: i32,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use anyhow::Context;
2+
use serde::Serialize;
3+
4+
use super::dto::ResponseDto;
5+
6+
pub trait ToJson {
7+
///
8+
/// Returns a string with the JSON serialized version of the response
9+
///
10+
/// # Errors
11+
///
12+
/// Will return an error if serialization fails.
13+
///
14+
fn to_json_string(&self) -> anyhow::Result<String>
15+
where
16+
Self: Serialize,
17+
{
18+
let pretty_json = serde_json::to_string_pretty(self).context("response JSON serialization")?;
19+
20+
Ok(pretty_json)
21+
}
22+
}
23+
24+
impl ToJson for ResponseDto {}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod dto;
2+
pub mod json;

0 commit comments

Comments
 (0)