From 8279e43330f34bd95cfe26317fbe79028dcb3df6 Mon Sep 17 00:00:00 2001 From: Bauke Date: Fri, 28 Aug 2020 13:37:29 +0200 Subject: [PATCH] Consolidate the API and CLI into one repository with Cargo workspaces. --- .gitignore | 5 +- Cargo.lock | 354 +++++++++++++++++ Cargo.toml | 26 +- README.md | 28 +- opml_api/Cargo.toml | 20 + .../examples}/opml_samples/rust_feeds.opml | 0 {examples => opml_api/examples}/rss.rs | 0 opml_api/source/lib.rs | 352 +++++++++++++++++ {tests => opml_api/tests}/construction.rs | 0 {tests => opml_api/tests}/errors.rs | 0 .../tests}/samples/construction_1.opml | 0 .../tests}/samples/construction_2.opml | 0 .../samples/invalid_opml_no_outlines.opml | 0 .../tests}/samples/invalid_opml_version.opml | 0 .../samples/invalid_opml_version_1_0.opml | 0 .../samples/invalid_opml_version_1_1.opml | 0 .../tests}/samples/invalid_xml.opml | 0 .../tests}/samples/minimum_valid_opml.opml | 0 .../tests}/samples/valid_opml_1_0.opml | 0 .../samples/valid_opml_with_everything.opml | 0 {tests => opml_api/tests}/spec_samples.rs | 0 .../tests}/spec_samples/LICENSE | 0 .../tests}/spec_samples/category.opml | 0 .../tests}/spec_samples/directory.opml | 0 .../tests}/spec_samples/placesLived.opml | 0 .../tests}/spec_samples/simpleScript.opml | 0 .../tests}/spec_samples/states.opml | 0 .../tests}/spec_samples/subscriptionList.opml | 0 {tests => opml_api/tests}/valid.rs | 0 opml_cli/Cargo.toml | 28 ++ opml_cli/source/main.rs | 110 ++++++ opml_cli/tests/invalid.rs | 27 ++ opml_cli/tests/samples/youtube.opml | 15 + opml_cli/tests/snapshots/json-pretty.json | 57 +++ opml_cli/tests/snapshots/json.json | 1 + opml_cli/tests/snapshots/missing-file.txt | 7 + opml_cli/tests/snapshots/missing-format.txt | 7 + opml_cli/tests/snapshots/rss.txt | 4 + opml_cli/tests/valid.rs | 38 ++ rustfmt.toml | 2 - source/lib.rs | 363 ------------------ 41 files changed, 1035 insertions(+), 409 deletions(-) create mode 100644 Cargo.lock create mode 100644 opml_api/Cargo.toml rename {examples => opml_api/examples}/opml_samples/rust_feeds.opml (100%) rename {examples => opml_api/examples}/rss.rs (100%) create mode 100644 opml_api/source/lib.rs rename {tests => opml_api/tests}/construction.rs (100%) rename {tests => opml_api/tests}/errors.rs (100%) rename {tests => opml_api/tests}/samples/construction_1.opml (100%) rename {tests => opml_api/tests}/samples/construction_2.opml (100%) rename {tests => opml_api/tests}/samples/invalid_opml_no_outlines.opml (100%) rename {tests => opml_api/tests}/samples/invalid_opml_version.opml (100%) rename {tests => opml_api/tests}/samples/invalid_opml_version_1_0.opml (100%) rename {tests => opml_api/tests}/samples/invalid_opml_version_1_1.opml (100%) rename {tests => opml_api/tests}/samples/invalid_xml.opml (100%) rename {tests => opml_api/tests}/samples/minimum_valid_opml.opml (100%) rename {tests => opml_api/tests}/samples/valid_opml_1_0.opml (100%) rename {tests => opml_api/tests}/samples/valid_opml_with_everything.opml (100%) rename {tests => opml_api/tests}/spec_samples.rs (100%) rename {tests => opml_api/tests}/spec_samples/LICENSE (100%) rename {tests => opml_api/tests}/spec_samples/category.opml (100%) rename {tests => opml_api/tests}/spec_samples/directory.opml (100%) rename {tests => opml_api/tests}/spec_samples/placesLived.opml (100%) rename {tests => opml_api/tests}/spec_samples/simpleScript.opml (100%) rename {tests => opml_api/tests}/spec_samples/states.opml (100%) rename {tests => opml_api/tests}/spec_samples/subscriptionList.opml (100%) rename {tests => opml_api/tests}/valid.rs (100%) create mode 100644 opml_cli/Cargo.toml create mode 100644 opml_cli/source/main.rs create mode 100644 opml_cli/tests/invalid.rs create mode 100644 opml_cli/tests/samples/youtube.opml create mode 100644 opml_cli/tests/snapshots/json-pretty.json create mode 100644 opml_cli/tests/snapshots/json.json create mode 100644 opml_cli/tests/snapshots/missing-file.txt create mode 100644 opml_cli/tests/snapshots/missing-format.txt create mode 100644 opml_cli/tests/snapshots/rss.txt create mode 100644 opml_cli/tests/valid.rs delete mode 100644 rustfmt.toml delete mode 100644 source/lib.rs diff --git a/.gitignore b/.gitignore index 32fdd69..c5cd39f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,5 @@ # Compiled files and executables. -/target/ - -# Cargo lockfile. -Cargo.lock +target/ # Backup files generated by rustfmt. **/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..023ccb8 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,354 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "aho-corasick" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86" +dependencies = [ + "memchr", +] + +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + +[[package]] +name = "assert_cmd" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c88b9ca26f9c16ec830350d309397e74ee9abdfd8eb1f71cb6ecc71a3fc818da" +dependencies = [ + "doc-comment", + "predicates", + "predicates-core", + "predicates-tree", + "wait-timeout", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + +[[package]] +name = "difference" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" + +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + +[[package]] +name = "hermit-abi" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9" +dependencies = [ + "libc", +] + +[[package]] +name = "itoa" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" + +[[package]] +name = "jetscii" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f25cca2463cb19dbb1061eb3bd38a8b5e4ce1cc5a5a9fc0e02de486d92b9b05" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.76" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3" + +[[package]] +name = "memchr" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" + +[[package]] +name = "opml" +version = "0.3.0" +dependencies = [ + "regex", + "serde", + "strong-xml", +] + +[[package]] +name = "opml_cli" +version = "0.3.0" +dependencies = [ + "assert_cmd", + "clap", + "opml", + "serde", + "serde_json", +] + +[[package]] +name = "predicates" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96bfead12e90dccead362d62bb2c90a5f6fc4584963645bc7f71a735e0b0735a" +dependencies = [ + "difference", + "predicates-core", +] + +[[package]] +name = "predicates-core" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06075c3a3e92559ff8929e7a280684489ea27fe44805174c3ebd9328dcb37178" + +[[package]] +name = "predicates-tree" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e63c4859013b38a76eca2414c64911fba30def9e3202ac461a2d22831220124" +dependencies = [ + "predicates-core", + "treeline", +] + +[[package]] +name = "proc-macro2" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", + "thread_local", +] + +[[package]] +name = "regex-syntax" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + +[[package]] +name = "serde" +version = "1.0.115" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.115" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "609feed1d0a73cc36a0182a840a9b37b4a82f0b1150369f0536a9e3f2a31dc48" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "164eacbdb13512ec2745fb09d51fd5b22b0d65ed294a1dcf7285a360c80a675c" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "strong-xml" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee06e7e5baf4508dea83506a83fcc5b80a404d4c0e9c473c9a4b38b802af3a07" +dependencies = [ + "jetscii", + "lazy_static", + "memchr", + "strong-xml-derive", + "xmlparser", +] + +[[package]] +name = "strong-xml-derive" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2e4e25fb64e61f55d495134d9e5ac68b1fa4bb2855b5a5b53857b9460e2bfde" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "syn" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891d8d6567fe7c7f8835a3a98af4208f3846fba258c1bc3c31d6e506239f11f9" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + +[[package]] +name = "thread_local" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "treeline" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7f741b240f1a48843f9b8e0444fb55fb2a4ff67293b50a9179dfd5ea67f8d41" + +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + +[[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "xmlparser" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52613e655f6f11f63c0fe7d1c3b5ef69e44d96df9b65dab296b441ed0e1125f5" diff --git a/Cargo.toml b/Cargo.toml index 1205e4e..caf598d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,23 +1,7 @@ # https://doc.rust-lang.org/cargo/reference/manifest.html -[package] -name = "opml" -authors = ["Holllo "] -version = "0.2.4" -license = "MIT OR Apache-2.0" -description = "An OPML parser for Rust." -repository = "https://git.holllo.cc/holllo/opml" -readme = "README.md" -edition = "2018" -keywords = ["xml", "opml"] - -[lib] -path = "source/lib.rs" - -[dependencies] -regex = "1.3.9" -strong-xml = "0.6.0" - -[dependencies.serde] -version = "1.0.114" -features = ["derive"] +[workspace] +members = [ + "opml_api", + "opml_cli" +] diff --git a/README.md b/README.md index 228fbbb..5c6f0ca 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,21 @@ # OPML -> An OPML parser for Rust. +> An OPML parser for Rust and the command-line. -## Getting Started +## API -```rust -use opml::OPML; - -let xml = r#""#; -let parsed = OPML::new(xml).unwrap(); - -println!("{:#?}", parsed); -``` - -Check out [the documentation](https://docs.rs/opml/) for further details. +For the API documentation, see [docs.rs](https://docs.rs/opml). ## CLI -Looking for a command-line utility to parse OPML documents? Check out [the `opml-cli`](https://git.holllo.cc/Holllo/opml-cli/), a small wrapper around this crate that will let you do just that. +To install the OPML command-line parser, run `cargo install opml_cli` or download a precompiled executable directly from [the Releases page](https://github.com/Holllo/opml/releases/latest). + +Then run `opml --help` to see all the available options. ## License -Open-sourced with either the +Both the API and command-line parser are licensed under either of [Apache License, Version 2.0](https://github.com/Holllo/opml/blob/main/LICENSE-Apache) or [MIT license](https://github.com/Holllo/opml/blob/main/LICENSE-MIT) at your option. -* [Apache License, Version 2.0](https://git.holllo.cc/Holllo/opml/src/branch/main/LICENSE-Apache) (http://www.apache.org/licenses/LICENSE-2.0) -* [MIT license](https://git.holllo.cc/Holllo/opml/src/branch/main/LICENSE-MIT) (http://opensource.org/licenses/MIT) +Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in either crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. -at your option. - -The samples [located in `tests/spec_samples`](https://git.holllo.cc/Holllo/opml/src/branch/main/tests/spec_samples) were [taken from the OPML 2.0 spec](http://dev.opml.org/spec2.html#examples) and are subject to [their own license](https://git.holllo.cc/Holllo/opml/src/branch/main/tests/spec_samples/LICENSE). +The samples [located in `opml_api/tests/spec_samples`](https://github.com/Holllo/opml/tree/main/opml_api/tests/spec_samples) were [taken from the OPML 2.0 spec](http://dev.opml.org/spec2.html#examples) and are subject to [their own license](https://github.com/Holllo/opml/tree/main/opml_api/tests/spec_samples). diff --git a/opml_api/Cargo.toml b/opml_api/Cargo.toml new file mode 100644 index 0000000..ff9305c --- /dev/null +++ b/opml_api/Cargo.toml @@ -0,0 +1,20 @@ +# https://doc.rust-lang.org/cargo/reference/manifest.html + +[package] +name = "opml" +description = "An OPML parser for Rust." +authors = ["Holllo "] +version = "0.3.0" +license = "MIT OR Apache-2.0" +repository = "https://github.com/Holllo/opml" +readme = "../README.md" +edition = "2018" +keywords = ["xml", "opml"] + +[lib] +path = "source/lib.rs" + +[dependencies] +regex = "1.3" +serde = { version = "1.0", features = ["derive"] } +strong-xml = "0.6" diff --git a/examples/opml_samples/rust_feeds.opml b/opml_api/examples/opml_samples/rust_feeds.opml similarity index 100% rename from examples/opml_samples/rust_feeds.opml rename to opml_api/examples/opml_samples/rust_feeds.opml diff --git a/examples/rss.rs b/opml_api/examples/rss.rs similarity index 100% rename from examples/rss.rs rename to opml_api/examples/rss.rs diff --git a/opml_api/source/lib.rs b/opml_api/source/lib.rs new file mode 100644 index 0000000..86f98a1 --- /dev/null +++ b/opml_api/source/lib.rs @@ -0,0 +1,352 @@ +//! This crate provides an API to parse and construct +//! [OPML documents](http://dev.opml.org/spec2.html) to and from regular Rust +//! structs. +//! +//! ## Getting Started +//! +//! ### Parsing +//! +//! Parsing XML into [an OPML struct](struct.OPML.html) can be done with +//! [`OPML::new()`](struct.OPML.html#method.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](struct.Body.html) element contains no child +//! [Outline](struct.Outline.html) elements, +//! [as per the spec](http://dev.opml.org/spec2.html#whatIsALtbodygt). +//! +//! ```rust +//! use opml::{OPML, Outline}; +//! +//! let xml = r#""#; +//! 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](struct.OPML.html) with +//! [`OPML::default()`](struct.OPML.html#method.default), add anything wanted +//! and then call [`OPML::to_xml()`](struct.OPML.html#method.to_xml) to return +//! the XML as a string. +//! +//! ```rust +//! 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#"Rust Feeds"#; +//! println!("{}", xml); +//! assert_eq!(xml, expected); +//! ``` + +use regex::Regex; +use serde::{Deserialize, Serialize}; +use strong_xml::{XmlError, XmlRead, XmlWrite}; + +/// The top-level [OPML](struct.OPML.html) element. +#[derive(XmlWrite, XmlRead, PartialEq, Debug, Clone, Serialize, Deserialize)] +#[xml(tag = "opml")] +pub struct OPML { + /// The version attribute from the element, valid values are `1.0`, `1.1` and `2.0`. + #[xml(attr = "version")] + pub version: String, + + /// The [Head](struct.Head.html) child element. Contains the metadata of the OPML document. + #[xml(child = "head")] + pub head: Option, + + /// The [Body](struct.Body.html) child element. Contains all the [Outlines](struct.Outline.html). + #[xml(child = "body")] + pub body: Body, +} + +impl OPML { + /// Parses an OPML document. + /// + /// # Example + /// + /// ```rust + /// use opml::{OPML, Outline}; + /// + /// let xml = r#""#; + /// let parsed = OPML::new(xml).unwrap(); + /// + /// let mut expected = OPML::default(); + /// expected.body.outlines.push(Outline { + /// text: "Outline".to_string(), + /// ..Outline::default() + /// }); + /// + /// assert_eq!(parsed, expected); + /// ``` + pub fn new(xml: &str) -> Result { + let opml: Result = OPML::from_str(xml); + + let opml = match opml { + Ok(value) => value, + Err(err) => return Err(format!("XML parsing error: {:#?}", err)), + }; + + let version = &opml.version; + + // SPEC: The version attribute is a version string, of the form, x.y, where x and y are both numeric strings. + let valid_version_regex = Regex::new(r"^\d+\.\d+$").unwrap(); + let valid_versions = vec!["1.0", "1.1", "2.0"]; + + if !valid_version_regex.is_match(version) || !valid_versions.contains(&version.as_str()) { + return Err(format!( + "Unsupported OPML version detected: {}", + opml.version + )); + } + + // SPEC: A `` contains one or more `` elements. + if opml.body.outlines.is_empty() { + return Err("OPML body has no outlines.".to_string()); + } + + Ok(opml) + } + + /// Helper function to add an [Outline](struct.Outline.html) element with `text` and `xml_url` attributes to the [Body](struct.Body.html). Useful for creating feed lists quickly. This function [also exists on the Outline struct](struct.Outline.html#method.add_feed) to create grouped lists easily. + /// + /// # 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 + } + + /// Converts the struct to an XML document. + /// + /// # Example + /// + /// ```rust + /// use opml::OPML; + /// + /// let opml = OPML::default(); + /// let xml = opml.to_xml().unwrap(); + /// + /// let expected = r#""#; + /// assert_eq!(xml, expected); + /// ``` + pub fn to_xml(&self) -> Result { + let result: Result = self.to_string(); + + match result { + Ok(value) => Ok(value), + Err(err) => Err(format!("XML writing error: {:#?}", err)), + } + } +} + +impl Default for OPML { + fn default() -> Self { + OPML { + version: "2.0".to_string(), + head: Some(Head::default()), + body: Body::default(), + } + } +} + +/// The [Head](struct.Head.html) child element of [OPML](struct.OPML.html). +/// Contains the metadata of the OPML document. +#[derive(XmlWrite, XmlRead, PartialEq, Debug, Clone, Default, Serialize, Deserialize)] +#[xml(tag = "head")] +pub struct Head { + /// The title of the document. + #[xml(flatten_text = "title")] + pub title: Option, + + /// A date-time (RFC822) indicating when the document was created. + #[xml(flatten_text = "dateCreated")] + pub date_created: Option, + + /// A date-time (RFC822) indicating when the document was last modified. + #[xml(flatten_text = "dateModified")] + pub date_modified: Option, + + /// The name of the document owner. + #[xml(flatten_text = "ownerName")] + pub owner_name: Option, + + /// The email address of the document owner. + #[xml(flatten_text = "ownerEmail")] + pub owner_email: Option, + + /// A link to the website of the document owner. + #[xml(flatten_text = "ownerId")] + pub owner_id: Option, + + /// A link to the documentation of the OPML format used for this document. + #[xml(flatten_text = "docs")] + pub docs: Option, + + /// 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. + #[xml(flatten_text = "expansionState")] + pub expansion_state: Option, + + /// 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. + #[xml(flatten_text = "vertScrollState")] + pub vert_scroll_state: Option, + + /// The pixel location of the top edge of the window. + #[xml(flatten_text = "windowTop")] + pub window_top: Option, + + /// The pixel location of the left edge of the window. + #[xml(flatten_text = "windowLeft")] + pub window_left: Option, + + /// The pixel location of the bottom edge of the window. + #[xml(flatten_text = "windowBottom")] + pub window_bottom: Option, + + /// The pixel location of the right edge of the window. + #[xml(flatten_text = "windowRight")] + pub window_right: Option, +} + +/// The [Body](struct.Body.html) child element of [OPML](struct.OPML.html). Contains all the [Outlines](struct.Outline.html). +#[derive(XmlWrite, XmlRead, PartialEq, Debug, Clone, Default, Serialize, Deserialize)] +#[xml(tag = "body")] +pub struct Body { + /// All the top-level [Outline](struct.Outline.html) elements. + #[xml(child = "outline")] + pub outlines: Vec, +} + +/// The [Outline](struct.Outline.html) element. +#[derive(XmlWrite, XmlRead, PartialEq, Debug, Clone, Default, Serialize, Deserialize)] +#[xml(tag = "outline")] +pub struct Outline { + /// 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. + /// Text attributes may contain encoded HTML markup. + #[xml(default, attr = "text")] + pub text: String, + + /// A string that indicates how the other attributes of the [Outline](struct.Outline.html) should be interpreted. + #[xml(attr = "type")] + pub r#type: Option, + + /// Indicating whether the outline is commented or not. By convention if an outline is commented, all subordinate outlines are considered to also be commented. + #[xml(attr = "isComment")] + pub is_comment: Option, + + /// Indicating whether a breakpoint is set on this outline. This attribute is mainly necessary for outlines used to edit scripts. + #[xml(attr = "isBreakpoint")] + pub is_breakpoint: Option, + + /// The date-time (RFC822) that this [Outline](struct.Outline.html) element was created. + #[xml(attr = "created")] + pub created: Option, + + /// A string of comma-separated slash-delimited category strings, in the format defined by the [RSS 2.0 category](http://cyber.law.harvard.edu/rss/rss.html#ltcategorygtSubelementOfLtitemgt) element. To represent a "tag," the category string should contain no slashes. + #[xml(attr = "category")] + pub category: Option, + + /// Child [Outline](struct.Outline.html) elements of the current one. + #[xml(child = "outline")] + pub outlines: Vec, + + /// The HTTP address of the feed. + #[xml(attr = "xmlUrl")] + pub xml_url: Option, + + /// The top-level description element from the feed. + #[xml(attr = "description")] + pub description: Option, + + /// The top-level link element from the feed. + #[xml(attr = "htmlUrl")] + pub html_url: Option, + + /// The top-level language element from the feed. + #[xml(attr = "language")] + pub language: Option, + + /// The top-level title element from the feed. + #[xml(attr = "title")] + pub title: Option, + + /// The version of the feed's format (such as RSS 0.91, 2.0, ...). + #[xml(attr = "version")] + pub version: Option, + + /// A link that can point to another OPML document or to something that can be displayed in a web browser. + #[xml(attr = "url")] + pub url: Option, +} + +impl Outline { + /// Helper function to add an [Outline](struct.Outline.html) element with `text` and `xml_url` attributes as a child element. Useful for creating grouped lists. This function [also exists on the OPML struct](struct.OPML.html#method.add_feed) for non-grouped lists. + /// + /// # 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 + } +} diff --git a/tests/construction.rs b/opml_api/tests/construction.rs similarity index 100% rename from tests/construction.rs rename to opml_api/tests/construction.rs diff --git a/tests/errors.rs b/opml_api/tests/errors.rs similarity index 100% rename from tests/errors.rs rename to opml_api/tests/errors.rs diff --git a/tests/samples/construction_1.opml b/opml_api/tests/samples/construction_1.opml similarity index 100% rename from tests/samples/construction_1.opml rename to opml_api/tests/samples/construction_1.opml diff --git a/tests/samples/construction_2.opml b/opml_api/tests/samples/construction_2.opml similarity index 100% rename from tests/samples/construction_2.opml rename to opml_api/tests/samples/construction_2.opml diff --git a/tests/samples/invalid_opml_no_outlines.opml b/opml_api/tests/samples/invalid_opml_no_outlines.opml similarity index 100% rename from tests/samples/invalid_opml_no_outlines.opml rename to opml_api/tests/samples/invalid_opml_no_outlines.opml diff --git a/tests/samples/invalid_opml_version.opml b/opml_api/tests/samples/invalid_opml_version.opml similarity index 100% rename from tests/samples/invalid_opml_version.opml rename to opml_api/tests/samples/invalid_opml_version.opml diff --git a/tests/samples/invalid_opml_version_1_0.opml b/opml_api/tests/samples/invalid_opml_version_1_0.opml similarity index 100% rename from tests/samples/invalid_opml_version_1_0.opml rename to opml_api/tests/samples/invalid_opml_version_1_0.opml diff --git a/tests/samples/invalid_opml_version_1_1.opml b/opml_api/tests/samples/invalid_opml_version_1_1.opml similarity index 100% rename from tests/samples/invalid_opml_version_1_1.opml rename to opml_api/tests/samples/invalid_opml_version_1_1.opml diff --git a/tests/samples/invalid_xml.opml b/opml_api/tests/samples/invalid_xml.opml similarity index 100% rename from tests/samples/invalid_xml.opml rename to opml_api/tests/samples/invalid_xml.opml diff --git a/tests/samples/minimum_valid_opml.opml b/opml_api/tests/samples/minimum_valid_opml.opml similarity index 100% rename from tests/samples/minimum_valid_opml.opml rename to opml_api/tests/samples/minimum_valid_opml.opml diff --git a/tests/samples/valid_opml_1_0.opml b/opml_api/tests/samples/valid_opml_1_0.opml similarity index 100% rename from tests/samples/valid_opml_1_0.opml rename to opml_api/tests/samples/valid_opml_1_0.opml diff --git a/tests/samples/valid_opml_with_everything.opml b/opml_api/tests/samples/valid_opml_with_everything.opml similarity index 100% rename from tests/samples/valid_opml_with_everything.opml rename to opml_api/tests/samples/valid_opml_with_everything.opml diff --git a/tests/spec_samples.rs b/opml_api/tests/spec_samples.rs similarity index 100% rename from tests/spec_samples.rs rename to opml_api/tests/spec_samples.rs diff --git a/tests/spec_samples/LICENSE b/opml_api/tests/spec_samples/LICENSE similarity index 100% rename from tests/spec_samples/LICENSE rename to opml_api/tests/spec_samples/LICENSE diff --git a/tests/spec_samples/category.opml b/opml_api/tests/spec_samples/category.opml similarity index 100% rename from tests/spec_samples/category.opml rename to opml_api/tests/spec_samples/category.opml diff --git a/tests/spec_samples/directory.opml b/opml_api/tests/spec_samples/directory.opml similarity index 100% rename from tests/spec_samples/directory.opml rename to opml_api/tests/spec_samples/directory.opml diff --git a/tests/spec_samples/placesLived.opml b/opml_api/tests/spec_samples/placesLived.opml similarity index 100% rename from tests/spec_samples/placesLived.opml rename to opml_api/tests/spec_samples/placesLived.opml diff --git a/tests/spec_samples/simpleScript.opml b/opml_api/tests/spec_samples/simpleScript.opml similarity index 100% rename from tests/spec_samples/simpleScript.opml rename to opml_api/tests/spec_samples/simpleScript.opml diff --git a/tests/spec_samples/states.opml b/opml_api/tests/spec_samples/states.opml similarity index 100% rename from tests/spec_samples/states.opml rename to opml_api/tests/spec_samples/states.opml diff --git a/tests/spec_samples/subscriptionList.opml b/opml_api/tests/spec_samples/subscriptionList.opml similarity index 100% rename from tests/spec_samples/subscriptionList.opml rename to opml_api/tests/spec_samples/subscriptionList.opml diff --git a/tests/valid.rs b/opml_api/tests/valid.rs similarity index 100% rename from tests/valid.rs rename to opml_api/tests/valid.rs diff --git a/opml_cli/Cargo.toml b/opml_cli/Cargo.toml new file mode 100644 index 0000000..16e08c9 --- /dev/null +++ b/opml_cli/Cargo.toml @@ -0,0 +1,28 @@ +# https://doc.rust-lang.org/cargo/reference/manifest.html + +[package] +name = "opml_cli" +description = "An OPML parser for the command-line." +version = "0.3.0" +authors = ["Holllo "] +license = "MIT OR Apache-2.0" +repository = "https://github.com/Holllo/opml" +readme = "../README.md" +edition = "2018" +keywords = ["xml", "opml"] + +[[bin]] +name = "opml" +path = "source/main.rs" + +[dependencies] +clap = "2.33" +opml = { path = "../opml_api", version = "0.3" } +serde_json = "1.0" + +[dependencies.serde] +version = "1.0" +features = ["derive"] + +[dev-dependencies] +assert_cmd = "1.0" diff --git a/opml_cli/source/main.rs b/opml_cli/source/main.rs new file mode 100644 index 0000000..53103cc --- /dev/null +++ b/opml_cli/source/main.rs @@ -0,0 +1,110 @@ +use std::fs::read_to_string; + +use clap::{ + crate_authors, crate_description, crate_version, App, Arg, ArgGroup, +}; +use opml::{Outline, OPML}; + +fn main() { + let cli = App::new("OPML CLI") + .about(crate_description!()) + .author(crate_authors!()) + .version(crate_version!()) + .args(&[ + // Format flags. + Arg::with_name("json") + .long("json") + .long_help("Output the OPML as JSON.") + .takes_value(false), + Arg::with_name("json pretty") + .long("json-pretty") + .long_help("Output the OPML as pretty-printed JSON.") + .takes_value(false), + Arg::with_name("rss") + .long("rss") + .long_help( + "Only output the outline text and xmlUrl attributes \ + when both are present in the outline element.", + ) + .takes_value(false), + // Boolean flags. + Arg::with_name("verbose") + .long("verbose") + .long_help("Print extra information while running.") + .takes_value(false), + // Options that are only allowed once. + Arg::with_name("file") + .long("file") + .long_help("The OPML file to parse.") + .required(true) + .short("f") + .takes_value(true), + ]) + .group( + ArgGroup::with_name("format") + .args(&["json", "json pretty", "rss"]) + .required(true), + ) + .get_matches(); + + // Extract format flags. + let json = cli.is_present("json"); + let json_pretty = cli.is_present("json pretty"); + let rss = cli.is_present("rss"); + + // Extract boolean flags. + let verbose = cli.is_present("verbose"); + + // Extract the various options. + let file = cli.value_of("file").unwrap(); + + // Read the file to string. + let xml = read_to_string(file).expect("Failed to read OPML file"); + + // Parse the OPML from the read file. + let opml = OPML::new(&xml).expect("Failed to parse OPML file"); + + if rss { + // Get all the outlines from the OPML document. + let outlines = extract_all_outlines(&opml.body.outlines); + + // Print out the text and xmlUrl attributes when possible. + for outline in outlines { + if let Some(xml_url) = outline.xml_url { + println!("{}", outline.text); + println!("{}", xml_url); + } else if verbose { + println!( + "Skipping \"{}\" because it did not have an xmlUrl attribute.", + outline.text + ); + } + } + } else if json { + println!( + "{}", + serde_json::to_string(&opml).expect("Failed to convert OPML to JSON") + ); + } else if json_pretty { + println!( + "{}", + serde_json::to_string_pretty(&opml) + .expect("Failed to convert OPML to pretty JSON") + ); + } else { + unreachable!(); + } +} + +/// A helper function that takes in `opml::Outline` elements and returns all +/// children it can find in a single `Vec`. +pub fn extract_all_outlines(outlines: &[Outline]) -> Vec { + let mut accumulator = vec![]; + + for outline in outlines { + accumulator.push(outline.clone()); + accumulator.append(&mut extract_all_outlines(&outline.outlines)); + } + + accumulator +} diff --git a/opml_cli/tests/invalid.rs b/opml_cli/tests/invalid.rs new file mode 100644 index 0000000..06be6a2 --- /dev/null +++ b/opml_cli/tests/invalid.rs @@ -0,0 +1,27 @@ +use std::fs::read_to_string; + +use assert_cmd::Command; + +const SAMPLE: &str = "tests/samples/youtube.opml"; + +#[test] +fn test_missing_file() { + let mut cmd = Command::cargo_bin("opml").unwrap(); + let assert = cmd.args(&["--rss"]).assert(); + + assert + .failure() + .code(1) + .stderr(read_to_string("tests/snapshots/missing-file.txt").unwrap()); +} + +#[test] +fn test_missing_format() { + let mut cmd = Command::cargo_bin("opml").unwrap(); + let assert = cmd.args(&["--file", SAMPLE]).assert(); + + assert + .failure() + .code(1) + .stderr(read_to_string("tests/snapshots/missing-format.txt").unwrap()); +} diff --git a/opml_cli/tests/samples/youtube.opml b/opml_cli/tests/samples/youtube.opml new file mode 100644 index 0000000..96a17be --- /dev/null +++ b/opml_cli/tests/samples/youtube.opml @@ -0,0 +1,15 @@ + + + + + + + + diff --git a/opml_cli/tests/snapshots/json-pretty.json b/opml_cli/tests/snapshots/json-pretty.json new file mode 100644 index 0000000..4dd1971 --- /dev/null +++ b/opml_cli/tests/snapshots/json-pretty.json @@ -0,0 +1,57 @@ +{ + "version": "1.1", + "head": null, + "body": { + "outlines": [ + { + "text": "YouTube Subscriptions", + "type": null, + "is_comment": null, + "is_breakpoint": null, + "created": null, + "category": null, + "outlines": [ + { + "text": "A YouTube Channel", + "type": "rss", + "is_comment": null, + "is_breakpoint": null, + "created": null, + "category": null, + "outlines": [], + "xml_url": "https://www.youtube.com/feeds/videos.xml?channel_id=abcdefghijklmnopqrstuvwxyz1", + "description": null, + "html_url": null, + "language": null, + "title": "A YouTube Channel", + "version": null, + "url": null + }, + { + "text": "Another YouTube Channel", + "type": "rss", + "is_comment": null, + "is_breakpoint": null, + "created": null, + "category": null, + "outlines": [], + "xml_url": "https://www.youtube.com/feeds/videos.xml?channel_id=abcdefghijklmnopqrstuvwxyz2", + "description": null, + "html_url": null, + "language": null, + "title": "Another YouTube Channel", + "version": null, + "url": null + } + ], + "xml_url": null, + "description": null, + "html_url": null, + "language": null, + "title": "YouTube Subscriptions", + "version": null, + "url": null + } + ] + } +} diff --git a/opml_cli/tests/snapshots/json.json b/opml_cli/tests/snapshots/json.json new file mode 100644 index 0000000..993e28c --- /dev/null +++ b/opml_cli/tests/snapshots/json.json @@ -0,0 +1 @@ +{"version":"1.1","head":null,"body":{"outlines":[{"text":"YouTube Subscriptions","type":null,"is_comment":null,"is_breakpoint":null,"created":null,"category":null,"outlines":[{"text":"A YouTube Channel","type":"rss","is_comment":null,"is_breakpoint":null,"created":null,"category":null,"outlines":[],"xml_url":"https://www.youtube.com/feeds/videos.xml?channel_id=abcdefghijklmnopqrstuvwxyz1","description":null,"html_url":null,"language":null,"title":"A YouTube Channel","version":null,"url":null},{"text":"Another YouTube Channel","type":"rss","is_comment":null,"is_breakpoint":null,"created":null,"category":null,"outlines":[],"xml_url":"https://www.youtube.com/feeds/videos.xml?channel_id=abcdefghijklmnopqrstuvwxyz2","description":null,"html_url":null,"language":null,"title":"Another YouTube Channel","version":null,"url":null}],"xml_url":null,"description":null,"html_url":null,"language":null,"title":"YouTube Subscriptions","version":null,"url":null}]}} diff --git a/opml_cli/tests/snapshots/missing-file.txt b/opml_cli/tests/snapshots/missing-file.txt new file mode 100644 index 0000000..22f0c2c --- /dev/null +++ b/opml_cli/tests/snapshots/missing-file.txt @@ -0,0 +1,7 @@ +error: The following required arguments were not provided: + --file + +USAGE: + opml --file <--json|--json-pretty|--rss> + +For more information try --help diff --git a/opml_cli/tests/snapshots/missing-format.txt b/opml_cli/tests/snapshots/missing-format.txt new file mode 100644 index 0000000..de0ec80 --- /dev/null +++ b/opml_cli/tests/snapshots/missing-format.txt @@ -0,0 +1,7 @@ +error: The following required arguments were not provided: + <--json|--json-pretty|--rss> + +USAGE: + opml [FLAGS] --file <--json|--json-pretty|--rss> + +For more information try --help diff --git a/opml_cli/tests/snapshots/rss.txt b/opml_cli/tests/snapshots/rss.txt new file mode 100644 index 0000000..0bc2bb0 --- /dev/null +++ b/opml_cli/tests/snapshots/rss.txt @@ -0,0 +1,4 @@ +A YouTube Channel +https://www.youtube.com/feeds/videos.xml?channel_id=abcdefghijklmnopqrstuvwxyz1 +Another YouTube Channel +https://www.youtube.com/feeds/videos.xml?channel_id=abcdefghijklmnopqrstuvwxyz2 diff --git a/opml_cli/tests/valid.rs b/opml_cli/tests/valid.rs new file mode 100644 index 0000000..1012d9c --- /dev/null +++ b/opml_cli/tests/valid.rs @@ -0,0 +1,38 @@ +use std::fs::read_to_string; + +use assert_cmd::Command; + +const SAMPLE: &str = "tests/samples/youtube.opml"; + +#[test] +fn test_valid_rss() { + let mut cmd = Command::cargo_bin("opml").unwrap(); + let assert = cmd.args(&["--file", SAMPLE, "--rss"]).assert(); + + assert + .success() + .code(0) + .stdout(read_to_string("tests/snapshots/rss.txt").unwrap()); +} + +#[test] +fn test_valid_json() { + let mut cmd = Command::cargo_bin("opml").unwrap(); + let assert = cmd.args(&["--file", SAMPLE, "--json"]).assert(); + + assert + .success() + .code(0) + .stdout(read_to_string("tests/snapshots/json.json").unwrap()); +} + +#[test] +fn test_valid_json_pretty() { + let mut cmd = Command::cargo_bin("opml").unwrap(); + let assert = cmd.args(&["--file", SAMPLE, "--json-pretty"]).assert(); + + assert + .success() + .code(0) + .stdout(read_to_string("tests/snapshots/json-pretty.json").unwrap()); +} diff --git a/rustfmt.toml b/rustfmt.toml deleted file mode 100644 index 4c1eefa..0000000 --- a/rustfmt.toml +++ /dev/null @@ -1,2 +0,0 @@ -max_width = 80 -tab_spaces = 2 diff --git a/source/lib.rs b/source/lib.rs deleted file mode 100644 index eba0691..0000000 --- a/source/lib.rs +++ /dev/null @@ -1,363 +0,0 @@ -//! This crate provides an API to parse and construct [OPML documents](http://dev.opml.org/spec2.html) to and from regular Rust structs. -//! -//! ## Getting Started -//! -//! ### Parsing -//! -//! Parsing XML into [an OPML struct](struct.OPML.html) can be done with [`OPML::new()`](struct.OPML.html#method.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](struct.Body.html) element contains no child [Outline](struct.Outline.html) elements, [as per the spec](http://dev.opml.org/spec2.html#whatIsALtbodygt). -//! -//! ```rust -//! use opml::{OPML, Outline}; -//! -//! let xml = r#""#; -//! 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](struct.OPML.html) with [`OPML::default()`](struct.OPML.html#method.default), add anything wanted and then call [`OPML::to_xml()`](struct.OPML.html#method.to_xml) to return the XML as a string. -//! -//! ```rust -//! 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#"Rust Feeds"#; -//! println!("{}", xml); -//! assert_eq!(xml, expected); -//! ``` -//! -//! ## CLI -//! -//! Looking for a command-line utility to parse OPML documents? Check out [the `opml-cli`](https://git.holllo.cc/Holllo/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](https://git.holllo.cc/Holllo/opml/src/branch/main/LICENSE-Apache) (http://www.apache.org/licenses/LICENSE-2.0) -//! * [MIT license](https://git.holllo.cc/Holllo/opml/src/branch/main/LICENSE-MIT) (http://opensource.org/licenses/MIT) -//! -//! at your option. -//! -//! The samples [located in `tests/spec_samples`](https://git.holllo.cc/Holllo/opml/src/branch/main/tests/spec_samples) were [taken from the OPML 2.0 spec](http://dev.opml.org/spec2.html#examples) and are subject to [their own license](https://git.holllo.cc/Holllo/opml/src/branch/main/tests/spec_samples/LICENSE). - -use regex::Regex; -use serde::{Deserialize, Serialize}; -use strong_xml::{XmlError, XmlRead, XmlWrite}; - -/// The top-level [OPML](struct.OPML.html) element. -#[derive(XmlWrite, XmlRead, PartialEq, Debug, Clone, Serialize, Deserialize)] -#[xml(tag = "opml")] -pub struct OPML { - /// The version attribute from the element, valid values are `1.0`, `1.1` and `2.0`. - #[xml(attr = "version")] - pub version: String, - - /// The [Head](struct.Head.html) child element. Contains the metadata of the OPML document. - #[xml(child = "head")] - pub head: Option, - - /// The [Body](struct.Body.html) child element. Contains all the [Outlines](struct.Outline.html). - #[xml(child = "body")] - pub body: Body, -} - -impl OPML { - /// Parses an OPML document. - /// - /// # Example - /// - /// ```rust - /// use opml::{OPML, Outline}; - /// - /// let xml = r#""#; - /// let parsed = OPML::new(xml).unwrap(); - /// - /// let mut expected = OPML::default(); - /// expected.body.outlines.push(Outline { - /// text: "Outline".to_string(), - /// ..Outline::default() - /// }); - /// - /// assert_eq!(parsed, expected); - /// ``` - pub fn new(xml: &str) -> Result { - let opml: Result = OPML::from_str(xml); - - let opml = match opml { - Ok(value) => value, - Err(err) => return Err(format!("XML parsing error: {:#?}", err)), - }; - - let version = &opml.version; - - // SPEC: The version attribute is a version string, of the form, x.y, where x and y are both numeric strings. - let valid_version_regex = Regex::new(r"^\d+\.\d+$").unwrap(); - let valid_versions = vec!["1.0", "1.1", "2.0"]; - - if !valid_version_regex.is_match(version) - || !valid_versions.contains(&version.as_str()) - { - return Err(format!( - "Unsupported OPML version detected: {}", - opml.version - )); - } - - // SPEC: A `` contains one or more `` elements. - if opml.body.outlines.is_empty() { - return Err("OPML body has no outlines.".to_string()); - } - - Ok(opml) - } - - /// Helper function to add an [Outline](struct.Outline.html) element with `text` and `xml_url` attributes to the [Body](struct.Body.html). Useful for creating feed lists quickly. This function [also exists on the Outline struct](struct.Outline.html#method.add_feed) to create grouped lists easily. - /// - /// # 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 - } - - /// Converts the struct to an XML document. - /// - /// # Example - /// - /// ```rust - /// use opml::OPML; - /// - /// let opml = OPML::default(); - /// let xml = opml.to_xml().unwrap(); - /// - /// let expected = r#""#; - /// assert_eq!(xml, expected); - /// ``` - pub fn to_xml(&self) -> Result { - let result: Result = self.to_string(); - - match result { - Ok(value) => Ok(value), - Err(err) => Err(format!("XML writing error: {:#?}", err)), - } - } -} - -impl Default for OPML { - fn default() -> Self { - OPML { - version: "2.0".to_string(), - head: Some(Head::default()), - body: Body::default(), - } - } -} - -/// The [Head](struct.Head.html) child element of [OPML](struct.OPML.html). -/// Contains the metadata of the OPML document. -#[derive( - XmlWrite, XmlRead, PartialEq, Debug, Clone, Default, Serialize, Deserialize, -)] -#[xml(tag = "head")] -pub struct Head { - /// The title of the document. - #[xml(flatten_text = "title")] - pub title: Option, - - /// A date-time (RFC822) indicating when the document was created. - #[xml(flatten_text = "dateCreated")] - pub date_created: Option, - - /// A date-time (RFC822) indicating when the document was last modified. - #[xml(flatten_text = "dateModified")] - pub date_modified: Option, - - /// The name of the document owner. - #[xml(flatten_text = "ownerName")] - pub owner_name: Option, - - /// The email address of the document owner. - #[xml(flatten_text = "ownerEmail")] - pub owner_email: Option, - - /// A link to the website of the document owner. - #[xml(flatten_text = "ownerId")] - pub owner_id: Option, - - /// A link to the documentation of the OPML format used for this document. - #[xml(flatten_text = "docs")] - pub docs: Option, - - /// 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. - #[xml(flatten_text = "expansionState")] - pub expansion_state: Option, - - /// 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. - #[xml(flatten_text = "vertScrollState")] - pub vert_scroll_state: Option, - - /// The pixel location of the top edge of the window. - #[xml(flatten_text = "windowTop")] - pub window_top: Option, - - /// The pixel location of the left edge of the window. - #[xml(flatten_text = "windowLeft")] - pub window_left: Option, - - /// The pixel location of the bottom edge of the window. - #[xml(flatten_text = "windowBottom")] - pub window_bottom: Option, - - /// The pixel location of the right edge of the window. - #[xml(flatten_text = "windowRight")] - pub window_right: Option, -} - -/// The [Body](struct.Body.html) child element of [OPML](struct.OPML.html). Contains all the [Outlines](struct.Outline.html). -#[derive( - XmlWrite, XmlRead, PartialEq, Debug, Clone, Default, Serialize, Deserialize, -)] -#[xml(tag = "body")] -pub struct Body { - /// All the top-level [Outline](struct.Outline.html) elements. - #[xml(child = "outline")] - pub outlines: Vec, -} - -/// The [Outline](struct.Outline.html) element. -#[derive( - XmlWrite, XmlRead, PartialEq, Debug, Clone, Default, Serialize, Deserialize, -)] -#[xml(tag = "outline")] -pub struct Outline { - /// 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. - /// Text attributes may contain encoded HTML markup. - #[xml(default, attr = "text")] - pub text: String, - - /// A string that indicates how the other attributes of the [Outline](struct.Outline.html) should be interpreted. - #[xml(attr = "type")] - pub r#type: Option, - - /// Indicating whether the outline is commented or not. By convention if an outline is commented, all subordinate outlines are considered to also be commented. - #[xml(attr = "isComment")] - pub is_comment: Option, - - /// Indicating whether a breakpoint is set on this outline. This attribute is mainly necessary for outlines used to edit scripts. - #[xml(attr = "isBreakpoint")] - pub is_breakpoint: Option, - - /// The date-time (RFC822) that this [Outline](struct.Outline.html) element was created. - #[xml(attr = "created")] - pub created: Option, - - /// A string of comma-separated slash-delimited category strings, in the format defined by the [RSS 2.0 category](http://cyber.law.harvard.edu/rss/rss.html#ltcategorygtSubelementOfLtitemgt) element. To represent a "tag," the category string should contain no slashes. - #[xml(attr = "category")] - pub category: Option, - - /// Child [Outline](struct.Outline.html) elements of the current one. - #[xml(child = "outline")] - pub outlines: Vec, - - /// The HTTP address of the feed. - #[xml(attr = "xmlUrl")] - pub xml_url: Option, - - /// The top-level description element from the feed. - #[xml(attr = "description")] - pub description: Option, - - /// The top-level link element from the feed. - #[xml(attr = "htmlUrl")] - pub html_url: Option, - - /// The top-level language element from the feed. - #[xml(attr = "language")] - pub language: Option, - - /// The top-level title element from the feed. - #[xml(attr = "title")] - pub title: Option, - - /// The version of the feed's format (such as RSS 0.91, 2.0, ...). - #[xml(attr = "version")] - pub version: Option, - - /// A link that can point to another OPML document or to something that can be displayed in a web browser. - #[xml(attr = "url")] - pub url: Option, -} - -impl Outline { - /// Helper function to add an [Outline](struct.Outline.html) element with `text` and `xml_url` attributes as a child element. Useful for creating grouped lists. This function [also exists on the OPML struct](struct.OPML.html#method.add_feed) for non-grouped lists. - /// - /// # 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 - } -}