saola_kit/envelope/
context_state.rs1use std::collections::BTreeMap;
2
3use laravel::http::{IntoResponse, Json, Response};
4use serde::Serialize;
5use serde_json::{Map, Value};
6
7use super::HEADER_VIEW_REVISION;
8
9#[derive(Debug, Clone, Serialize)]
11pub struct ViewContextState {
12 pub context: String,
13 pub revision: String,
14 pub views: String,
15 #[serde(rename = "systemData")]
16 pub system_data: Map<String, Value>,
17 #[serde(rename = "sharedData")]
18 pub shared_data: Map<String, Value>,
19 pub routes: Vec<Value>,
20 pub links: BTreeMap<String, Value>,
21 pub contexts: BTreeMap<String, Value>,
22 pub primary: String,
23}
24
25impl ViewContextState {
26 pub fn new(
27 context: impl Into<String>,
28 revision: impl Into<String>,
29 views: impl Into<String>,
30 primary: impl Into<String>,
31 ) -> Self {
32 let ctx = context.into();
33 let mut system_data = Map::new();
34 system_data.insert("__context__".to_owned(), Value::String(ctx.clone()));
35 Self {
36 context: ctx,
37 revision: revision.into(),
38 views: views.into(),
39 system_data,
40 shared_data: Map::new(),
41 routes: Vec::new(),
42 links: BTreeMap::new(),
43 contexts: BTreeMap::new(),
44 primary: primary.into(),
45 }
46 }
47}
48
49impl IntoResponse for ViewContextState {
50 fn into_response(self) -> Response {
51 let revision = self.revision.clone();
52 Json(self)
53 .with_header("content-type", "application/json; charset=utf-8")
54 .with_header(HEADER_VIEW_REVISION, revision)
55 .with_header("cache-control", "private, no-cache")
56 .with_header("vary", "Cookie, Accept-Language")
57 }
58}
59
60#[derive(Debug, Clone, Serialize)]
63pub struct AppBootConfig {
64 pub container: String,
65 #[serde(skip_serializing_if = "Option::is_none")]
66 pub context: Option<String>,
67 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
68 pub shared: BTreeMap<String, Value>,
69 #[serde(skip_serializing_if = "Vec::is_empty")]
70 pub bundles: Vec<String>,
71 #[serde(skip_serializing_if = "Option::is_none")]
72 pub i18n: Option<Value>,
73 pub view: Value,
74 pub router: Value,
75}
76
77impl Default for AppBootConfig {
78 fn default() -> Self {
79 Self {
80 container: "#app-root".to_owned(),
81 context: None,
82 shared: BTreeMap::new(),
83 bundles: Vec::new(),
84 i18n: None,
85 view: Value::Object(Map::new()),
86 router: Value::Object(Map::new()),
87 }
88 }
89}
90
91impl AppBootConfig {
92 pub fn new() -> Self {
93 Self::default()
94 }
95
96 pub fn to_json_string(&self) -> Result<String, serde_json::Error> {
97 serde_json::to_string(self)
98 }
99}