rustysan-sword 0.1.1

Blade-inspired HTML templates for Rust
Documentation
//! Values only the host application can supply: its environment, session and context.
use serde_json::Value;

/// What `@env`, `@production`, `@session` and `@context` read. The standalone engine knows
/// its configured environment; the Laravel adapter supplies all three.
pub(crate) trait Host: Send + Sync {
    /// The application environment name.
    fn environment(&self) -> &str;
    /// The session value for `key`: `Ok(None)` when absent or null, `Err` with advice when
    /// there is no session to read.
    fn session(&self, key: &str) -> Result<Option<Value>, String>;
    /// The context value for `key`, as for [`Host::session`].
    fn context(&self, key: &str) -> Result<Option<Value>, String>;
    /// The asset tags of Vite entry points: trusted markup.
    fn vite(&self, entries: &[String]) -> Result<String, String>;
    /// React Fast Refresh's preamble, or nothing.
    fn vite_react_refresh(&self) -> Result<String, String>;
}

/// The host of a standalone [`crate::Sword`], which has an environment and nothing else.
pub(crate) struct Standalone {
    pub environment: String,
}

impl Host for Standalone {
    fn environment(&self) -> &str {
        &self.environment
    }
    fn session(&self, _: &str) -> Result<Option<Value>, String> {
        Err("no session is available; @session needs the Laravel adapter".to_owned())
    }
    fn context(&self, _: &str) -> Result<Option<Value>, String> {
        Err("no context is available; @context needs the Laravel adapter".to_owned())
    }
    fn vite(&self, _: &[String]) -> Result<String, String> {
        Err("@vite needs the Laravel adapter, which knows where the Vite build is".to_owned())
    }
    fn vite_react_refresh(&self) -> Result<String, String> {
        Err("@viteReactRefresh needs the Laravel adapter".to_owned())
    }
}