From e8006bff092d6ef2a003c9a6626a62fefb318144 Mon Sep 17 00:00:00 2001 From: Bauke Date: Tue, 23 Mar 2021 13:02:16 +0100 Subject: [PATCH] Fix some things and do some cleanup. --- opml_api/examples/rss.rs | 5 ++--- opml_api/source/lib.rs | 21 +++++++++++---------- opml_api/tests/construction.rs | 4 ++-- opml_api/tests/errors.rs | 9 +++++---- opml_api/tests/spec_samples.rs | 5 ++--- opml_api/tests/valid.rs | 12 ++++++++---- opml_cli/source/main.rs | 2 +- 7 files changed, 31 insertions(+), 27 deletions(-) diff --git a/opml_api/examples/rss.rs b/opml_api/examples/rss.rs index a808b25..9b88cbc 100644 --- a/opml_api/examples/rss.rs +++ b/opml_api/examples/rss.rs @@ -1,12 +1,11 @@ -use std::error::Error; -use std::fs; +use std::{error::Error, fs}; use opml::OPML; fn main() -> Result<(), Box> { 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 title = head.title.unwrap(); diff --git a/opml_api/source/lib.rs b/opml_api/source/lib.rs index 146aa86..0826f96 100644 --- a/opml_api/source/lib.rs +++ b/opml_api/source/lib.rs @@ -119,7 +119,7 @@ impl OPML { /// ``` #[deprecated(note = "use from_str instead", since = "1.1.0")] pub fn new(xml: &str) -> Result { - Self::from_str(xml) + Self::from_str(xml).map_err(Into::into) } /// Parses an OPML document. @@ -140,6 +140,7 @@ impl OPML { /// /// assert_eq!(parsed, expected); /// ``` + #[allow(clippy::should_implement_trait)] pub fn from_str(xml: &str) -> Result { let opml = ::from_str(xml)?; @@ -167,12 +168,12 @@ impl OPML { /// /// # Example /// - /// ```rust,norun + /// ```rust,no_run /// use opml::{OPML, Outline}; - /// use std::file::File; + /// use std::fs::File; /// - /// let file = File::open("opml.xml").unwrap(); - /// let parsed = OPML::from_reader(file).unwrap(); + /// let mut file = File::open("file.opml").unwrap(); + /// let parsed = OPML::from_reader(&mut file).unwrap(); /// ``` pub fn from_reader(reader: &mut R) -> Result where @@ -180,7 +181,7 @@ impl OPML { { let mut s = String::new(); 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. @@ -251,13 +252,13 @@ impl OPML { /// /// # Example /// - /// ```rust,norun + /// ```rust,no_run /// use opml::OPML; - /// use std::file::File; + /// use std::fs::File; /// /// let opml = OPML::default(); - /// let file = File::create("opml.xml").unwrap(); - /// let xml = opml.to_writer(file).unwrap(); + /// let mut file = File::create("file.opml").unwrap(); + /// let xml = opml.to_writer(&mut file).unwrap(); /// ``` pub fn to_writer(&self, writer: &mut W) -> Result<(), Error> where diff --git a/opml_api/tests/construction.rs b/opml_api/tests/construction.rs index b1d2627..0af69ad 100644 --- a/opml_api/tests/construction.rs +++ b/opml_api/tests/construction.rs @@ -17,7 +17,7 @@ fn test_opml_construction_1() -> Result<(), Box> { ..Head::default() }); - let actual = opml.to_xml().unwrap(); + let actual = opml.to_string().unwrap(); let expected = read("tests/samples/construction_1.opml")?; assert_eq!(actual.trim(), expected.trim()); @@ -52,7 +52,7 @@ fn test_opml_construction_2() -> Result<(), Box> { opml.body.outlines.push(rust_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")?; assert_eq!(actual.trim(), expected.trim()); diff --git a/opml_api/tests/errors.rs b/opml_api/tests/errors.rs index b2fa0c8..4033c10 100644 --- a/opml_api/tests/errors.rs +++ b/opml_api/tests/errors.rs @@ -1,23 +1,24 @@ -use opml::*; use std::fs::read_to_string as read; +use opml::*; + #[test] #[should_panic] fn test_invalid_xml() { let sample = read("tests/samples/invalid_xml.opml").unwrap(); - OPML::new(sample.as_str()).unwrap(); + OPML::from_str(&sample).unwrap(); } #[test] fn test_invalid_opml_version() { 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")); } #[test] fn test_invalid_opml_no_outlines() { 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))); } diff --git a/opml_api/tests/spec_samples.rs b/opml_api/tests/spec_samples.rs index bc76bdd..ed48722 100644 --- a/opml_api/tests/spec_samples.rs +++ b/opml_api/tests/spec_samples.rs @@ -1,5 +1,4 @@ -use std::error::Error; -use std::fs; +use std::{error::Error, fs}; use opml::*; @@ -16,7 +15,7 @@ pub fn test_spec_samples() -> Result<(), Box> { for sample in samples { let sample_content = fs::read_to_string(&sample)?; - OPML::new(sample_content.as_str())?; + OPML::from_str(&sample_content)?; } Ok(()) diff --git a/opml_api/tests/valid.rs b/opml_api/tests/valid.rs index 27e559a..dc9d2be 100644 --- a/opml_api/tests/valid.rs +++ b/opml_api/tests/valid.rs @@ -5,7 +5,8 @@ use opml::*; #[test] fn test_minimum_valid_opml() { 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 { version: "2.0".to_string(), head: Some(Head::default()), @@ -22,8 +23,10 @@ fn test_minimum_valid_opml() { #[test] fn test_valid_opml_with_everything() { assert_eq!( - OPML::new(&read("tests/samples/valid_opml_with_everything.opml").unwrap()) - .unwrap(), + OPML::from_str( + &read("tests/samples/valid_opml_with_everything.opml").unwrap() + ) + .unwrap(), OPML { version: "2.0".to_string(), head: Some(Head { @@ -81,7 +84,8 @@ fn test_valid_opml_with_everything() { #[test] fn test_valid_opml_1_0() { 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 { version: "1.0".to_string(), head: Some(Head::default()), diff --git a/opml_cli/source/main.rs b/opml_cli/source/main.rs index 53103cc..7f9779b 100644 --- a/opml_cli/source/main.rs +++ b/opml_cli/source/main.rs @@ -62,7 +62,7 @@ fn main() { 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"); + let opml = OPML::from_str(&xml).expect("Failed to parse OPML file"); if rss { // Get all the outlines from the OPML document.