2021-09-27 10:06:22 +00:00
|
|
|
//! This crate provides an API to parse and create [OPML documents].
|
2020-08-28 11:37:29 +00:00
|
|
|
//!
|
2021-09-27 10:06:22 +00:00
|
|
|
//! [OPML documents]: http://opml.org/spec2.opml
|
2020-08-28 11:37:29 +00:00
|
|
|
//!
|
2021-09-27 10:06:22 +00:00
|
|
|
//! ## Parsing
|
2020-08-28 11:37:29 +00:00
|
|
|
//!
|
2021-09-27 10:06:22 +00:00
|
|
|
//! To parse an OPML document use [`OPML::from_str`] or [`OPML::from_reader`].
|
|
|
|
//!
|
|
|
|
//! Parsing will result in an error if:
|
|
|
|
//! * the XML is malformed,
|
|
|
|
//! * the included OPML version is not supported (currently all OPML versions
|
|
|
|
//! (1.0, 1.1 and 2.0) are supported) or,
|
|
|
|
//! * if the [`Body`] element contains no child [`Outline`] elements,
|
|
|
|
//! [as per the spec].
|
|
|
|
//!
|
|
|
|
//! [as per the spec]: http://opml.org/spec2.opml#1629042198000
|
2020-08-28 11:37:29 +00:00
|
|
|
//!
|
|
|
|
//! ```rust
|
2021-09-27 11:49:08 +00:00
|
|
|
//! use opml::OPML;
|
2020-08-28 11:37:29 +00:00
|
|
|
//!
|
|
|
|
//! let xml = r#"<opml version="2.0"><head/><body><outline text="Outline"/></body></opml>"#;
|
2021-09-27 10:06:22 +00:00
|
|
|
//! let document = OPML::from_str(xml).unwrap();
|
2020-08-28 11:37:29 +00:00
|
|
|
//!
|
2021-09-27 10:06:22 +00:00
|
|
|
//! assert_eq!(document.version, "2.0");
|
2020-08-28 11:37:29 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
2021-09-27 10:06:22 +00:00
|
|
|
//! ## Creating
|
2020-08-28 11:37:29 +00:00
|
|
|
//!
|
2021-09-27 10:06:22 +00:00
|
|
|
//! To create an OPML document from scratch, use [`OPML::default()`] or the good
|
|
|
|
//! old `OPML { /* ... */ }` syntax.
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2022-07-19 11:47:46 +00:00
|
|
|
use hard_xml::{XmlRead, XmlWrite};
|
2022-07-20 12:14:44 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-03-22 11:41:11 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// All possible errors.
|
2021-03-22 11:41:11 +00:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum Error {
|
2021-09-27 10:06:22 +00:00
|
|
|
/// [From the spec], "a `<body>` contains one or more `<outline>` elements".
|
|
|
|
///
|
|
|
|
/// [From the spec]: http://opml.org/spec2.opml#1629042198000
|
2021-03-22 11:50:59 +00:00
|
|
|
#[error("OPML body has no <outline> elements")]
|
2021-03-22 11:41:11 +00:00
|
|
|
BodyHasNoOutlines,
|
2021-03-22 21:31:59 +00:00
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// Wrapper for [`std::io::Error`].
|
2021-03-22 21:31:59 +00:00
|
|
|
#[error("Failed to read file")]
|
|
|
|
IoError(#[from] std::io::Error),
|
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// The version string in the XML is not supported.
|
2021-03-22 11:41:11 +00:00
|
|
|
#[error("Unsupported OPML version: {0:?}")]
|
|
|
|
UnsupportedVersion(String),
|
2021-03-22 21:31:59 +00:00
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// The input string is not valid XML.
|
2021-03-22 11:41:11 +00:00
|
|
|
#[error("Failed to process XML file")]
|
2022-07-19 11:47:46 +00:00
|
|
|
XmlError(#[from] hard_xml::XmlError),
|
2021-03-22 11:41:11 +00:00
|
|
|
}
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// The top-level [`OPML`] element.
|
2021-03-22 11:41:11 +00:00
|
|
|
#[derive(
|
2022-09-30 23:00:34 +00:00
|
|
|
XmlWrite, XmlRead, PartialEq, Eq, Debug, Clone, Serialize, Deserialize,
|
2021-03-22 11:41:11 +00:00
|
|
|
)]
|
2020-08-28 11:37:29 +00:00
|
|
|
#[xml(tag = "opml")]
|
|
|
|
pub struct OPML {
|
2021-09-27 10:06:22 +00:00
|
|
|
/// The version attribute from the element, valid values are `1.0`, `1.1` and
|
|
|
|
/// `2.0`.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[xml(attr = "version")]
|
|
|
|
pub version: String,
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// The [`Head`] child element. Contains the metadata of the OPML document.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[xml(child = "head")]
|
|
|
|
pub head: Option<Head>,
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// The [`Body`] child element. Contains all the [`Outline`] elements.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[xml(child = "body")]
|
|
|
|
pub body: Body,
|
2020-08-28 11:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OPML {
|
2021-09-27 10:06:22 +00:00
|
|
|
/// Deprecated, use [`OPML::from_str`] instead.
|
|
|
|
#[deprecated(note = "Use from_str instead", since = "1.1.0")]
|
2021-03-22 11:41:11 +00:00
|
|
|
pub fn new(xml: &str) -> Result<Self, Error> {
|
2021-03-23 12:02:16 +00:00
|
|
|
Self::from_str(xml).map_err(Into::into)
|
2021-03-22 21:31:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses an OPML document.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use opml::{OPML, Outline};
|
|
|
|
///
|
|
|
|
/// let xml = r#"<opml version="2.0"><head/><body><outline text="Outline"/></body></opml>"#;
|
2021-09-27 10:06:22 +00:00
|
|
|
/// let document = OPML::from_str(xml).unwrap();
|
2021-03-22 21:31:59 +00:00
|
|
|
///
|
|
|
|
/// let mut expected = OPML::default();
|
|
|
|
/// expected.body.outlines.push(Outline {
|
|
|
|
/// text: "Outline".to_string(),
|
|
|
|
/// ..Outline::default()
|
|
|
|
/// });
|
|
|
|
///
|
2021-09-27 10:06:22 +00:00
|
|
|
/// assert_eq!(document, expected);
|
2021-03-22 21:31:59 +00:00
|
|
|
/// ```
|
2021-03-23 12:02:16 +00:00
|
|
|
#[allow(clippy::should_implement_trait)]
|
2021-03-22 21:31:59 +00:00
|
|
|
pub fn from_str(xml: &str) -> Result<Self, Error> {
|
|
|
|
let opml = <OPML as XmlRead>::from_str(xml)?;
|
2020-08-29 14:46:54 +00:00
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
// SPEC: The version attribute is a version string, of the form, x.y, where
|
|
|
|
// x and y are both numeric strings.
|
2020-08-29 14:46:54 +00:00
|
|
|
let valid_versions = vec!["1.0", "1.1", "2.0"];
|
|
|
|
|
2021-09-27 11:37:43 +00:00
|
|
|
if !valid_versions.contains(&opml.version.as_str()) {
|
2021-03-22 11:41:11 +00:00
|
|
|
return Err(Error::UnsupportedVersion(opml.version));
|
2020-08-28 11:37:29 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 14:46:54 +00:00
|
|
|
// SPEC: A `<body>` contains one or more `<outline>` elements.
|
|
|
|
if opml.body.outlines.is_empty() {
|
2021-03-22 11:41:11 +00:00
|
|
|
return Err(Error::BodyHasNoOutlines);
|
2020-08-28 11:37:29 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 14:46:54 +00:00
|
|
|
Ok(opml)
|
|
|
|
}
|
|
|
|
|
2021-03-22 21:31:59 +00:00
|
|
|
/// Parses an OPML document from a reader.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2021-03-23 12:02:16 +00:00
|
|
|
/// ```rust,no_run
|
2021-09-27 11:49:08 +00:00
|
|
|
/// use opml::OPML;
|
2021-03-23 12:02:16 +00:00
|
|
|
/// use std::fs::File;
|
2021-03-22 21:31:59 +00:00
|
|
|
///
|
2021-03-23 12:02:16 +00:00
|
|
|
/// let mut file = File::open("file.opml").unwrap();
|
2021-09-27 10:06:22 +00:00
|
|
|
/// let document = OPML::from_reader(&mut file).unwrap();
|
2021-03-22 21:31:59 +00:00
|
|
|
/// ```
|
|
|
|
pub fn from_reader<R>(reader: &mut R) -> Result<Self, Error>
|
|
|
|
where
|
|
|
|
R: std::io::Read,
|
|
|
|
{
|
|
|
|
let mut s = String::new();
|
|
|
|
reader.read_to_string(&mut s)?;
|
2021-03-23 12:02:16 +00:00
|
|
|
Self::from_str(&s).map_err(Into::into)
|
2021-03-22 21:31:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// Helper function to add an [`Outline`] element with `text` and `xml_url`
|
|
|
|
/// attributes to the [`Body`]. Useful for creating feed lists quickly.
|
|
|
|
/// This function also exists as [`Outline::add_feed`] for grouped lists.
|
2020-08-29 14:46:54 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use opml::{OPML, Outline};
|
|
|
|
///
|
|
|
|
/// let mut opml = OPML::default();
|
|
|
|
/// opml.add_feed("Feed Name", "https://example.com/");
|
|
|
|
/// let added_feed = opml.body.outlines.first().unwrap();
|
|
|
|
///
|
|
|
|
/// let expected_feed = &Outline {
|
|
|
|
/// text: "Feed Name".to_string(),
|
|
|
|
/// xml_url: Some("https://example.com/".to_string()),
|
|
|
|
/// ..Outline::default()
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// assert_eq!(added_feed, expected_feed);
|
|
|
|
/// ```
|
|
|
|
pub fn add_feed(&mut self, text: &str, url: &str) -> &mut Self {
|
|
|
|
self.body.outlines.push(Outline {
|
|
|
|
text: text.to_string(),
|
|
|
|
xml_url: Some(url.to_string()),
|
|
|
|
..Outline::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// Deprecated, use [`OPML::to_string`] instead.
|
|
|
|
#[deprecated(note = "Use to_string instead", since = "1.1.0")]
|
2021-03-22 11:41:11 +00:00
|
|
|
pub fn to_xml(&self) -> Result<String, Error> {
|
2021-03-22 21:31:59 +00:00
|
|
|
self.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts the struct to an XML document.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use opml::OPML;
|
|
|
|
///
|
|
|
|
/// let opml = OPML::default();
|
|
|
|
/// let xml = opml.to_string().unwrap();
|
|
|
|
///
|
|
|
|
/// let expected = r#"<opml version="2.0"><head/><body/></opml>"#;
|
|
|
|
/// assert_eq!(xml, expected);
|
|
|
|
/// ```
|
|
|
|
pub fn to_string(&self) -> Result<String, Error> {
|
|
|
|
Ok(XmlWrite::to_string(self)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts the struct to an XML document and writes it using the writer.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2021-03-23 12:02:16 +00:00
|
|
|
/// ```rust,no_run
|
2021-03-22 21:31:59 +00:00
|
|
|
/// use opml::OPML;
|
2021-03-23 12:02:16 +00:00
|
|
|
/// use std::fs::File;
|
2021-03-22 21:31:59 +00:00
|
|
|
///
|
|
|
|
/// let opml = OPML::default();
|
2021-03-23 12:02:16 +00:00
|
|
|
/// let mut file = File::create("file.opml").unwrap();
|
|
|
|
/// let xml = opml.to_writer(&mut file).unwrap();
|
2021-03-22 21:31:59 +00:00
|
|
|
/// ```
|
|
|
|
pub fn to_writer<W>(&self, writer: &mut W) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
W: std::io::Write,
|
|
|
|
{
|
|
|
|
let xml_string = self.to_string()?;
|
2021-09-27 10:07:31 +00:00
|
|
|
writer.write_all(xml_string.as_bytes())?;
|
2021-03-22 21:31:59 +00:00
|
|
|
Ok(())
|
2020-08-29 14:46:54 +00:00
|
|
|
}
|
2020-08-28 11:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for OPML {
|
2020-08-29 14:46:54 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
OPML {
|
|
|
|
version: "2.0".to_string(),
|
|
|
|
head: Some(Head::default()),
|
|
|
|
body: Body::default(),
|
2020-08-28 11:37:29 +00:00
|
|
|
}
|
2020-08-29 14:46:54 +00:00
|
|
|
}
|
2020-08-28 11:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// The [`Head`] child element of [`OPML`]. Contains the metadata of the OPML
|
|
|
|
/// document.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[derive(
|
2022-09-30 23:00:34 +00:00
|
|
|
XmlWrite,
|
|
|
|
XmlRead,
|
|
|
|
PartialEq,
|
|
|
|
Eq,
|
|
|
|
Debug,
|
|
|
|
Clone,
|
|
|
|
Default,
|
|
|
|
Serialize,
|
|
|
|
Deserialize,
|
2020-08-29 14:46:54 +00:00
|
|
|
)]
|
2020-08-28 11:37:29 +00:00
|
|
|
#[xml(tag = "head")]
|
|
|
|
pub struct Head {
|
2020-08-29 14:46:54 +00:00
|
|
|
/// The title of the document.
|
|
|
|
#[xml(flatten_text = "title")]
|
|
|
|
pub title: Option<String>,
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2020-08-29 14:46:54 +00:00
|
|
|
/// A date-time (RFC822) indicating when the document was created.
|
|
|
|
#[xml(flatten_text = "dateCreated")]
|
|
|
|
pub date_created: Option<String>,
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2020-08-29 14:46:54 +00:00
|
|
|
/// A date-time (RFC822) indicating when the document was last modified.
|
|
|
|
#[xml(flatten_text = "dateModified")]
|
|
|
|
pub date_modified: Option<String>,
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2020-08-29 14:46:54 +00:00
|
|
|
/// The name of the document owner.
|
|
|
|
#[xml(flatten_text = "ownerName")]
|
|
|
|
pub owner_name: Option<String>,
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2020-08-29 14:46:54 +00:00
|
|
|
/// The email address of the document owner.
|
|
|
|
#[xml(flatten_text = "ownerEmail")]
|
|
|
|
pub owner_email: Option<String>,
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2020-08-29 14:46:54 +00:00
|
|
|
/// A link to the website of the document owner.
|
|
|
|
#[xml(flatten_text = "ownerId")]
|
|
|
|
pub owner_id: Option<String>,
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2020-08-29 14:46:54 +00:00
|
|
|
/// A link to the documentation of the OPML format used for this document.
|
|
|
|
#[xml(flatten_text = "docs")]
|
|
|
|
pub docs: Option<String>,
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// A comma-separated list of line numbers that are expanded. The line numbers
|
|
|
|
/// in the list tell you which headlines to expand. The order is important.
|
|
|
|
/// For each element in the list, X, starting at the first summit, navigate
|
|
|
|
/// flatdown X times and expand. Repeat for each element in the list.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[xml(flatten_text = "expansionState")]
|
|
|
|
pub expansion_state: Option<String>,
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// A number indicating which line of the outline is displayed on the top line
|
|
|
|
/// of the window. This number is calculated with the expansion state already
|
|
|
|
/// applied.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[xml(flatten_text = "vertScrollState")]
|
|
|
|
pub vert_scroll_state: Option<i32>,
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2020-08-29 14:46:54 +00:00
|
|
|
/// The pixel location of the top edge of the window.
|
|
|
|
#[xml(flatten_text = "windowTop")]
|
|
|
|
pub window_top: Option<i32>,
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2020-08-29 14:46:54 +00:00
|
|
|
/// The pixel location of the left edge of the window.
|
|
|
|
#[xml(flatten_text = "windowLeft")]
|
|
|
|
pub window_left: Option<i32>,
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2020-08-29 14:46:54 +00:00
|
|
|
/// The pixel location of the bottom edge of the window.
|
|
|
|
#[xml(flatten_text = "windowBottom")]
|
|
|
|
pub window_bottom: Option<i32>,
|
2020-08-28 11:37:29 +00:00
|
|
|
|
2020-08-29 14:46:54 +00:00
|
|
|
/// The pixel location of the right edge of the window.
|
|
|
|
#[xml(flatten_text = "windowRight")]
|
|
|
|
pub window_right: Option<i32>,
|
2020-08-28 11:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// The [`Body`] child element of [`OPML`]. Contains all the [`Outline`]
|
|
|
|
/// elements.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[derive(
|
2022-09-30 23:00:34 +00:00
|
|
|
XmlWrite,
|
|
|
|
XmlRead,
|
|
|
|
PartialEq,
|
|
|
|
Eq,
|
|
|
|
Debug,
|
|
|
|
Clone,
|
|
|
|
Default,
|
|
|
|
Serialize,
|
|
|
|
Deserialize,
|
2020-08-29 14:46:54 +00:00
|
|
|
)]
|
2020-08-28 11:37:29 +00:00
|
|
|
#[xml(tag = "body")]
|
|
|
|
pub struct Body {
|
2021-09-27 10:06:22 +00:00
|
|
|
/// All the top-level [`Outline`] elements.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[xml(child = "outline")]
|
|
|
|
pub outlines: Vec<Outline>,
|
2020-08-28 11:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// The [`Outline`] element.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[derive(
|
2022-09-30 23:00:34 +00:00
|
|
|
XmlWrite,
|
|
|
|
XmlRead,
|
|
|
|
PartialEq,
|
|
|
|
Eq,
|
|
|
|
Debug,
|
|
|
|
Clone,
|
|
|
|
Default,
|
|
|
|
Serialize,
|
|
|
|
Deserialize,
|
2020-08-29 14:46:54 +00:00
|
|
|
)]
|
2020-08-28 11:37:29 +00:00
|
|
|
#[xml(tag = "outline")]
|
|
|
|
pub struct Outline {
|
2021-09-27 10:06:22 +00:00
|
|
|
/// Every outline element must have at least a text attribute, which is what
|
|
|
|
/// is displayed when an outliner opens the OPML document.
|
|
|
|
///
|
|
|
|
/// Version 1.0 OPML documents may omit this attribute, so for compatibility
|
|
|
|
/// and strictness this attribute is "technically optional" as it will be
|
|
|
|
/// replaced by an empty String if it is omitted.
|
|
|
|
///
|
2020-08-29 14:46:54 +00:00
|
|
|
/// Text attributes may contain encoded HTML markup.
|
|
|
|
#[xml(default, attr = "text")]
|
|
|
|
pub text: String,
|
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// A string that indicates how the other attributes of the [`Outline`]
|
|
|
|
/// should be interpreted.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[xml(attr = "type")]
|
|
|
|
pub r#type: Option<String>,
|
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// Indicating whether the outline is commented or not. By convention if an
|
|
|
|
/// outline is commented, all subordinate outlines are considered to also be
|
|
|
|
/// commented.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[xml(attr = "isComment")]
|
|
|
|
pub is_comment: Option<bool>,
|
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// Indicating whether a breakpoint is set on this outline. This attribute is
|
|
|
|
/// mainly necessary for outlines used to edit scripts.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[xml(attr = "isBreakpoint")]
|
|
|
|
pub is_breakpoint: Option<bool>,
|
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// The date-time (RFC822) that this [`Outline`] element was created.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[xml(attr = "created")]
|
|
|
|
pub created: Option<String>,
|
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// A string of comma-separated slash-delimited category strings, in the
|
|
|
|
/// format defined by the [RSS 2.0 category] element. To represent a "tag",
|
|
|
|
/// the category string should contain no slashes.
|
|
|
|
///
|
|
|
|
/// [RSS 2.0 category]: https://cyber.law.harvard.edu/rss/rss.html#ltcategorygtSubelementOfLtitemgt
|
2020-08-29 14:46:54 +00:00
|
|
|
#[xml(attr = "category")]
|
|
|
|
pub category: Option<String>,
|
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// Child [`Outline`] elements of the current one.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[xml(child = "outline")]
|
|
|
|
pub outlines: Vec<Outline>,
|
|
|
|
|
|
|
|
/// The HTTP address of the feed.
|
|
|
|
#[xml(attr = "xmlUrl")]
|
|
|
|
pub xml_url: Option<String>,
|
|
|
|
|
|
|
|
/// The top-level description element from the feed.
|
|
|
|
#[xml(attr = "description")]
|
|
|
|
pub description: Option<String>,
|
|
|
|
|
|
|
|
/// The top-level link element from the feed.
|
|
|
|
#[xml(attr = "htmlUrl")]
|
|
|
|
pub html_url: Option<String>,
|
|
|
|
|
|
|
|
/// The top-level language element from the feed.
|
|
|
|
#[xml(attr = "language")]
|
|
|
|
pub language: Option<String>,
|
|
|
|
|
|
|
|
/// The top-level title element from the feed.
|
|
|
|
#[xml(attr = "title")]
|
|
|
|
pub title: Option<String>,
|
|
|
|
|
|
|
|
/// The version of the feed's format (such as RSS 0.91, 2.0, ...).
|
|
|
|
#[xml(attr = "version")]
|
|
|
|
pub version: Option<String>,
|
|
|
|
|
2021-09-27 10:06:22 +00:00
|
|
|
/// A link that can point to another OPML document or to something that can
|
|
|
|
/// be displayed in a web browser.
|
2020-08-29 14:46:54 +00:00
|
|
|
#[xml(attr = "url")]
|
|
|
|
pub url: Option<String>,
|
2020-08-28 11:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Outline {
|
2021-09-27 10:06:22 +00:00
|
|
|
/// Helper function to add an [`Outline`] element with `text` and `xml_url`
|
|
|
|
/// attributes as a child element, useful for creating grouped lists. This
|
|
|
|
/// function also exists as [`OPML::add_feed`] for non-grouped lists.
|
2020-08-29 14:46:54 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use opml::{Outline};
|
|
|
|
///
|
|
|
|
/// let mut group = Outline::default();
|
|
|
|
/// group.add_feed("Feed Name", "https://example.com/");
|
|
|
|
/// let added_feed = group.outlines.first().unwrap();
|
|
|
|
///
|
|
|
|
/// let expected_feed = &Outline {
|
|
|
|
/// text: "Feed Name".to_string(),
|
|
|
|
/// xml_url: Some("https://example.com/".to_string()),
|
|
|
|
/// ..Outline::default()
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// assert_eq!(added_feed, expected_feed);
|
|
|
|
/// ```
|
|
|
|
pub fn add_feed(&mut self, name: &str, url: &str) -> &mut Self {
|
|
|
|
self.outlines.push(Outline {
|
|
|
|
text: name.to_string(),
|
|
|
|
xml_url: Some(url.to_string()),
|
|
|
|
..Outline::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
2020-08-28 11:37:29 +00:00
|
|
|
}
|