use axum::{ Json, Router, extract::{Path, Query, State}, routing::get, }; use serde::{Deserialize, Serialize}; use std::sync::Arc; use crate::error::ErrorResponse; use crate::state::AppState; #[derive(Debug, Deserialize)] pub struct EncodeQuery { pub dict: Option, } #[derive(Debug, Deserialize)] pub struct DecodeQuery {} #[derive(Debug, Serialize)] pub struct EncodeResponse { pub input: String, pub dict: String, pub result: Vec>, } #[derive(Debug, Serialize)] pub struct EncodePart { pub value: u64, pub words: Vec, } #[derive(Debug, Serialize)] pub struct DecodeResponse { pub input: String, pub result: String, } pub async fn encode_handler( State(state): State>, Path(input): Path, Query(params): Query, ) -> Result, ErrorResponse> { let dict_name = params.dict.unwrap_or_else(|| "demo_pl".to_string()); let result = state.dependencies.major_system_service.encode(&input)?; let encoded_parts: Vec> = result .iter() .map(|split| { split .iter() .map(|part| EncodePart { value: part.value, words: part.words.clone(), }) .collect() }) .collect(); Ok(Json(EncodeResponse { input, dict: dict_name, result: encoded_parts, })) } pub async fn decode_handler( State(state): State>, Path(input): Path, Query(_params): Query, ) -> Result, ErrorResponse> { let result = state.dependencies.major_system_service.decode(&input)?; Ok(Json(DecodeResponse { input, result: result.as_str().to_string(), })) } pub fn routes() -> Router> { Router::new() .route("/encode/pl/{input}", get(encode_handler)) .route("/decode/pl/{input}", get(decode_handler)) }