forked from pierre/GrammalecteClient
improve error management with thiserror use
- add thiserror dependency - use distinct Error enum in lib and server
This commit is contained in:
49
src/lib.rs
49
src/lib.rs
@ -36,10 +36,11 @@
|
||||
//! println!("RESULT = {:#?}", res);
|
||||
//! ```
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
#[cfg(feature = "embedded-server")]
|
||||
use crate::server::EmbeddedServer;
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
|
||||
#[cfg(feature = "embedded-server")]
|
||||
pub mod server;
|
||||
@ -302,6 +303,28 @@ pub struct SuggestResult {
|
||||
pub suggestions: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
#[cfg(feature = "embedded-server")]
|
||||
#[error("Grammalecte-server failed to start")]
|
||||
ServerStartFailed(#[from] server::Error),
|
||||
|
||||
#[error("Failed to Serialize Option in Json")]
|
||||
OptionJsonSerialization(#[source] serde_json::Error),
|
||||
|
||||
#[error("Failed to send request `check with option`")]
|
||||
RequestSendCheckWithOptions(#[source] reqwest::Error),
|
||||
|
||||
#[error("Failed to send request `suggest`")]
|
||||
RequestSendSuggest(#[source] reqwest::Error),
|
||||
|
||||
#[error("Failed to Deserialize Check result")]
|
||||
CheckResultDeserialize(#[source] reqwest::Error),
|
||||
|
||||
#[error("Failed to Deserialize Suggest result")]
|
||||
SuggestDeserialize(#[source] reqwest::Error),
|
||||
}
|
||||
|
||||
/// The Grammalecte client itself
|
||||
pub struct GrammalecteClient {
|
||||
base_url: String,
|
||||
@ -335,7 +358,7 @@ impl GrammalecteClient {
|
||||
///
|
||||
/// Python 3.7 or higher must is required at runtime
|
||||
#[cfg(feature = "embedded-server")]
|
||||
pub fn start_server() -> Result<Self, Box<dyn Error>> {
|
||||
pub fn start_server() -> Result<Self, Error> {
|
||||
let server = EmbeddedServer::start()?;
|
||||
Ok(Self {
|
||||
base_url: server.base_url(),
|
||||
@ -344,7 +367,7 @@ impl GrammalecteClient {
|
||||
}
|
||||
|
||||
/// Run spell check on text
|
||||
pub async fn spell_check(&self, text: &str) -> Result<CheckResult, Box<dyn Error>> {
|
||||
pub async fn spell_check(&self, text: &str) -> Result<CheckResult, Error> {
|
||||
self.spell_check_with_options(text, &HashMap::new()).await
|
||||
}
|
||||
|
||||
@ -353,7 +376,7 @@ impl GrammalecteClient {
|
||||
&self,
|
||||
text: &str,
|
||||
options: &HashMap<GramOpt, bool>,
|
||||
) -> Result<CheckResult, Box<dyn Error>> {
|
||||
) -> Result<CheckResult, Error> {
|
||||
let url = format!("{}/gc_text/fr", self.base_url);
|
||||
log::debug!("Will use URL {} for spell check", url);
|
||||
|
||||
@ -361,7 +384,7 @@ impl GrammalecteClient {
|
||||
.iter()
|
||||
.map(|t| (t.0.id(), t.1))
|
||||
.collect::<HashMap<_, _>>();
|
||||
let options = serde_json::to_string(&options)?;
|
||||
let options = serde_json::to_string(&options).map_err(Error::OptionJsonSerialization)?;
|
||||
|
||||
let mut params = HashMap::new();
|
||||
params.insert("text", text);
|
||||
@ -371,28 +394,32 @@ impl GrammalecteClient {
|
||||
.post(url)
|
||||
.form(¶ms)
|
||||
.send()
|
||||
.await?
|
||||
.await
|
||||
.map_err(Error::RequestSendCheckWithOptions)?
|
||||
.json::<CheckResult>()
|
||||
.await?;
|
||||
.await
|
||||
.map_err(Error::CheckResultDeserialize)?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Ask for word suggestion
|
||||
pub async fn suggest(&self, token: &str) -> Result<SuggestResult, Box<dyn Error>> {
|
||||
pub async fn suggest(&self, token: &str) -> Result<SuggestResult, Error> {
|
||||
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()
|
||||
reqwest::Client::new()
|
||||
.post(&url)
|
||||
.form(¶ms)
|
||||
.send()
|
||||
.await?
|
||||
.await
|
||||
.map_err(Error::RequestSendSuggest)?
|
||||
.json()
|
||||
.await?)
|
||||
.await
|
||||
.map_err(Error::SuggestDeserialize)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user