1#![doc = include_str!("../examples/basic.rs")]
10use std::fmt::Display;
20
21use axum::routing::MethodRouter;
22
23pub trait HtmxHandler<S> {
25 fn axum_router(self) -> (&'static str, MethodRouter<S>);
26}
27
28#[non_exhaustive]
29#[derive(Debug, PartialEq, Eq)]
30pub enum HtmxMethod {
32 Get,
33 Post,
34 Delete,
35 Patch,
36 Put,
37}
38
39impl Display for HtmxMethod {
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 f.write_str(match self {
42 HtmxMethod::Get => "hx-get",
43 HtmxMethod::Post => "hx-post",
44 HtmxMethod::Delete => "hx-delete",
45 HtmxMethod::Patch => "hx-patch",
46 HtmxMethod::Put => "hx-put",
47 })
48 }
49}
50
51pub use axum_routing_htmx_macros::{hx_delete, hx_get, hx_patch, hx_post, hx_put};
52
53pub trait HtmxRouter: Sized {
56 type State: Send + Sync + Clone + 'static;
58
59 fn htmx_route(self, handler: impl HtmxHandler<Self::State>) -> Self;
63}
64
65impl<S> HtmxRouter for axum::Router<S>
66where
67 S: Send + Sync + Clone + 'static,
68{
69 type State = S;
70
71 fn htmx_route(self, handler: impl HtmxHandler<Self::State>) -> Self {
72 let (path, method_router) = handler.axum_router();
73 self.route(path, method_router)
74 }
75}