From cc51208a4878f25cae29edd56428b2988b45d5ff Mon Sep 17 00:00:00 2001 From: Hudson Shykowski Date: Mon, 12 Apr 2021 12:04:38 -0600 Subject: [PATCH] Implement .into_inner() for Json (#1816) * Implement .into_inner() for Json This allows users to use .into_inner() the same way it would be possible in the serde crate. Previously, it was only possible via dump.0 (which looks gross), or Json(dump). * Fix an error using HtmlBlock The definition uses {content, brace} but the usage used [brace, content} * Update packages/yew/src/format/json.rs Co-authored-by: Simon --- .../yew-macro/src/html_tree/html_block.rs | 2 +- packages/yew/src/format/json.rs | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/yew-macro/src/html_tree/html_block.rs b/packages/yew-macro/src/html_tree/html_block.rs index 07bbab041..bc7b8f9a8 100644 --- a/packages/yew-macro/src/html_tree/html_block.rs +++ b/packages/yew-macro/src/html_tree/html_block.rs @@ -32,7 +32,7 @@ impl Parse for HtmlBlock { BlockContent::Node(Box::new(content.parse()?)) }; - Ok(HtmlBlock { brace, content }) + Ok(HtmlBlock { content, brace }) } } diff --git a/packages/yew/src/format/json.rs b/packages/yew/src/format/json.rs index ce9f0a826..4c386b0ed 100644 --- a/packages/yew/src/format/json.rs +++ b/packages/yew/src/format/json.rs @@ -11,6 +11,8 @@ /// /// // Converts JSON string to a data (lazy). /// let Json(data) = dump; +/// // Or another way to convert JSON string to data: +/// let data = dump.into_inner(); /// ``` #[derive(Debug)] pub struct Json(pub T); @@ -19,6 +21,14 @@ text_format!(Json based on serde_json); binary_format!(Json based on serde_json); +impl Json { + /// Consumes the JSON wrapper and returns the wrapped item. + #[inline(always)] + pub fn into_inner(self) -> T { + self.0 + } +} + #[cfg(test)] mod tests { use super::*; @@ -41,4 +51,16 @@ mod tests { let _stored: Text = Json(&data).into(); let _stored: Binary = Json(&data).into(); } + + #[test] + fn test_into_inner() { + #[derive(Serialize, Deserialize)] + struct Data { + value: u8, + } + + let data: Json> = Json::from(Ok(r#"{"value": 123}"#.to_string())); + let data = data.into_inner().unwrap(); + assert_eq!(data.value, 123); + } }