atom_syndication/content.rs
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
use std::borrow::Cow;
use std::io::{BufRead, Write};
use quick_xml::events::attributes::Attributes;
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use quick_xml::Reader;
use quick_xml::Writer;
use crate::error::{Error, XmlError};
use crate::fromxml::FromXml;
use crate::toxml::ToXml;
use crate::util::{atom_text, atom_xhtml, attr_value, decode};
/// Represents the content of an Atom entry
//
/// ## Attention
///
/// Atom format specification [RFC4287](https://datatracker.ietf.org/doc/html/rfc4287#section-4.1.3.2)
/// states that `src` and `value` (content) fields are mutually exclusive:
///
/// > atom:content MAY have a "src" attribute, whose value MUST be an IRI reference.
/// > If the "src" attribute is present, atom:content MUST be empty.
///
/// Setting of both fields when authoring an Atom feed is still technically possible,
/// but it will lead to a non-compliant result.
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug, Default, Clone, PartialEq)]
#[cfg_attr(feature = "builders", derive(Builder))]
#[cfg_attr(
feature = "builders",
builder(
setter(into),
default,
build_fn(name = "build_impl", private, error = "never::Never")
)
)]
pub struct Content {
/// Base URL for resolving any relative references found in the element.
pub base: Option<String>,
/// Indicates the natural language for the element.
pub lang: Option<String>,
/// The text value of the content.
pub value: Option<String>,
/// The URI of where the content can be found.
pub src: Option<String>,
/// Either "text", "html", "xhtml", or the MIME type of the content.
pub content_type: Option<String>,
}
impl Content {
/// Return base URL of the content.
pub fn base(&self) -> Option<&str> {
self.base.as_deref()
}
/// Set base URL of the content.
pub fn set_base<V>(&mut self, base: V)
where
V: Into<Option<String>>,
{
self.base = base.into();
}
/// Return natural language of the content.
pub fn lang(&self) -> Option<&str> {
self.lang.as_deref()
}
/// Set the base URL of the content.
pub fn set_lang<V>(&mut self, lang: V)
where
V: Into<Option<String>>,
{
self.lang = lang.into();
}
/// Return the text value of the content.
///
/// If the `content_type` is neither `"text"`, `"html"`, or `"xhtml"` then the value should
/// be a base64 encoded document of the indicated MIME type.
///
/// # Examples
///
/// ```
/// use atom_syndication::Content;
///
/// let mut content = Content::default();
/// content.set_value("Example content".to_string());
/// assert_eq!(content.value(), Some("Example content"));
/// ```
pub fn value(&self) -> Option<&str> {
self.value.as_deref()
}
/// Set the text value of the content.
///
/// # Examples
///
/// ```
/// use atom_syndication::Content;
///
/// let mut content = Content::default();
/// content.set_value("Example content".to_string());
/// ```
pub fn set_value<V>(&mut self, value: V)
where
V: Into<Option<String>>,
{
self.value = value.into();
}
/// Return the URI where the content can be found.
///
/// # Examples
///
/// ```
/// use atom_syndication::Content;
///
/// let mut content = Content::default();
/// content.set_src("http://example.com/content.html".to_string());
/// assert_eq!(content.src(), Some("http://example.com/content.html"));
/// ```
pub fn src(&self) -> Option<&str> {
self.src.as_deref()
}
/// Set the URI where the content can be found.
///
/// # Examples
///
/// ```
/// use atom_syndication::Content;
///
/// let mut content = Content::default();
/// content.set_src("http://example.com/content.html".to_string());
/// ```
pub fn set_src<V>(&mut self, src: V)
where
V: Into<Option<String>>,
{
self.src = src.into();
}
/// Return the type of the content.
///
/// The type is either `"text"`, `"html"`, `"xhtml"`, or the MIME type of the content.
///
/// # Examples
///
/// ```
/// use atom_syndication::Content;
///
/// let mut content = Content::default();
/// content.set_content_type("image/png".to_string());
/// assert_eq!(content.content_type(), Some("image/png"));
/// ```
pub fn content_type(&self) -> Option<&str> {
self.content_type.as_deref()
}
/// Set the type of the content.
///
/// # Examples
///
/// ```
/// use atom_syndication::Content;
///
/// let mut content = Content::default();
/// content.set_content_type("image/png".to_string());
/// assert_eq!(content.content_type(), Some("image/png"));
/// ```
pub fn set_content_type<V>(&mut self, content_type: V)
where
V: Into<Option<String>>,
{
self.content_type = content_type.into();
}
}
impl FromXml for Content {
fn from_xml<B: BufRead>(
reader: &mut Reader<B>,
mut atts: Attributes<'_>,
) -> Result<Self, Error> {
let mut content = Content::default();
for att in atts.with_checks(false).flatten() {
match decode(att.key.as_ref(), reader)? {
Cow::Borrowed("xml:base") => {
content.base = Some(attr_value(&att, reader)?.to_string());
}
Cow::Borrowed("xml:lang") => {
content.lang = Some(attr_value(&att, reader)?.to_string());
}
Cow::Borrowed("type") => {
content.content_type = Some(attr_value(&att, reader)?.to_string());
}
Cow::Borrowed("src") => {
content.src = Some(attr_value(&att, reader)?.to_string());
}
_ => {}
}
}
content.value = match content.content_type {
Some(ref t) if t == "xhtml" => atom_xhtml(reader)?,
_ => atom_text(reader)?,
};
Ok(content)
}
}
impl ToXml for Content {
fn to_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), XmlError> {
let name = "content";
let mut element = BytesStart::new(name);
if let Some(ref base) = self.base {
element.push_attribute(("xml:base", base.as_str()));
}
if let Some(ref lang) = self.lang {
element.push_attribute(("xml:lang", lang.as_str()));
}
if let Some(ref content_type) = self.content_type {
if content_type == "xhtml" {
element.push_attribute(("type", "xhtml"));
} else {
element.push_attribute(("type", &**content_type));
}
}
if let Some(ref src) = self.src {
element.push_attribute(("src", &**src));
}
writer
.write_event(Event::Start(element))
.map_err(XmlError::new)?;
if let Some(ref value) = self.value {
writer
.write_event(Event::Text(
if self.content_type.as_deref() == Some("xhtml") {
BytesText::from_escaped(value)
} else {
BytesText::new(value)
},
))
.map_err(XmlError::new)?;
}
writer
.write_event(Event::End(BytesEnd::new(name)))
.map_err(XmlError::new)?;
Ok(())
}
}
#[cfg(feature = "builders")]
impl ContentBuilder {
/// Builds a new `Content`.
pub fn build(&self) -> Content {
self.build_impl().unwrap()
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::error::Error;
use crate::util::decode;
fn lines(text: &str) -> Vec<&str> {
text.lines()
.map(|line| line.trim())
.filter(|line| !line.is_empty())
.collect::<Vec<_>>()
}
fn to_xml(content: &Content) -> String {
let mut buffer = Vec::new();
let mut writer = Writer::new_with_indent(&mut buffer, b' ', 4);
content.to_xml(&mut writer).unwrap();
String::from_utf8(buffer).unwrap()
}
fn from_xml(xml: &str) -> Result<Content, Error> {
let mut reader = Reader::from_reader(xml.as_bytes());
reader.config_mut().expand_empty_elements = true;
loop {
let mut buf = Vec::new();
match reader.read_event_into(&mut buf).map_err(XmlError::new)? {
Event::Start(element) => {
if decode(element.name().as_ref(), &reader)? == "content" {
let content = Content::from_xml(&mut reader, element.attributes())?;
return Ok(content);
} else {
return Err(Error::InvalidStartTag);
}
}
Event::Eof => return Err(Error::Eof),
_ => {}
}
}
}
#[test]
fn test_plain_text() {
let content = Content {
value: Some("Text with ampersand & <tag>.".into()),
..Default::default()
};
let xml_fragment = r#"<content>Text with ampersand & <tag>.</content>"#;
assert_eq!(to_xml(&content), xml_fragment);
assert_eq!(from_xml(xml_fragment).unwrap(), content);
}
#[test]
fn test_html() {
let content = Content {
content_type: Some("html".into()),
value: Some("Markup with ampersand, <tag>, & </closing-tag>.".into()),
..Default::default()
};
let xml_fragment = r#"<content type="html">Markup with ampersand, <tag>, & </closing-tag>.</content>"#;
assert_eq!(to_xml(&content), xml_fragment);
assert_eq!(from_xml(xml_fragment).unwrap(), content);
}
#[test]
fn test_xhtml() {
let content = Content {
content_type: Some("xhtml".into()),
value: Some(r#"<div>a line<br/>& one more</div>"#.into()),
..Default::default()
};
let xml_fragment =
r#"<content type="xhtml"><div>a line<br/>& one more</div></content>"#;
assert_eq!(to_xml(&content), xml_fragment);
assert_eq!(from_xml(xml_fragment).unwrap(), content);
}
#[test]
fn test_write_image() {
let content = Content {
content_type: Some("image/png".into()),
src: Some("http://example.com/image.png".into()),
..Default::default()
};
assert_eq!(
lines(&to_xml(&content)),
lines(
r#"
<content type="image/png" src="http://example.com/image.png">
</content>
"#
)
);
}
}