-
-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
tracing
support for component props (#1531)
- Loading branch information
1 parent
d9abebb
commit 18deb39
Showing
4 changed files
with
202 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
use wasm_bindgen::UnwrapThrowExt; | ||
|
||
#[macro_export] | ||
/// Use for tracing property | ||
macro_rules! tracing_props { | ||
() => { | ||
::leptos::leptos_dom::tracing::span!( | ||
::leptos::leptos_dom::tracing::Level::INFO, | ||
"leptos_dom::tracing_props", | ||
props = String::from("[]") | ||
); | ||
}; | ||
($($prop:tt),+ $(,)?) => { | ||
{ | ||
use ::leptos::leptos_dom::tracing_property::{Match, SerializeMatch, DefaultMatch}; | ||
let mut props = String::from('['); | ||
$( | ||
let prop = (&&Match { | ||
name: stringify!{$prop}, | ||
value: std::cell::Cell::new(Some(&$prop)) | ||
}).spez(); | ||
props.push_str(&format!("{prop},")); | ||
)* | ||
props.pop(); | ||
props.push(']'); | ||
::leptos::leptos_dom::tracing::span!( | ||
::leptos::leptos_dom::tracing::Level::INFO, | ||
"leptos_dom::tracing_props", | ||
props | ||
); | ||
} | ||
}; | ||
} | ||
|
||
// Implementation based on spez | ||
// see https://github.com/m-ou-se/spez | ||
|
||
pub struct Match<T> { | ||
pub name: &'static str, | ||
pub value: std::cell::Cell<Option<T>>, | ||
} | ||
|
||
pub trait SerializeMatch { | ||
type Return; | ||
fn spez(&self) -> Self::Return; | ||
} | ||
impl<T: serde::Serialize> SerializeMatch for &Match<&T> { | ||
type Return = String; | ||
fn spez(&self) -> Self::Return { | ||
let name = self.name; | ||
serde_json::to_string(self.value.get().unwrap_throw()).map_or_else( | ||
|err| format!(r#"{{"name": "{name}", "error": "{err}"}}"#), | ||
|value| format!(r#"{{"name": "{name}", "value": {value}}}"#), | ||
) | ||
} | ||
} | ||
|
||
pub trait DefaultMatch { | ||
type Return; | ||
fn spez(&self) -> Self::Return; | ||
} | ||
impl<T> DefaultMatch for Match<&T> { | ||
type Return = String; | ||
fn spez(&self) -> Self::Return { | ||
let name = self.name; | ||
format!( | ||
r#"{{"name": "{name}", "error": "The trait `serde::Serialize` is not implemented"}}"# | ||
) | ||
} | ||
} | ||
|
||
#[test] | ||
fn match_primitive() { | ||
// String | ||
let test = String::from("string"); | ||
let prop = (&&Match { | ||
name: stringify! {test}, | ||
value: std::cell::Cell::new(Some(&test)), | ||
}) | ||
.spez(); | ||
assert_eq!(prop, r#"{"name": "test", "value": "string"}"#); | ||
|
||
// &str | ||
let test = "string"; | ||
let prop = (&&Match { | ||
name: stringify! {test}, | ||
value: std::cell::Cell::new(Some(&test)), | ||
}) | ||
.spez(); | ||
assert_eq!(prop, r#"{"name": "test", "value": "string"}"#); | ||
|
||
// u128 | ||
let test: u128 = 1; | ||
let prop = (&&Match { | ||
name: stringify! {test}, | ||
value: std::cell::Cell::new(Some(&test)), | ||
}) | ||
.spez(); | ||
assert_eq!(prop, r#"{"name": "test", "value": 1}"#); | ||
|
||
// i128 | ||
let test: i128 = -1; | ||
let prop = (&&Match { | ||
name: stringify! {test}, | ||
value: std::cell::Cell::new(Some(&test)), | ||
}) | ||
.spez(); | ||
assert_eq!(prop, r#"{"name": "test", "value": -1}"#); | ||
|
||
// f64 | ||
let test = 3.14; | ||
let prop = (&&Match { | ||
name: stringify! {test}, | ||
value: std::cell::Cell::new(Some(&test)), | ||
}) | ||
.spez(); | ||
assert_eq!(prop, r#"{"name": "test", "value": 3.14}"#); | ||
|
||
// bool | ||
let test = true; | ||
let prop = (&&Match { | ||
name: stringify! {test}, | ||
value: std::cell::Cell::new(Some(&test)), | ||
}) | ||
.spez(); | ||
assert_eq!(prop, r#"{"name": "test", "value": true}"#); | ||
} | ||
|
||
#[test] | ||
fn match_serialize() { | ||
use serde::Serialize; | ||
#[derive(Serialize)] | ||
struct CustomStruct { | ||
field: &'static str, | ||
} | ||
|
||
let test = CustomStruct { field: "field" }; | ||
let prop = (&&Match { | ||
name: stringify! {test}, | ||
value: std::cell::Cell::new(Some(&test)), | ||
}) | ||
.spez(); | ||
assert_eq!(prop, r#"{"name": "test", "value": {"field":"field"}}"#); | ||
// Verification of ownership | ||
assert_eq!(test.field, "field"); | ||
} | ||
|
||
#[test] | ||
fn match_no_serialize() { | ||
struct CustomStruct { | ||
field: &'static str, | ||
} | ||
|
||
let test = CustomStruct { field: "field" }; | ||
let prop = (&&Match { | ||
name: stringify! {test}, | ||
value: std::cell::Cell::new(Some(&test)), | ||
}) | ||
.spez(); | ||
assert_eq!( | ||
prop, | ||
r#"{"name": "test", "error": "The trait `serde::Serialize` is not implemented"}"# | ||
); | ||
// Verification of ownership | ||
assert_eq!(test.field, "field"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters