Fix some things and do some cleanup.

This commit is contained in:
Bauke 2021-03-23 13:02:16 +01:00
parent 88d96dea73
commit e8006bff09
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
7 changed files with 31 additions and 27 deletions

View File

@ -1,12 +1,11 @@
use std::error::Error; use std::{error::Error, fs};
use std::fs;
use opml::OPML; use opml::OPML;
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let xml = fs::read_to_string("examples/opml_samples/rust_feeds.opml")?; let xml = fs::read_to_string("examples/opml_samples/rust_feeds.opml")?;
let subscriptions = OPML::new(&xml)?; let subscriptions = OPML::from_str(&xml)?;
let head = subscriptions.head.unwrap(); let head = subscriptions.head.unwrap();
let title = head.title.unwrap(); let title = head.title.unwrap();

View File

@ -119,7 +119,7 @@ impl OPML {
/// ``` /// ```
#[deprecated(note = "use from_str instead", since = "1.1.0")] #[deprecated(note = "use from_str instead", since = "1.1.0")]
pub fn new(xml: &str) -> Result<Self, Error> { pub fn new(xml: &str) -> Result<Self, Error> {
Self::from_str(xml) Self::from_str(xml).map_err(Into::into)
} }
/// Parses an OPML document. /// Parses an OPML document.
@ -140,6 +140,7 @@ impl OPML {
/// ///
/// assert_eq!(parsed, expected); /// assert_eq!(parsed, expected);
/// ``` /// ```
#[allow(clippy::should_implement_trait)]
pub fn from_str(xml: &str) -> Result<Self, Error> { pub fn from_str(xml: &str) -> Result<Self, Error> {
let opml = <OPML as XmlRead>::from_str(xml)?; let opml = <OPML as XmlRead>::from_str(xml)?;
@ -167,12 +168,12 @@ impl OPML {
/// ///
/// # Example /// # Example
/// ///
/// ```rust,norun /// ```rust,no_run
/// use opml::{OPML, Outline}; /// use opml::{OPML, Outline};
/// use std::file::File; /// use std::fs::File;
/// ///
/// let file = File::open("opml.xml").unwrap(); /// let mut file = File::open("file.opml").unwrap();
/// let parsed = OPML::from_reader(file).unwrap(); /// let parsed = OPML::from_reader(&mut file).unwrap();
/// ``` /// ```
pub fn from_reader<R>(reader: &mut R) -> Result<Self, Error> pub fn from_reader<R>(reader: &mut R) -> Result<Self, Error>
where where
@ -180,7 +181,7 @@ impl OPML {
{ {
let mut s = String::new(); let mut s = String::new();
reader.read_to_string(&mut s)?; reader.read_to_string(&mut s)?;
Self::from_str(&s) Self::from_str(&s).map_err(Into::into)
} }
/// 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. /// 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.
@ -251,13 +252,13 @@ impl OPML {
/// ///
/// # Example /// # Example
/// ///
/// ```rust,norun /// ```rust,no_run
/// use opml::OPML; /// use opml::OPML;
/// use std::file::File; /// use std::fs::File;
/// ///
/// let opml = OPML::default(); /// let opml = OPML::default();
/// let file = File::create("opml.xml").unwrap(); /// let mut file = File::create("file.opml").unwrap();
/// let xml = opml.to_writer(file).unwrap(); /// let xml = opml.to_writer(&mut file).unwrap();
/// ``` /// ```
pub fn to_writer<W>(&self, writer: &mut W) -> Result<(), Error> pub fn to_writer<W>(&self, writer: &mut W) -> Result<(), Error>
where where

View File

@ -17,7 +17,7 @@ fn test_opml_construction_1() -> Result<(), Box<dyn Error>> {
..Head::default() ..Head::default()
}); });
let actual = opml.to_xml().unwrap(); let actual = opml.to_string().unwrap();
let expected = read("tests/samples/construction_1.opml")?; let expected = read("tests/samples/construction_1.opml")?;
assert_eq!(actual.trim(), expected.trim()); assert_eq!(actual.trim(), expected.trim());
@ -52,7 +52,7 @@ fn test_opml_construction_2() -> Result<(), Box<dyn Error>> {
opml.body.outlines.push(rust_group); opml.body.outlines.push(rust_group);
opml.body.outlines.push(mozilla_group); opml.body.outlines.push(mozilla_group);
let actual = opml.to_xml().unwrap(); let actual = opml.to_string().unwrap();
let expected = read("tests/samples/construction_2.opml")?; let expected = read("tests/samples/construction_2.opml")?;
assert_eq!(actual.trim(), expected.trim()); assert_eq!(actual.trim(), expected.trim());

View File

@ -1,23 +1,24 @@
use opml::*;
use std::fs::read_to_string as read; use std::fs::read_to_string as read;
use opml::*;
#[test] #[test]
#[should_panic] #[should_panic]
fn test_invalid_xml() { fn test_invalid_xml() {
let sample = read("tests/samples/invalid_xml.opml").unwrap(); let sample = read("tests/samples/invalid_xml.opml").unwrap();
OPML::new(sample.as_str()).unwrap(); OPML::from_str(&sample).unwrap();
} }
#[test] #[test]
fn test_invalid_opml_version() { fn test_invalid_opml_version() {
let sample = read("tests/samples/invalid_opml_version.opml").unwrap(); let sample = read("tests/samples/invalid_opml_version.opml").unwrap();
let res = OPML::new(sample.as_str()); let res = OPML::from_str(&sample);
assert!(matches!(res, Err(Error::UnsupportedVersion(e)) if e == "invalid")); assert!(matches!(res, Err(Error::UnsupportedVersion(e)) if e == "invalid"));
} }
#[test] #[test]
fn test_invalid_opml_no_outlines() { fn test_invalid_opml_no_outlines() {
let sample = read("tests/samples/invalid_opml_no_outlines.opml").unwrap(); let sample = read("tests/samples/invalid_opml_no_outlines.opml").unwrap();
let res = OPML::new(sample.as_str()); let res = OPML::from_str(&sample);
assert!(matches!(res, Err(Error::BodyHasNoOutlines))); assert!(matches!(res, Err(Error::BodyHasNoOutlines)));
} }

View File

@ -1,5 +1,4 @@
use std::error::Error; use std::{error::Error, fs};
use std::fs;
use opml::*; use opml::*;
@ -16,7 +15,7 @@ pub fn test_spec_samples() -> Result<(), Box<dyn Error>> {
for sample in samples { for sample in samples {
let sample_content = fs::read_to_string(&sample)?; let sample_content = fs::read_to_string(&sample)?;
OPML::new(sample_content.as_str())?; OPML::from_str(&sample_content)?;
} }
Ok(()) Ok(())

View File

@ -5,7 +5,8 @@ use opml::*;
#[test] #[test]
fn test_minimum_valid_opml() { fn test_minimum_valid_opml() {
assert_eq!( assert_eq!(
OPML::new(&read("tests/samples/minimum_valid_opml.opml").unwrap()).unwrap(), OPML::from_str(&read("tests/samples/minimum_valid_opml.opml").unwrap())
.unwrap(),
OPML { OPML {
version: "2.0".to_string(), version: "2.0".to_string(),
head: Some(Head::default()), head: Some(Head::default()),
@ -22,7 +23,9 @@ fn test_minimum_valid_opml() {
#[test] #[test]
fn test_valid_opml_with_everything() { fn test_valid_opml_with_everything() {
assert_eq!( assert_eq!(
OPML::new(&read("tests/samples/valid_opml_with_everything.opml").unwrap()) OPML::from_str(
&read("tests/samples/valid_opml_with_everything.opml").unwrap()
)
.unwrap(), .unwrap(),
OPML { OPML {
version: "2.0".to_string(), version: "2.0".to_string(),
@ -81,7 +84,8 @@ fn test_valid_opml_with_everything() {
#[test] #[test]
fn test_valid_opml_1_0() { fn test_valid_opml_1_0() {
assert_eq!( assert_eq!(
OPML::new(&read("tests/samples/valid_opml_1_0.opml").unwrap()).unwrap(), OPML::from_str(&read("tests/samples/valid_opml_1_0.opml").unwrap())
.unwrap(),
OPML { OPML {
version: "1.0".to_string(), version: "1.0".to_string(),
head: Some(Head::default()), head: Some(Head::default()),

View File

@ -62,7 +62,7 @@ fn main() {
let xml = read_to_string(file).expect("Failed to read OPML file"); let xml = read_to_string(file).expect("Failed to read OPML file");
// Parse the OPML from the read file. // Parse the OPML from the read file.
let opml = OPML::new(&xml).expect("Failed to parse OPML file"); let opml = OPML::from_str(&xml).expect("Failed to parse OPML file");
if rss { if rss {
// Get all the outlines from the OPML document. // Get all the outlines from the OPML document.