atom

Atom syndication format library

moon add marianoguerra/atom@0.1.1
Download zip
Version
0.1.1
License
Apache-2.0
Last updated
12 hours ago
Downloads
6

Dependencies

README

#marianoguerra/atom

An Atom 1.0 (RFC 4287) syndication library for MoonBit: model, parser, writer and validation, built on marianoguerra/xml.

///|
test {
let feed = try! Feed::read_from(
"<?xml version=\"1.0\"?><feed xmlns=\"http://www.w3.org/2005/Atom\"><id>urn:x</id><title>T</title><updated>2026-01-01T00:00:00Z</updated><entry><id>e1</id><title>E1</title><updated>2026-01-02T00:00:00Z</updated></entry></feed>",
)
inspect(feed.title.value, content="T")
assert_true(feed.entries.length() == 1)
// Re-serialising then parsing yields an equal model.
let again = try! Feed::read_from(feed.to_xml())
assert_true(again == feed)
}

Unknown elements are skipped while parsing, which keeps readers forward-compatible with RFC extensions. XHTML text constructs are captured verbatim minus their mandatory div wrapper.

#
AtomError

pub(all) suberror AtomError {
Xml(err~ :
XmlError
)
UnexpectedRoot(found~ : String)
MissingElement(path~ : String)
} derive(Eq,
Debug
)

Errors produced while reading or validating Atom documents.

#
Category

pub(all) struct Category {
term : String
scheme : String?
label : String?
} derive(Eq,
Debug
)

An Atom category, RFC 4287 §4.2.2.

#
Content

pub(all) enum Content {
Text(String)
Html(String)
Xhtml(String)
Src(src~ : String, media_type~ : String?)
} derive(Eq,
Debug
)

Entry content per RFC 4287 §4.1.3.

#
Entry

pub(all) struct Entry {
id : String
title : Text
updated : String
authors : Array[Person]
contributors : Array[Person]
categories : Array[Category]
links : Array[Link]
published : String?
rights : Text?
summary : Text?
content : Content?
source : Source?
} derive(Eq,
Debug
)

An Atom entry, RFC 4287 §4.1.2.

#
Entry::new

fn Entry::new(id~ : String, title~ : String, updated~ : String) -> Entry

Create an entry skeleton with the required elements filled and everything else empty.

#
Feed

pub(all) struct Feed {
id : String
title : Text
updated : String
authors : Array[Person]
contributors : Array[Person]
categories : Array[Category]
links : Array[Link]
generator : Generator?
icon : String?
logo : String?
rights : Text?
subtitle : Text?
entries : Array[Entry]
} derive(Eq,
Debug
)

A minimal Atom 1.0 feed modelled on RFC 4287.

#
Feed::new

fn Feed::new(id~ : String, title~ : String, updated~ : String) -> Feed

Create a feed skeleton with the required elements filled and everything else empty.

#
Feed::read_from

fn Feed::read_from(input : String) -> Feed raise AtomError

Parse an Atom 1.0 document into a [Feed].

The root element must be feed. Children are matched by local name; unprefixed children resolve against the feed's default namespace, and the conventional atom: prefix is always accepted. Unknown elements are skipped, which keeps the parser forward-compatible with extensions.

#
Feed::to_xml

fn Feed::to_xml(self : Feed) -> String

Serialise the feed to an XML string.

#
Generator

pub(all) struct Generator {
value : String?
uri : String?
version : String?
} derive(Eq,
Debug
)

The software agent that generated the feed.
pub(all) struct Link {
href : String
rel : String?
media_type : String?
hreflang : String?
title : String?
length : String?
} derive(Eq,
Debug
)

An Atom link, RFC 4287 §4.2.7.

#
Link::alternate

fn Link::alternate(href : String) -> Link

Construct an alternate link.

#
Link::enclosure

fn Link::enclosure(href~ : String, media_type~ : String, length~ : String) -> Link

Construct an enclosure-style link.

#
Person

pub(all) struct Person {
name : String
email : String?
uri : String?
} derive(Eq,
Debug
)

An Atom person construct, RFC 4287 §3.2.

#
Source

pub(all) struct Source {
id : String?
title : Text?
updated : String?
} derive(Eq,
Debug
)

Metadata about the original source feed of an entry.

#
Text

pub(all) struct Text {
typ : TextType
value : String
} derive(Eq,
Debug
)

A text construct (title, subtitle, rights, summary).

#
Text::html

fn Text::html(value : String) -> Text

Construct an HTML text construct.

#
Text::plain

fn Text::plain(value : String) -> Text

Construct a plain-text construct.

#
Text::xhtml

fn Text::xhtml(value : String) -> Text

Construct an XHTML text construct.

#
TextType

pub(all) enum TextType {
Text
Html
Xhtml
} derive(Eq,
Debug
)

How a Text construct is encoded.

#
ATOM_NAMESPACE

let ATOM_NAMESPACE : String

The Atom namespace URI.

#
hello

fn hello() -> String