facet-atom 0.46.0

Atom Syndication Format (RFC 4287) types for facet-xml
Documentation
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Atom Syndication Format (RFC 4287) types for `facet-xml`.
//!
//! This crate provides strongly-typed Rust representations of Atom feed elements,
//! enabling parsing and generation of Atom feeds using `facet-xml`.
//!
//! # Example
//!
//! ```rust
//! use facet_atom::{Feed, Entry, Person, Link, TextContent, TextType};
//!
//! let atom_xml = r#"<?xml version="1.0" encoding="utf-8"?>
//! <feed xmlns="http://www.w3.org/2005/Atom">
//!     <title>Example Feed</title>
//!     <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
//!     <updated>2003-12-13T18:30:02Z</updated>
//!     <author>
//!         <name>John Doe</name>
//!     </author>
//!     <link href="http://example.org/"/>
//!     <entry>
//!         <title>Atom-Powered Robots Run Amok</title>
//!         <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
//!         <updated>2003-12-13T18:30:02Z</updated>
//!         <link href="http://example.org/2003/12/13/atom03"/>
//!         <summary>Some text.</summary>
//!     </entry>
//! </feed>"#;
//!
//! let feed: Feed = facet_atom::from_str(atom_xml).unwrap();
//! assert_eq!(feed.title.as_ref().unwrap().content.as_deref(), Some("Example Feed"));
//! assert_eq!(feed.entries.len(), 1);
//! ```
//!
//! # Atom Namespace
//!
//! All types use the Atom namespace `http://www.w3.org/2005/Atom` as specified in RFC 4287.

use facet::Facet;
use facet_xml as xml;

pub const ATOM_NS: &str = "http://www.w3.org/2005/Atom";

/// Error type for Atom parsing
pub type Error = facet_xml::DeserializeError<facet_xml::XmlError>;

/// Error type for Atom serialization
pub type SerializeError = facet_xml::SerializeError<facet_xml::XmlSerializeError>;

/// Deserialize an Atom document from a string.
pub fn from_str<'input, T>(input: &'input str) -> Result<T, Error>
where
    T: Facet<'input>,
{
    facet_xml::from_str_borrowed(input)
}

/// Deserialize an Atom document from bytes.
pub fn from_slice<'input, T>(input: &'input [u8]) -> Result<T, Error>
where
    T: Facet<'input>,
{
    facet_xml::from_slice_borrowed(input)
}

/// Serialize an Atom value to a string.
pub fn to_string<'facet, T>(value: &T) -> Result<String, SerializeError>
where
    T: Facet<'facet> + ?Sized,
{
    facet_xml::to_string(value)
}

// =============================================================================
// Container Elements
// =============================================================================

/// The top-level Atom feed document (`<feed>`).
///
/// A feed contains metadata about the feed itself and zero or more entries.
///
/// # Required Elements (per RFC 4287)
/// - `id`: Permanent, universally unique identifier
/// - `title`: Human-readable title
/// - `updated`: Most recent modification time
///
/// # Optional Elements
/// - `author`: One or more feed authors (required if entries lack authors)
/// - `link`: Links to related resources
/// - `category`: Categories for the feed
/// - `contributor`: Contributors to the feed
/// - `generator`: Software that generated the feed
/// - `icon`: Small image for the feed (1:1 aspect ratio)
/// - `logo`: Larger image for the feed (2:1 aspect ratio)
/// - `rights`: Copyright/usage rights
/// - `subtitle`: Human-readable description
/// - `entry`: Individual content entries
#[derive(Facet, Debug, Clone, Default)]
#[facet(
    xml::ns_all = "http://www.w3.org/2005/Atom",
    rename = "feed",
    skip_all_unless_truthy
)]
pub struct Feed {
    /// Permanent, universally unique identifier for the feed.
    /// Must be an IRI (Internationalized Resource Identifier).
    #[facet(xml::element)]
    pub id: Option<String>,

    /// Human-readable title for the feed.
    #[facet(xml::element)]
    pub title: Option<TextContent>,

    /// Most recent time the feed was modified in a significant way.
    /// Format: RFC 3339 timestamp (e.g., "2003-12-13T18:30:02Z")
    #[facet(xml::element)]
    pub updated: Option<String>,

    /// Authors of the feed.
    #[facet(xml::elements, rename = "author")]
    pub authors: Vec<Person>,

    /// Links to related resources.
    #[facet(xml::elements, rename = "link")]
    pub links: Vec<Link>,

    /// Categories that the feed belongs to.
    #[facet(xml::elements, rename = "category")]
    pub categories: Vec<Category>,

    /// Contributors to the feed.
    #[facet(xml::elements, rename = "contributor")]
    pub contributors: Vec<Person>,

    /// Software agent used to generate the feed.
    #[facet(xml::element)]
    pub generator: Option<Generator>,

    /// IRI reference to a small image (favicon-style, 1:1 aspect ratio).
    #[facet(xml::element)]
    pub icon: Option<String>,

    /// IRI reference to a larger image (banner-style, 2:1 aspect ratio).
    #[facet(xml::element)]
    pub logo: Option<String>,

    /// Copyright/usage rights information.
    #[facet(xml::element)]
    pub rights: Option<TextContent>,

    /// Human-readable description or subtitle.
    #[facet(xml::element)]
    pub subtitle: Option<TextContent>,

    /// Individual entries in the feed.
    #[facet(xml::elements, rename = "entry")]
    pub entries: Vec<Entry>,
}

/// An individual entry in an Atom feed (`<entry>`).
///
/// # Required Elements (per RFC 4287)
/// - `id`: Permanent, universally unique identifier
/// - `title`: Human-readable title
/// - `updated`: Most recent modification time
///
/// # Conditionally Required
/// - `author`: Required unless the feed or source provides one
/// - `link` with `rel="alternate"`: Required if no `content` element
/// - `summary`: Required if content has `src` attribute or is non-text
#[derive(Facet, Debug, Clone, Default)]
#[facet(
    xml::ns_all = "http://www.w3.org/2005/Atom",
    rename = "entry",
    skip_all_unless_truthy
)]
pub struct Entry {
    /// Permanent, universally unique identifier for the entry.
    #[facet(xml::element)]
    pub id: Option<String>,

    /// Human-readable title for the entry.
    #[facet(xml::element)]
    pub title: Option<TextContent>,

    /// Most recent time the entry was modified in a significant way.
    #[facet(xml::element)]
    pub updated: Option<String>,

    /// Authors of the entry.
    #[facet(xml::elements, rename = "author")]
    pub authors: Vec<Person>,

    /// Links to related resources.
    #[facet(xml::elements, rename = "link")]
    pub links: Vec<Link>,

    /// Categories that the entry belongs to.
    #[facet(xml::elements, rename = "category")]
    pub categories: Vec<Category>,

    /// Contributors to the entry.
    #[facet(xml::elements, rename = "contributor")]
    pub contributors: Vec<Person>,

    /// The entry content.
    #[facet(xml::element)]
    pub content: Option<Content>,

    /// Time when the entry was first created or published.
    #[facet(xml::element)]
    pub published: Option<String>,

    /// Copyright/usage rights information.
    #[facet(xml::element)]
    pub rights: Option<TextContent>,

    /// Brief summary or excerpt of the entry.
    #[facet(xml::element)]
    pub summary: Option<TextContent>,

    /// Metadata from the original feed if this entry was copied.
    #[facet(xml::element)]
    pub source: Option<Source>,
}

/// Metadata about the original feed when an entry is copied (`<source>`).
///
/// Contains a subset of feed metadata to preserve attribution
/// when entries are aggregated from multiple sources.
#[derive(Facet, Debug, Clone, Default)]
#[facet(
    xml::ns_all = "http://www.w3.org/2005/Atom",
    rename = "source",
    skip_all_unless_truthy
)]
pub struct Source {
    /// Identifier of the original feed.
    #[facet(xml::element)]
    pub id: Option<String>,

    /// Title of the original feed.
    #[facet(xml::element)]
    pub title: Option<TextContent>,

    /// Last update time of the original feed.
    #[facet(xml::element)]
    pub updated: Option<String>,

    /// Authors of the original feed.
    #[facet(xml::elements, rename = "author")]
    pub authors: Vec<Person>,

    /// Links from the original feed.
    #[facet(xml::elements, rename = "link")]
    pub links: Vec<Link>,

    /// Categories from the original feed.
    #[facet(xml::elements, rename = "category")]
    pub categories: Vec<Category>,

    /// Contributors from the original feed.
    #[facet(xml::elements, rename = "contributor")]
    pub contributors: Vec<Person>,

    /// Generator of the original feed.
    #[facet(xml::element)]
    pub generator: Option<Generator>,

    /// Icon from the original feed.
    #[facet(xml::element)]
    pub icon: Option<String>,

    /// Logo from the original feed.
    #[facet(xml::element)]
    pub logo: Option<String>,

    /// Rights from the original feed.
    #[facet(xml::element)]
    pub rights: Option<TextContent>,

    /// Subtitle from the original feed.
    #[facet(xml::element)]
    pub subtitle: Option<TextContent>,
}

// =============================================================================
// Person Construct
// =============================================================================

/// A person (author or contributor) in an Atom feed.
///
/// Used for both `<author>` and `<contributor>` elements.
#[derive(Facet, Debug, Clone, Default)]
#[facet(xml::ns_all = "http://www.w3.org/2005/Atom", skip_all_unless_truthy)]
pub struct Person {
    /// Human-readable name for the person (required).
    #[facet(xml::element)]
    pub name: Option<String>,

    /// IRI associated with the person (e.g., homepage).
    #[facet(xml::element)]
    pub uri: Option<String>,

    /// Email address for the person (RFC 2822 format).
    #[facet(xml::element)]
    pub email: Option<String>,
}

// =============================================================================
// Text Construct
// =============================================================================

/// Content type for text constructs.
#[derive(Facet, Debug, Clone, Copy, Default, PartialEq, Eq)]
#[facet(rename_all = "lowercase")]
#[repr(u8)]
pub enum TextType {
    /// Plain text (default). Content should be displayed as-is.
    #[default]
    Text,
    /// HTML content. Markup should be escaped in the XML.
    Html,
    /// XHTML content. Markup is embedded as child elements.
    Xhtml,
}

/// A text construct used for title, subtitle, summary, and rights.
///
/// Per RFC 4287, text constructs can contain:
/// - Plain text (`type="text"`, default)
/// - Escaped HTML (`type="html"`)
/// - Inline XHTML (`type="xhtml"`)
#[derive(Facet, Debug, Clone, Default)]
#[facet(xml::ns_all = "http://www.w3.org/2005/Atom", skip_all_unless_truthy)]
pub struct TextContent {
    /// The content type. Defaults to "text" if not specified.
    #[facet(xml::attribute, rename = "type")]
    pub content_type: Option<TextType>,

    /// The text content (for type="text" or type="html").
    /// For type="xhtml", the content is within a div element.
    #[facet(xml::text)]
    pub content: Option<String>,
}

// =============================================================================
// Link Element
// =============================================================================

/// A link to a related resource (`<link>`).
///
/// Links define relationships between the feed/entry and external resources.
#[derive(Facet, Debug, Clone, Default)]
#[facet(xml::ns_all = "http://www.w3.org/2005/Atom", skip_all_unless_truthy)]
pub struct Link {
    /// The URI of the referenced resource (required).
    #[facet(xml::attribute)]
    pub href: Option<String>,

    /// The link relation type.
    /// Common values: "alternate", "self", "enclosure", "related", "via"
    #[facet(xml::attribute)]
    pub rel: Option<String>,

    /// Advisory media type of the resource.
    #[facet(xml::attribute, rename = "type")]
    pub media_type: Option<String>,

    /// Language of the referenced resource (RFC 3066 tag).
    #[facet(xml::attribute)]
    pub hreflang: Option<String>,

    /// Human-readable description of the link.
    #[facet(xml::attribute)]
    pub title: Option<String>,

    /// Advisory length of the resource in bytes.
    #[facet(xml::attribute)]
    pub length: Option<u64>,
}

// =============================================================================
// Category Element
// =============================================================================

/// A category for the feed or entry (`<category>`).
#[derive(Facet, Debug, Clone, Default)]
#[facet(xml::ns_all = "http://www.w3.org/2005/Atom", skip_all_unless_truthy)]
pub struct Category {
    /// The category identifier (required).
    #[facet(xml::attribute)]
    pub term: Option<String>,

    /// IRI identifying the categorization scheme.
    #[facet(xml::attribute)]
    pub scheme: Option<String>,

    /// Human-readable label for display.
    #[facet(xml::attribute)]
    pub label: Option<String>,
}

// =============================================================================
// Generator Element
// =============================================================================

/// Information about the software that generated the feed (`<generator>`).
#[derive(Facet, Debug, Clone, Default)]
#[facet(xml::ns_all = "http://www.w3.org/2005/Atom", skip_all_unless_truthy)]
pub struct Generator {
    /// IRI reference to the generator's website.
    #[facet(xml::attribute)]
    pub uri: Option<String>,

    /// Version of the generating software.
    #[facet(xml::attribute)]
    pub version: Option<String>,

    /// Human-readable name of the generator.
    #[facet(xml::text)]
    pub name: Option<String>,
}

// =============================================================================
// Content Element
// =============================================================================

/// The content of an entry (`<content>`).
///
/// Content can be inline (text, HTML, XHTML, or other XML) or referenced
/// via a `src` attribute for external content.
#[derive(Facet, Debug, Clone, Default)]
#[facet(xml::ns_all = "http://www.w3.org/2005/Atom", skip_all_unless_truthy)]
pub struct Content {
    /// The content type. For inline content: "text", "html", "xhtml", or a MIME type.
    /// For external content: a MIME type hint.
    #[facet(xml::attribute, rename = "type")]
    pub content_type: Option<String>,

    /// IRI reference to external content. If present, the element should be empty.
    #[facet(xml::attribute)]
    pub src: Option<String>,

    /// The inline content (when `src` is not present).
    /// For non-XML MIME types, this is Base64-encoded.
    #[facet(xml::text)]
    pub body: Option<String>,
}

// Re-export XML utilities for convenience
pub use facet_xml;