http_halforms/hal/
link.rs

1mod link_hints;
2
3pub use link_hints::*;
4use serde::Serialize;
5
6/// Representation of a single Link in a HAL document.
7#[derive(Debug, Serialize)]
8pub struct Link {
9    pub href:        String,
10    #[serde(skip_serializing_if = "is_false")]
11    pub templated:   bool,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub r#type:      Option<String>,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub deprecation: Option<String>,
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub name:        Option<String>,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub profile:     Option<String>,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub title:       Option<String>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub hreflang:    Option<String>,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub hints:       Option<LinkHints>,
26}
27
28impl Link {
29    /// Create a new Link with the given href.
30    #[must_use]
31    pub fn new<H>(href: H) -> Self
32    where
33        H: ToString,
34    {
35        Self {
36            href:        href.to_string(),
37            templated:   false,
38            r#type:      None,
39            deprecation: None,
40            name:        None,
41            profile:     None,
42            title:       None,
43            hreflang:    None,
44            hints:       None,
45        }
46    }
47
48    /// Indicate that the link is templated.
49    #[must_use]
50    pub fn templated(mut self) -> Self {
51        self.templated = true;
52
53        self
54    }
55
56    /// Specify the type value on the link.
57    #[must_use]
58    pub fn with_type<S>(mut self, value: S) -> Self
59    where
60        S: ToString,
61    {
62        self.r#type = Some(value.to_string());
63
64        self
65    }
66
67    /// Specify the deprecation value on the link.
68    #[must_use]
69    pub fn with_deprecation<S>(mut self, value: S) -> Self
70    where
71        S: ToString,
72    {
73        self.deprecation = Some(value.to_string());
74
75        self
76    }
77
78    /// Specify the name value on the link.
79    #[must_use]
80    pub fn with_name<S>(mut self, value: S) -> Self
81    where
82        S: ToString,
83    {
84        self.name = Some(value.to_string());
85
86        self
87    }
88
89    /// Specify the profile value on the link.
90    #[must_use]
91    pub fn with_profile<S>(mut self, value: S) -> Self
92    where
93        S: ToString,
94    {
95        self.profile = Some(value.to_string());
96
97        self
98    }
99
100    /// Specify the title value on the link.
101    #[must_use]
102    pub fn with_title<S>(mut self, value: S) -> Self
103    where
104        S: ToString,
105    {
106        self.title = Some(value.to_string());
107
108        self
109    }
110
111    /// Specify the hreflang value on the link.
112    #[must_use]
113    pub fn with_hreflang<S>(mut self, value: S) -> Self
114    where
115        S: ToString,
116    {
117        self.hreflang = Some(value.to_string());
118
119        self
120    }
121
122    /// Specify the hreflang value on the link.
123    #[must_use]
124    pub fn with_hints<V>(mut self, value: V) -> Self
125    where
126        V: Into<LinkHints>,
127    {
128        self.hints = Some(value.into());
129
130        self
131    }
132}
133
134impl<S> From<S> for Link
135where
136    S: ToString,
137{
138    fn from(href: S) -> Self {
139        Self::new(href.to_string())
140    }
141}
142
143#[allow(clippy::trivially_copy_pass_by_ref)] // Needed for Serde.
144fn is_false(t: &bool) -> bool {
145    t == &false
146}