Skip to main content

notifica_rust_sdk/payload/
request.rs

1use std::collections::HashMap;
2
3/// Key-value payload used for both `request` and `automation` channels.
4///
5/// The Notifica service forwards this map to a configured upstream URL.
6///
7/// # Example
8/// ```rust
9/// use notifica_crate::payload::request::RequestPayload;
10///
11/// let payload = RequestPayload::new()
12///     .field("user_id", "42")
13///     .field("action", "signup");
14/// ```
15#[derive(Debug, Clone, Default)]
16pub struct RequestPayload(pub HashMap<String, String>);
17
18impl RequestPayload {
19    /// Create an empty payload.
20    pub fn new() -> Self {
21        Self::default()
22    }
23
24    /// Add a single key-value field.
25    pub fn field(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
26        self.0.insert(key.into(), value.into());
27        self
28    }
29}
30
31impl From<HashMap<String, String>> for RequestPayload {
32    fn from(map: HashMap<String, String>) -> Self {
33        Self(map)
34    }
35}
36
37impl From<RequestPayload> for HashMap<String, String> {
38    fn from(p: RequestPayload) -> Self {
39        p.0
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn field_builder_inserts() {
49        let p = RequestPayload::new().field("a", "1").field("b", "2");
50        assert_eq!(p.0["a"], "1");
51        assert_eq!(p.0["b"], "2");
52    }
53
54    #[test]
55    fn from_hashmap_roundtrips() {
56        let mut map = HashMap::new();
57        map.insert("x".into(), "y".into());
58        let p = RequestPayload::from(map.clone());
59        let out: HashMap<String, String> = p.into();
60        assert_eq!(out, map);
61    }
62}