|
| 1 | +use actix_web::{ |
| 2 | + get, |
| 3 | + http::{header::ContentType, StatusCode}, |
| 4 | + web::{Data, Path, Query}, |
| 5 | + HttpResponse, Responder, ResponseError, |
| 6 | +}; |
| 7 | +use derive_more::{Display, Error}; |
| 8 | +use odict::{format::json::ToJSON, split::SplitOptions}; |
| 9 | +use serde::Deserialize; |
| 10 | + |
| 11 | +use crate::get_lookup_entries; |
| 12 | + |
| 13 | +#[derive(Debug, Deserialize)] |
| 14 | +pub struct SplitRequest { |
| 15 | + q: String, |
| 16 | + follow: Option<bool>, |
| 17 | + min_length: Option<usize>, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Debug, Display, Error)] |
| 21 | +enum SplitError { |
| 22 | + #[display("Dictionary not found: {}", name)] |
| 23 | + DictionaryNotFound { name: String }, |
| 24 | + |
| 25 | + #[display("Failed to read dictionary: {}", name)] |
| 26 | + DictionaryReadError { name: String }, |
| 27 | + |
| 28 | + #[display("Split error: {}", message)] |
| 29 | + SplitError { message: String }, |
| 30 | + |
| 31 | + #[display("Failed to serialize response")] |
| 32 | + SerializeError, |
| 33 | +} |
| 34 | + |
| 35 | +impl ResponseError for SplitError { |
| 36 | + fn error_response(&self) -> HttpResponse { |
| 37 | + HttpResponse::build(self.status_code()) |
| 38 | + .insert_header(ContentType::html()) |
| 39 | + .body(self.to_string()) |
| 40 | + } |
| 41 | + |
| 42 | + fn status_code(&self) -> StatusCode { |
| 43 | + match *self { |
| 44 | + SplitError::DictionaryNotFound { .. } => StatusCode::NOT_FOUND, |
| 45 | + SplitError::DictionaryReadError { .. } => StatusCode::INTERNAL_SERVER_ERROR, |
| 46 | + SplitError::SplitError { .. } => StatusCode::INTERNAL_SERVER_ERROR, |
| 47 | + SplitError::SerializeError => StatusCode::INTERNAL_SERVER_ERROR, |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[get("/{name}/split")] |
| 53 | +async fn handle_split( |
| 54 | + params: Query<SplitRequest>, |
| 55 | + dict: Path<String>, |
| 56 | + dictionary_cache: Data<crate::serve::DictionaryCache>, |
| 57 | +) -> Result<impl Responder, SplitError> { |
| 58 | + let SplitRequest { |
| 59 | + q: raw_queries, |
| 60 | + follow, |
| 61 | + min_length, |
| 62 | + } = params.0; |
| 63 | + |
| 64 | + let queries = raw_queries |
| 65 | + .split(',') |
| 66 | + .map(|s| s.to_string()) |
| 67 | + .collect::<Vec<_>>(); |
| 68 | + |
| 69 | + let dictionary_name = dict.into_inner(); |
| 70 | + |
| 71 | + let file = dictionary_cache |
| 72 | + .get(&dictionary_name) |
| 73 | + .await |
| 74 | + .map_err(|_e| SplitError::DictionaryReadError { |
| 75 | + name: dictionary_name.to_string(), |
| 76 | + })? |
| 77 | + .ok_or(SplitError::DictionaryNotFound { |
| 78 | + name: dictionary_name.to_string(), |
| 79 | + })?; |
| 80 | + |
| 81 | + let dictionary = file |
| 82 | + .contents() |
| 83 | + .map_err(|_e| SplitError::DictionaryReadError { |
| 84 | + name: dictionary_name.to_string(), |
| 85 | + })?; |
| 86 | + |
| 87 | + let opts = SplitOptions::default() |
| 88 | + .threshold(min_length.unwrap_or(1)) |
| 89 | + .follow(follow.unwrap_or(false)); |
| 90 | + |
| 91 | + let entries = dictionary |
| 92 | + .split(&queries, &opts) |
| 93 | + .map_err(|e| SplitError::SplitError { |
| 94 | + message: e.to_string(), |
| 95 | + })?; |
| 96 | + |
| 97 | + let json = get_lookup_entries(entries) |
| 98 | + .to_json(true) |
| 99 | + .map_err(|_e| SplitError::SerializeError)?; |
| 100 | + |
| 101 | + Ok(HttpResponse::Ok() |
| 102 | + .content_type("application/json") |
| 103 | + .body(json)) |
| 104 | +} |
0 commit comments