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 <simon@siku2.io>
This commit is contained in:
Hudson Shykowski 2021-04-12 12:04:38 -06:00 committed by GitHub
parent e5dca45257
commit cc51208a48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -32,7 +32,7 @@ impl Parse for HtmlBlock {
BlockContent::Node(Box::new(content.parse()?))
};
Ok(HtmlBlock { brace, content })
Ok(HtmlBlock { content, brace })
}
}

View File

@ -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<T>(pub T);
@ -19,6 +21,14 @@ text_format!(Json based on serde_json);
binary_format!(Json based on serde_json);
impl<T> Json<T> {
/// 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<Result<Data, _>> = Json::from(Ok(r#"{"value": 123}"#.to_string()));
let data = data.into_inner().unwrap();
assert_eq!(data.value, 123);
}
}