mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
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:
parent
e5dca45257
commit
cc51208a48
@ -32,7 +32,7 @@ impl Parse for HtmlBlock {
|
||||
BlockContent::Node(Box::new(content.parse()?))
|
||||
};
|
||||
|
||||
Ok(HtmlBlock { brace, content })
|
||||
Ok(HtmlBlock { content, brace })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user