[−][src]Crate opml
This crate provides an API to parse and construct OPML documents to and from regular Rust structs.
Getting Started
Parsing
Parsing XML into an OPML struct can be done with OPML::new()
. Resulting in an error if the XML can't be parsed, if 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.
use opml::{OPML, Outline}; let xml = r#"<opml version="2.0"><head/><body><outline text="Outline"/></body></opml>"#; let parsed = OPML::new(xml).unwrap(); let mut expected = OPML::default(); expected.body.outlines.push(Outline { text: "Outline".to_string(), ..Outline::default() }); println!("{:#?}", parsed); assert_eq!(parsed, expected);
Constructing
Constructing OPMLs is very easy as all you have to do is instantiate the OPML struct with OPML::default()
, add anything wanted and then call OPML::to_xml()
to return the XML as a string.
use opml::{Head, OPML}; let mut opml = OPML::default(); opml.head = Some(Head { title: Some("Rust Feeds".to_string()), ..Head::default() }); opml .add_feed("Rust Blog", "https://blog.rust-lang.org/feed.xml") .add_feed( "Inside Rust", "https://blog.rust-lang.org/inside-rust/feed.xml", ); let xml = opml.to_xml().unwrap(); let expected = r#"<opml version="2.0"><head><title>Rust Feeds</title></head><body><outline text="Rust Blog" xmlUrl="https://blog.rust-lang.org/feed.xml"/><outline text="Inside Rust" xmlUrl="https://blog.rust-lang.org/inside-rust/feed.xml"/></body></opml>"#; println!("{}", xml); assert_eq!(xml, expected);
CLI
Looking for a command-line utility to parse OPML documents? Check out the opml-cli
, a small wrapper around this crate that will let you do just that.
License
Open-sourced with either the
- Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (http://opensource.org/licenses/MIT)
at your option.
The samples located in tests/spec_samples
were taken from the OPML 2.0 spec and are subject to their own license.
Structs
Body | |
Head | The Head child element of OPML. Contains the metadata of the OPML document. |
OPML | The top-level OPML element. |
Outline | The Outline element. |