From f4b2beab7ae26a5b01d3fc643062c6f660070e67 Mon Sep 17 00:00:00 2001 From: tk <49250442+tkr-sh@users.noreply.github.com> Date: Fri, 21 Nov 2025 19:00:17 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20datastar=20headers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/axum.rs | 32 ++++++++++++++++++++++++++++++++ src/consts.rs | 4 ++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/axum.rs b/src/axum.rs index 98905aa..01149f5 100644 --- a/src/axum.rs +++ b/src/axum.rs @@ -213,3 +213,35 @@ where Ok(Self(json)) } } + +/// Datastar's headers +pub mod header { + use { + crate::consts::ElementPatchMode, + axum::http::{HeaderName, HeaderValue}, + }; + + /// A CSS selector for the target elements to patch + pub const DATASTAR_SELECTOR: HeaderName = HeaderName::from_static("datastar-selector"); + + /// How to patch the elements (See [`ElementPatchMode`]). Defaults to [`ElementPatchMode::Outer`]. + pub const DATASTAR_MODE: HeaderName = HeaderName::from_static("datastar-mode"); + + /// Whether to use the [View Transition API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transition_API) when patching elements. + pub const DATASTAR_USE_VIEW_TRANSITION: HeaderName = + HeaderName::from_static("datastar-use-view-transition"); + + /// If set to true, only patch signals that don’t already exist + pub const DATASTAR_ONLY_IF_MISSING: HeaderName = + HeaderName::from_static("datastar-only-if-missing"); + + /// Sets the script element’s attributes using a JSON encoded string. + pub const DATASTAR_SCRIPT_ATTRIBUTES: HeaderName = + HeaderName::from_static("datastar-script-attributes"); + + impl From for HeaderValue { + fn from(value: ElementPatchMode) -> Self { + HeaderValue::from_static(value.as_str()) + } + } +} diff --git a/src/consts.rs b/src/consts.rs index ad194b0..c14e3c3 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -68,8 +68,8 @@ pub enum ElementPatchMode { } impl ElementPatchMode { - /// Returns the [`ElementPatchMode`] as a string. - pub(crate) const fn as_str(&self) -> &str { + /// Returns the [`ElementPatchMode`] as a [`&'static str`]. + pub(crate) const fn as_str(&self) -> &'static str { match self { Self::Outer => "outer", Self::Inner => "inner",