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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Link {
#[serde(skip_serializing_if = "Option::is_none")]
class: Option<Vec<String>>,
href: String,
rel: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", rename = "type")]
typ: Option<String>,
}
impl Link {
pub fn builder(rel: Vec<impl Into<String>>, href: impl Into<String>) -> LinkBuilder {
LinkBuilder {
class: None,
href: href.into(),
rel: rel.into_iter().map(|s| s.into()).collect(),
title: None,
typ: None,
}
}
/// Returns a reference to the Link's Classes.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let link: Link = Link::builder(vec!["self"], "http://api.x.io/orders/42")
/// .classes(vec!["item"]).into();
///
/// assert_eq!(&Some(vec!["item".to_string()]), link.classes());
/// ```
pub fn classes(&self) -> &Option<Vec<String>> {
&self.class
}
/// Returns a reference to the Link's title.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let link: Link = Link::builder(vec!["self"], "http://api.x.io/orders/42")
/// .title("Link").into();
///
/// assert_eq!(&Some("Link".to_string()), link.title());
/// ```
pub fn title(&self) -> &Option<String> {
&self.title
}
/// Returns a reference to the Link's type.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let link: Link = Link::builder(vec!["self"], "http://api.x.io/orders/42")
/// .typ("application/x-www-form-urlencoded")
/// .into();
///
/// assert_eq!(&Some("application/x-www-form-urlencoded".to_string()), link.typ());
/// ```
pub fn typ(&self) -> &Option<String> {
&self.typ
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct LinkBuilder {
class: Option<Vec<String>>,
href: String,
rel: Vec<String>,
title: Option<String>,
typ: Option<String>,
}
impl LinkBuilder {
/// Add a Class to the Link.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let link: Link = Link::builder(vec!["self"], "http://api.x.io/orders/42")
/// .class("item").into();
///
/// assert_eq!(&Some(vec!["item".to_string()]), link.classes());
/// ```
pub fn class(self, class: impl Into<String>) -> Self {
self.classes(vec![class])
}
/// Add a vector of Classes to the Link.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let link: Link = Link::builder(vec!["self"], "http://api.x.io/orders/42")
/// .classes(vec!["item"]).into();
///
/// assert_eq!(&Some(vec!["item".to_string()]), link.classes());
/// ```
pub fn classes(mut self, classes: Vec<impl Into<String>>) -> Self {
if let Some(ref mut s_class) = self.class {
for class in classes.into_iter() {
s_class.push(class.into());
}
} else {
let mut s_class = Vec::new();
for class in classes.into_iter() {
s_class.push(class.into());
}
self.class = Some(s_class)
}
self
}
/// Set the title of the Link.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let link: Link = Link::builder(vec!["self"], "http://api.x.io/orders/42")
/// .title("Link").into();
///
/// assert_eq!(&Some("Link".to_string()), link.title());
/// ```
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
/// Set the Link's type.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let link: Link = Link::builder(vec!["self"], "http://api.x.io/orders/42")
/// .typ("application/x-www-form-urlencoded")
/// .into();
///
/// assert_eq!(&Some("application/x-www-form-urlencoded".to_string()), link.typ());
/// ```
pub fn typ(mut self, typ: impl Into<String>) -> Self {
self.typ = Some(typ.into());
self
}
}
impl From<LinkBuilder> for Link {
fn from(builder: LinkBuilder) -> Link {
Link {
class: builder.class,
href: builder.href,
rel: builder.rel,
title: builder.title,
typ: builder.typ,
}
}
}