saola-kit 0.1.2

Saola core toolkit for Laravel for Rust (contexts, envelope protocol, i18n, masks, modules, Sanctum-compatible tokens)
Documentation
use std::collections::BTreeMap;

use laravel::http::{IntoResponse, Json, Response};
use serde::Serialize;
use serde_json::{Map, Value};

use super::HEADER_VIEW_REVISION;

/// Payload returned by `GET /_saola/context/{context}`, matching `ViewContextController`.
#[derive(Debug, Clone, Serialize)]
pub struct ViewContextState {
    pub context: String,
    pub revision: String,
    pub views: String,
    #[serde(rename = "systemData")]
    pub system_data: Map<String, Value>,
    #[serde(rename = "sharedData")]
    pub shared_data: Map<String, Value>,
    pub routes: Vec<Value>,
    pub links: BTreeMap<String, Value>,
    pub contexts: BTreeMap<String, Value>,
    pub primary: String,
}

impl ViewContextState {
    pub fn new(
        context: impl Into<String>,
        revision: impl Into<String>,
        views: impl Into<String>,
        primary: impl Into<String>,
    ) -> Self {
        let ctx = context.into();
        let mut system_data = Map::new();
        system_data.insert("__context__".to_owned(), Value::String(ctx.clone()));
        Self {
            context: ctx,
            revision: revision.into(),
            views: views.into(),
            system_data,
            shared_data: Map::new(),
            routes: Vec::new(),
            links: BTreeMap::new(),
            contexts: BTreeMap::new(),
            primary: primary.into(),
        }
    }
}

impl IntoResponse for ViewContextState {
    fn into_response(self) -> Response {
        let revision = self.revision.clone();
        Json(self)
            .with_header("content-type", "application/json; charset=utf-8")
            .with_header(HEADER_VIEW_REVISION, revision)
            .with_header("cache-control", "private, no-cache")
            .with_header("vary", "Cookie, Accept-Language")
    }
}

/// Boot configuration embedded as `window.APP_CONFIGS` in the root HTML shell,
/// matching `client/src/core/bootstrap/ssr.ts` (`readBootConfig`).
#[derive(Debug, Clone, Serialize)]
pub struct AppBootConfig {
    pub container: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context: Option<String>,
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub shared: BTreeMap<String, Value>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub bundles: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub i18n: Option<Value>,
    pub view: Value,
    pub router: Value,
}

impl Default for AppBootConfig {
    fn default() -> Self {
        Self {
            container: "#app-root".to_owned(),
            context: None,
            shared: BTreeMap::new(),
            bundles: Vec::new(),
            i18n: None,
            view: Value::Object(Map::new()),
            router: Value::Object(Map::new()),
        }
    }
}

impl AppBootConfig {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn to_json_string(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string(self)
    }
}