You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.0 KiB
84 lines
2.0 KiB
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<String>, |
|
} |
|
|
|
#[derive(Debug, Deserialize)] |
|
pub struct DecodeQuery {} |
|
|
|
#[derive(Debug, Serialize)] |
|
pub struct EncodeResponse { |
|
pub input: String, |
|
pub dict: String, |
|
pub result: Vec<Vec<EncodePart>>, |
|
} |
|
|
|
#[derive(Debug, Serialize)] |
|
pub struct EncodePart { |
|
pub value: u64, |
|
pub words: Vec<String>, |
|
} |
|
|
|
#[derive(Debug, Serialize)] |
|
pub struct DecodeResponse { |
|
pub input: String, |
|
pub result: String, |
|
} |
|
|
|
pub async fn encode_handler( |
|
State(state): State<Arc<AppState>>, |
|
Path(input): Path<String>, |
|
Query(params): Query<EncodeQuery>, |
|
) -> Result<Json<EncodeResponse>, 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<Vec<EncodePart>> = 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<Arc<AppState>>, |
|
Path(input): Path<String>, |
|
Query(_params): Query<DecodeQuery>, |
|
) -> Result<Json<DecodeResponse>, ErrorResponse> { |
|
let result = state.dependencies.major_system_service.decode(&input)?; |
|
|
|
Ok(Json(DecodeResponse { |
|
input, |
|
result: result.as_str().to_string(), |
|
})) |
|
} |
|
|
|
pub fn routes() -> Router<Arc<AppState>> { |
|
Router::new() |
|
.route("/encode/pl/{input}", get(encode_handler)) |
|
.route("/decode/pl/{input}", get(decode_handler)) |
|
}
|
|
|