forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
69 lines (62 loc) · 2.05 KB
/
Copy patherror.rs
File metadata and controls
69 lines (62 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! `Error` response for the [`HTTP tracker`](crate::servers::http).
//!
//! Data structures and logic to build the error responses.
//!
//! From the [BEP 03. The `BitTorrent` Protocol Specification](https://www.bittorrent.org/beps/bep_0003.html):
//!
//! _"Tracker responses are bencoded dictionaries. If a tracker response has a
//! key failure reason, then that maps to a human readable string which explains
//! why the query failed, and no other keys are required."_
//!
//! > **NOTICE**: error responses are bencoded and always have a `200 OK` status
//! code. The official `BitTorrent` specification does not specify the status
//! code.
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use serde::{self, Serialize};
/// `Error` response for the [`HTTP tracker`](crate::servers::http).
#[derive(Serialize, Debug, PartialEq)]
pub struct Error {
/// Human readable string which explains why the request failed.
#[serde(rename = "failure reason")]
pub failure_reason: String,
}
impl Error {
/// Returns the bencoded representation of the `Error` struct.
///
/// ```rust
/// use torrust_tracker::servers::http::v1::responses::error::Error;
///
/// let err = Error {
/// failure_reason: "error message".to_owned(),
/// };
///
/// // cspell:disable-next-line
/// assert_eq!(err.write(), "d14:failure reason13:error messagee");
/// ```
///
/// # Panics
///
/// It would panic if the `Error` struct contained an inappropriate field
/// type.
#[must_use]
pub fn write(&self) -> String {
serde_bencode::to_string(&self).unwrap()
}
}
impl IntoResponse for Error {
fn into_response(self) -> Response {
(StatusCode::OK, self.write()).into_response()
}
}
#[cfg(test)]
mod tests {
use super::Error;
#[test]
fn http_tracker_errors_can_be_bencoded() {
let err = Error {
failure_reason: "error message".to_owned(),
};
assert_eq!(err.write(), "d14:failure reason13:error messagee"); // cspell:disable-line
}
}