use serde::Serialize;
#[derive(Clone, Default, Debug, Serialize)]
pub struct InertiaShared {
#[serde(skip_serializing_if = "Option::is_none")]
pub auth: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub flash: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub csrf: Option<String>,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub extra: Option<serde_json::Value>,
}
impl InertiaShared {
pub fn new() -> Self {
Self::default()
}
pub fn auth(mut self, auth: impl Serialize) -> Self {
self.auth = Some(serde_json::to_value(auth).unwrap_or_default());
self
}
pub fn flash(mut self, flash: impl Serialize) -> Self {
self.flash = Some(serde_json::to_value(flash).unwrap_or_default());
self
}
pub fn csrf(mut self, token: impl Into<String>) -> Self {
self.csrf = Some(token.into());
self
}
pub fn with(mut self, extra: impl Serialize) -> Self {
self.extra = Some(serde_json::to_value(extra).unwrap_or_default());
self
}
pub fn merge_into(&self, props: &mut serde_json::Value) {
if let serde_json::Value::Object(ref mut map) = props {
if let Some(auth) = &self.auth {
map.insert("auth".to_string(), auth.clone());
}
if let Some(flash) = &self.flash {
map.insert("flash".to_string(), flash.clone());
}
if let Some(csrf) = &self.csrf {
map.insert("csrf".to_string(), serde_json::Value::String(csrf.clone()));
}
if let Some(serde_json::Value::Object(extra_map)) = &self.extra {
for (k, v) in extra_map {
map.insert(k.clone(), v.clone());
}
}
}
}
}