Skip to main content

waveless_commons/http_executor/
response_cx.rs

1// Waveless
2// Copyright (C) 2026 Oscar Alvarez Gonzalez
3
4use crate::*;
5
6/// Client's HTTP response.
7#[derive(Getters, MutGetters, Default)]
8#[getset(get = "pub", get_mut = "pub")]
9pub struct ResponseCx {
10    status: StatusCode,
11    headers: HashMap<CompactString, CompactString>,
12    body: Option<BodyValue>,
13}
14
15impl ResponseCx {
16    pub fn new(headers: HashMap<CompactString, CompactString>, body: Option<BodyValue>) -> Self {
17        Self {
18            status: StatusCode::OK,
19            headers,
20            body,
21        }
22    }
23
24    // Cannot change the default name of `Constructor`.
25    pub fn new_with_status(
26        status: StatusCode,
27        headers: HashMap<CompactString, CompactString>,
28        body: Option<BodyValue>,
29    ) -> Self {
30        Self {
31            status,
32            headers,
33            body,
34        }
35    }
36}
37
38/// Defines all possible values a body could accept.
39/// NOTE: `BodyValue::Any` is serialized and stored in a byte array in the JSON field `data`.
40pub enum BodyValue {
41    Json(serde_json::Value),
42    Any(Box<dyn Encode<Output = Bytes> + Send + Sync>),
43}