Skip to main content

nanofish_client/
header.rs

1/// Common HTTP header names as constants for convenience
2pub mod headers {
3    /// Content-Type header
4    pub const CONTENT_TYPE: &str = "Content-Type";
5    /// Authorization header
6    pub const AUTHORIZATION: &str = "Authorization";
7    /// User-Agent header
8    pub const USER_AGENT: &str = "User-Agent";
9    /// Accept header
10    pub const ACCEPT: &str = "Accept";
11    /// Content-Length header (usually auto-generated)
12    pub const CONTENT_LENGTH: &str = "Content-Length";
13    /// Cache-Control header
14    pub const CACHE_CONTROL: &str = "Cache-Control";
15    /// Host header (auto-generated by the client)
16    pub const HOST: &str = "Host";
17    /// Connection header
18    pub const CONNECTION: &str = "Connection";
19    /// X-API-Key header for API authentication
20    pub const X_API_KEY: &str = "X-API-Key";
21    /// Accept-Encoding header
22    pub const ACCEPT_ENCODING: &str = "Accept-Encoding";
23}
24
25/// Common MIME types for Content-Type header values
26pub mod mime_types {
27    /// application/json
28    pub const JSON: &str = "application/json";
29    /// application/xml
30    pub const XML: &str = "application/xml";
31    /// text/plain
32    pub const TEXT: &str = "text/plain";
33    /// text/html
34    pub const HTML: &str = "text/html";
35    /// application/x-www-form-urlencoded
36    pub const FORM: &str = "application/x-www-form-urlencoded";
37    /// application/octet-stream
38    pub const BINARY: &str = "application/octet-stream";
39}
40
41/// HTTP Header struct for representing a single header with owned strings
42///
43/// This struct represents a single HTTP header with a name and value.
44/// Headers are used to pass additional information about the request or response.
45/// For `no_std` compatibility, we use `heapless::String` with fixed capacity.
46#[derive(Clone, Debug)]
47pub struct HttpHeader<'a> {
48    /// The name of the header (e.g., "Content-Type", "Authorization")
49    pub name: &'a str,
50    /// The value of the header (e.g., "application/json", "Bearer token123")
51    pub value: &'a str,
52}
53
54impl<'a> HttpHeader<'a> {
55    /// Create a new HTTP header
56    #[must_use]
57    pub const fn new(name: &'a str, value: &'a str) -> Self {
58        Self { name, value }
59    }
60
61    /// Create a Content-Type header
62    #[must_use]
63    pub const fn content_type(value: &'a str) -> Self {
64        Self::new(headers::CONTENT_TYPE, value)
65    }
66
67    /// Create an Authorization header
68    #[must_use]
69    pub const fn authorization(value: &'a str) -> Self {
70        Self::new(headers::AUTHORIZATION, value)
71    }
72
73    /// Create a User-Agent header
74    #[must_use]
75    pub const fn user_agent(value: &'a str) -> Self {
76        Self::new(headers::USER_AGENT, value)
77    }
78
79    /// Create an Accept header
80    #[must_use]
81    pub const fn accept(value: &'a str) -> Self {
82        Self::new(headers::ACCEPT, value)
83    }
84
85    /// Create an X-API-Key header
86    #[must_use]
87    pub const fn api_key(value: &'a str) -> Self {
88        Self::new(headers::X_API_KEY, value)
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    #[test]
97    fn test_http_header_creation() {
98        let header = HttpHeader {
99            name: "Content-Type",
100            value: "application/json",
101        };
102        assert_eq!(header.name, "Content-Type");
103        assert_eq!(header.value, "application/json");
104    }
105}