1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::collections::BTreeMap;

use serde::Serialize;
use serde_json::Value;

use crate::{utils::single_multiple::SingleOrMultiple, Link, Template};

/// Representation of a HAL document.
#[derive(Debug, Serialize)]
pub struct Hal {
    #[serde(rename = "_links")]
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub links: BTreeMap<String, SingleOrMultiple<Link>>,

    #[serde(rename = "_embedded")]
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub embedded: BTreeMap<String, SingleOrMultiple<Hal>>,

    #[serde(rename = "_templates")]
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub templates: BTreeMap<String, Template>,

    #[serde(flatten)]
    pub payload: Value,
}

impl Hal {
    /// Create a new HAL document for the given payload value.
    ///
    /// # Panics
    /// This will panic if the value provided can not be serialized into JSON for some reason.
    #[must_use]
    pub fn new<V>(value: V) -> Self
    where
        V: Serialize,
    {
        let payload = serde_json::to_value(value).unwrap();

        Self {
            payload,
            links: BTreeMap::new(),
            embedded: BTreeMap::new(),
            templates: BTreeMap::new(),
        }
    }

    /// Add a new link to a HAL document.
    #[must_use]
    pub fn with_link<N, L>(mut self, name: N, link: L) -> Self
    where
        N: ToString,
        L: Into<Link>,
    {
        let name = name.to_string();
        let link = link.into();

        let links = match self.links.remove(&name) {
            None => SingleOrMultiple::Single(link),
            Some(links) => links.insert(link),
        };

        self.links.insert(name, links);

        self
    }

    /// Add a new link to a HAL document.
    #[must_use]
    pub fn maybe_with_link<N, L>(self, name: N, link: Option<L>) -> Self
    where
        N: ToString,
        L: Into<Link>,
    {
        if let Some(link) = link {
            self.with_link(name, link)
        } else {
            self
        }
    }

    /// Add a new embedded HAL document to the HAL document.
    #[must_use]
    pub fn with_embedded<N, H>(mut self, name: N, value: H) -> Self
    where
        N: ToString,
        H: Into<Hal>,
    {
        let name = name.to_string();
        let value = value.into();

        let embedded = match self.embedded.remove(&name) {
            None => SingleOrMultiple::Single(value),
            Some(links) => links.insert(value),
        };

        self.embedded.insert(name, embedded);

        self
    }

    /// Add a new embedded HAL document to a HAL document.
    #[must_use]
    pub fn maybe_with_embedded<N, L>(self, name: N, embedded: Option<L>) -> Self
    where
        N: ToString,
        L: Into<Hal>,
    {
        if let Some(embedded) = embedded {
            self.with_embedded(name, embedded)
        } else {
            self
        }
    }

    /// Add a new action template to the HAL-FORMS document.
    #[must_use]
    pub fn with_template<N, T>(mut self, name: N, value: T) -> Self
    where
        N: ToString,
        T: Into<Template>,
    {
        self.templates.insert(name.to_string(), value.into());

        self
    }

    /// Add a new action template to the HAL-FORMS document.
    #[must_use]
    pub fn maybe_with_template<N, T>(self, name: N, template: Option<T>) -> Self
    where
        N: ToString,
        T: Into<Template>,
    {
        if let Some(template) = template {
            self.with_template(name, template)
        } else {
            self
        }
    }
}