diff --git a/examples/rss.rs b/examples/rss.rs index df01874..a808b25 100644 --- a/examples/rss.rs +++ b/examples/rss.rs @@ -7,7 +7,8 @@ fn main() -> Result<(), Box> { let xml = fs::read_to_string("examples/opml_samples/rust_feeds.opml")?; let subscriptions = OPML::new(&xml)?; - let title = subscriptions.head.title.unwrap(); + let head = subscriptions.head.unwrap(); + let title = head.title.unwrap(); println!(" {}", title); println!(" {}", "─".repeat(title.len())); diff --git a/source/lib.rs b/source/lib.rs index b69de0e..a9c610f 100644 --- a/source/lib.rs +++ b/source/lib.rs @@ -7,7 +7,7 @@ //! ```rust //! use opml::OPML; //! -//! let xml = r#""#; +//! let xml = r#""#; //! let parsed = OPML::new(xml).unwrap(); //! //! println!("{:#?}", parsed); @@ -16,7 +16,7 @@ //! ### Constructing //! //! ```rust -//! use opml::OPML; +//! use opml::{Head, OPML}; //! //! let mut opml = OPML::default(); //! opml @@ -24,9 +24,11 @@ //! .add_feed( //! "Inside Rust", //! "https://blog.rust-lang.org/inside-rust/feed.xml", -//! ); -//! -//! opml.head.title = Some("Rust Feeds".to_string()); +//! ) +//! .set_head(Head { +//! title: Some("Rust Feeds".to_string()), +//! ..Head::default() +//! }); //! //! let xml = opml.to_xml().unwrap(); //! println!("{}", xml); @@ -69,7 +71,7 @@ pub struct OPML { /// The `` child element. Contains the metadata of the OPML document. #[xml(child = "head")] - pub head: Head, + pub head: Option, /// The `` child element. Contains all the ``. #[xml(child = "body")] @@ -117,6 +119,12 @@ impl OPML { self } + pub fn set_head(&mut self, head: Head) -> &mut Self { + self.head = Some(head); + + self + } + pub fn to_xml(&self) -> Result { let result: Result = self.to_string(); @@ -131,7 +139,7 @@ impl Default for OPML { fn default() -> Self { OPML { version: "2.0".to_string(), - head: Head::default(), + head: Some(Head::default()), body: Body::default(), } } diff --git a/tests/construction.rs b/tests/construction.rs index 2aaca1a..5468f9c 100644 --- a/tests/construction.rs +++ b/tests/construction.rs @@ -11,9 +11,11 @@ fn test_opml_construction_1() -> Result<(), Box> { .add_feed( "Inside Rust", "https://blog.rust-lang.org/inside-rust/feed.xml", - ); - - opml.head.title = Some("Rust Feeds".to_string()); + ) + .set_head(Head { + title: Some("Rust Feeds".to_string()), + ..Head::default() + }); let actual = opml.to_xml().unwrap(); let expected = read("tests/samples/construction_1.opml")?; @@ -26,6 +28,10 @@ fn test_opml_construction_1() -> Result<(), Box> { #[test] fn test_opml_construction_2() -> Result<(), Box> { let mut opml = OPML::default(); + opml.set_head(Head { + title: Some("Rust Feeds".to_string()), + ..Head::default() + }); let mut rust_group = Outline::default(); rust_group.text = "Rust Feeds".to_string(); @@ -44,7 +50,6 @@ fn test_opml_construction_2() -> Result<(), Box> { opml.body.outlines.push(rust_group); opml.body.outlines.push(mozilla_group); - opml.head.title = Some("Rust Feeds".to_string()); let actual = opml.to_xml().unwrap(); let expected = read("tests/samples/construction_2.opml")?; diff --git a/tests/valid.rs b/tests/valid.rs index ea9ba16..6e2d7b2 100644 --- a/tests/valid.rs +++ b/tests/valid.rs @@ -8,7 +8,7 @@ fn test_minimum_valid_opml() { OPML::new(&read("tests/samples/minimum_valid_opml.opml").unwrap()).unwrap(), OPML { version: "2.0".to_string(), - head: Head::default(), + head: Some(Head::default()), body: Body { outlines: vec![Outline { text: "Outline Text".to_string(), @@ -26,7 +26,7 @@ fn test_valid_opml_with_everything() { .unwrap(), OPML { version: "2.0".to_string(), - head: Head { + head: Some(Head { title: Some("Title".to_string()), date_created: Some("Date Created".to_string()), date_modified: Some("Date Modified".to_string()), @@ -40,7 +40,7 @@ fn test_valid_opml_with_everything() { window_left: Some(2), window_bottom: Some(3), window_right: Some(4), - }, + }), body: Body { outlines: vec![Outline { text: "Outline Text".to_string(),