aquatic_http: use Cow<_> for FailureResponse.failure_reason

This commit is contained in:
Joakim Frostegård 2021-10-16 00:19:58 +02:00
parent 31b88a5e9d
commit 2e68155bf4
2 changed files with 5 additions and 6 deletions

View file

@ -233,9 +233,7 @@ pub fn handle_connection_read_event(
peer_addr: established.peer_addr, peer_addr: established.peer_addr,
}; };
let response = FailureResponse { let response = FailureResponse::new("Invalid request");
failure_reason: "invalid request".to_string(),
};
local_responses.push((meta, Response::Failure(response))); local_responses.push((meta, Response::Failure(response)));

View file

@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::io::Write; use std::io::Write;
use std::net::{Ipv4Addr, Ipv6Addr}; use std::net::{Ipv4Addr, Ipv6Addr};
@ -131,11 +132,11 @@ impl ScrapeResponse {
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FailureResponse { pub struct FailureResponse {
#[serde(rename = "failure reason")] #[serde(rename = "failure reason")]
pub failure_reason: String, pub failure_reason: Cow<'static, str>,
} }
impl FailureResponse { impl FailureResponse {
pub fn new(reason: &str) -> Self { pub fn new<S: Into<Cow<'static, str>>>(reason: S) -> Self {
Self { Self {
failure_reason: reason.into(), failure_reason: reason.into(),
} }
@ -248,7 +249,7 @@ impl quickcheck::Arbitrary for ScrapeResponse {
impl quickcheck::Arbitrary for FailureResponse { impl quickcheck::Arbitrary for FailureResponse {
fn arbitrary(g: &mut quickcheck::Gen) -> Self { fn arbitrary(g: &mut quickcheck::Gen) -> Self {
Self { Self {
failure_reason: String::arbitrary(g), failure_reason: String::arbitrary(g).into(),
} }
} }
} }