forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: [torrust#670] new mod for responses logic and refactors to …
…json serialization trait
- Loading branch information
Showing
4 changed files
with
33 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod dto; | ||
pub mod json; |