use crate::control::{FilterPatch, Hydrant}; use crate::filter::{FilterMode, SetUpdate}; use axum::{ Json, Router, extract::State, http::StatusCode, routing::{get, patch}, }; use serde::Deserialize; type FilterSnapshot = crate::control::FilterSnapshot; pub fn router() -> Router { Router::new() .route("/filter", get(handle_get_filter)) .route("/filter", patch(handle_patch_filter)) } pub async fn handle_get_filter( State(hydrant): State, ) -> Result, (StatusCode, String)> { hydrant .filter .get() .await .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string())) .map(Json) } #[derive(Deserialize)] pub struct PatchFilterBody { pub mode: Option, pub signals: Option, pub collections: Option, pub excludes: Option, } pub async fn handle_patch_filter( State(hydrant): State, Json(body): Json, ) -> Result, (StatusCode, String)> { let mut p = FilterPatch::new(&hydrant.filter); p.mode = body.mode; p.signals = body.signals; p.collections = body.collections; p.excludes = body.excludes; p.apply() .await .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string())) .map(Json) }