tradukhelpu.rs (886B)
1 use futures::prelude::*; 2 use serde::{Deserialize, de::DeserializeOwned}; 3 4 #[derive(Deserialize)] 5 #[serde(rename_all="camelCase")] 6 struct Response { 7 response_data: Traduk, 8 response_status: u16, 9 response_details: Option<String> 10 } 11 12 #[derive(Deserialize)] 13 #[serde(rename_all="camelCase")] 14 struct Traduk { 15 translated_text: String 16 } 17 18 pub async fn trad(content: &str, srclang: &str) -> Result<String, String> { 19 let client = reqwest::Client::new(); 20 21 if srclang.len() != 3 { 22 return Err("Invalid lang".into()); 23 } 24 25 let resp: Response = client.post("https://beta.apertium.org/apy/translate") 26 .query(&[ 27 ("langpair", format!("{}|epo", srclang)), 28 ("format", "txt".into()), 29 ("q", content.into()) 30 ]).send().and_then(|r| r.json()).map_err(|e| e.to_string()).await?; 31 32 Ok(resp.response_data.translated_text) 33 }