1use std::collections::HashMap;
2
3use crate::body::Body;
4use crate::value::Value;
5
6#[derive(Debug, Clone)]
8pub struct Message {
9 pub headers: HashMap<String, Value>,
11 pub body: Body,
13}
14
15impl Default for Message {
16 fn default() -> Self {
17 Self {
18 headers: HashMap::new(),
19 body: Body::Empty,
20 }
21 }
22}
23
24impl Message {
25 pub fn new(body: impl Into<Body>) -> Self {
27 Self {
28 headers: HashMap::new(),
29 body: body.into(),
30 }
31 }
32
33 pub fn header(&self, key: &str) -> Option<&Value> {
35 self.headers.get(key)
36 }
37
38 pub fn set_header(&mut self, key: impl Into<String>, value: impl Into<Value>) {
40 self.headers.insert(key.into(), value.into());
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47
48 #[test]
49 fn test_message_default() {
50 let msg = Message::default();
51 assert!(msg.body.is_empty());
52 assert!(msg.headers.is_empty());
53 }
54
55 #[test]
56 fn test_message_new_with_body() {
57 let msg = Message::new("hello");
58 assert_eq!(msg.body.as_text(), Some("hello"));
59 }
60
61 #[test]
62 fn test_message_headers() {
63 let mut msg = Message::default();
64 msg.set_header("key", Value::String("value".into()));
65 assert_eq!(msg.header("key"), Some(&Value::String("value".into())));
66 assert_eq!(msg.header("missing"), None);
67 }
68}