1use serde::Serialize;
4
5#[derive(Clone, Default, Debug, Serialize)]
28pub struct InertiaShared {
29 #[serde(skip_serializing_if = "Option::is_none")]
31 pub auth: Option<serde_json::Value>,
32 #[serde(skip_serializing_if = "Option::is_none")]
34 pub flash: Option<serde_json::Value>,
35 #[serde(skip_serializing_if = "Option::is_none")]
37 pub csrf: Option<String>,
38 #[serde(flatten, skip_serializing_if = "Option::is_none")]
40 pub extra: Option<serde_json::Value>,
41}
42
43impl InertiaShared {
44 pub fn new() -> Self {
46 Self::default()
47 }
48
49 pub fn auth(mut self, auth: impl Serialize) -> Self {
51 self.auth = Some(serde_json::to_value(auth).unwrap_or_default());
52 self
53 }
54
55 pub fn flash(mut self, flash: impl Serialize) -> Self {
57 self.flash = Some(serde_json::to_value(flash).unwrap_or_default());
58 self
59 }
60
61 pub fn csrf(mut self, token: impl Into<String>) -> Self {
63 self.csrf = Some(token.into());
64 self
65 }
66
67 pub fn with(mut self, extra: impl Serialize) -> Self {
71 self.extra = Some(serde_json::to_value(extra).unwrap_or_default());
72 self
73 }
74
75 pub fn merge_into(&self, props: &mut serde_json::Value) {
77 if let serde_json::Value::Object(ref mut map) = props {
78 if let Some(auth) = &self.auth {
79 map.insert("auth".to_string(), auth.clone());
80 }
81 if let Some(flash) = &self.flash {
82 map.insert("flash".to_string(), flash.clone());
83 }
84 if let Some(csrf) = &self.csrf {
85 map.insert("csrf".to_string(), serde_json::Value::String(csrf.clone()));
86 }
87 if let Some(serde_json::Value::Object(extra_map)) = &self.extra {
88 for (k, v) in extra_map {
89 map.insert(k.clone(), v.clone());
90 }
91 }
92 }
93 }
94}