px_wsdom_core/
serialize.rs1use serde::Serialize;
2
3use crate::js::value::JsValue;
4use crate::protocol::GET;
5
6pub trait UseInJsCode {
12 fn serialize_to(&self, buf: &mut core::fmt::Formatter<'_>) -> core::fmt::Result;
13}
14
15impl UseInJsCode for JsValue {
16 fn serialize_to(&self, buf: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
17 let self_id = self.id;
18 write!(buf, "{GET}({self_id})").unwrap();
19 Ok(())
20 }
21}
22
23pub struct SerdeToJs<'a, T: ?Sized>(pub &'a T);
24
25impl<'a, T: Serialize + ?Sized> UseInJsCode for SerdeToJs<'a, T> {
26 fn serialize_to(&self, buf: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
27 buf.write_str(&serde_json::to_string(&self.0).map_err(|_|core::fmt::Error)?)
28 }
29}
30
31pub(crate) struct UseInJsCodeWriter<'a, T: UseInJsCode + ?Sized>(pub &'a T);
32
33impl<'a, T: UseInJsCode + ?Sized> core::fmt::Display for UseInJsCodeWriter<'a, T> {
34 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
35 self.0.serialize_to(f)
36 }
37}
38
39pub struct RawCodeImmediate<'a>(pub &'a str);
40impl<'a> UseInJsCode for RawCodeImmediate<'a> {
41 fn serialize_to(&self, buf: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
42 buf.write_str(self.0)
43 }
44}
45
46pub trait ToJs<JsType>
51where
52 Self: UseInJsCode,
53 JsType: ?Sized,
54{
55}
56
57impl<T> ToJs<T> for T where T: UseInJsCode {}