diff --git a/src/lib.rs b/src/lib.rs index 400a18a..f458ec8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,6 +57,11 @@ pub struct SpellingError { pub error_type: String, } +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct SuggestResult { + suggestions: Vec, +} + pub struct GrammalecteClient { base_url: String, } @@ -80,7 +85,7 @@ impl GrammalecteClient { /// Run spell check on text pub async fn spell_check(&self, text: &str) -> Result> { let url = format!("{}/gc_text/fr", self.base_url); - log::info!("Will use URL {}", url); + log::debug!("Will use URL {} for spell check", url); let mut params = HashMap::new(); params.insert("text", text); @@ -95,6 +100,23 @@ impl GrammalecteClient { Ok(result) } + + /// Ask for word suggestion + pub async fn suggest(&self, token: &str) -> Result> { + let url = format!("{}/suggest/fr", self.base_url); + log::debug!("Will use URL {} for word suggestion", url); + + let mut params = HashMap::new(); + params.insert("token", token); + + Ok(reqwest::Client::new() + .post(&url) + .form(¶ms) + .send() + .await? + .json() + .await?) + } } #[cfg(test)] @@ -109,4 +131,16 @@ mod test { let res = GrammalecteClient::default().spell_check(msg).await.unwrap(); println!("RESULT = {:#?}", res); } + + #[tokio::test] + async fn simple_suggestion() { + let _ = env_logger::builder().is_test(true).try_init(); + + let res = GrammalecteClient::default() + .suggest("bonjou") + .await + .unwrap(); + assert!(res.suggestions.contains(&"bonjour".to_string())); + println!("RESULT = {:#?}", res); + } }