forked from pierre/GrammalecteClient
wip: improve error management with thiserror
This commit is contained in:
parent
c7b111a53e
commit
738ece6f92
47
src/lib.rs
47
src/lib.rs
@ -36,10 +36,11 @@
|
|||||||
//! println!("RESULT = {:#?}", res);
|
//! println!("RESULT = {:#?}", res);
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
#[cfg(feature = "embedded-server")]
|
#[cfg(feature = "embedded-server")]
|
||||||
use crate::server::EmbeddedServer;
|
use crate::server::EmbeddedServer;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
#[cfg(feature = "embedded-server")]
|
#[cfg(feature = "embedded-server")]
|
||||||
pub mod server;
|
pub mod server;
|
||||||
@ -302,6 +303,28 @@ pub struct SuggestResult {
|
|||||||
pub suggestions: Vec<String>,
|
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
|
/// The Grammalecte client itself
|
||||||
pub struct GrammalecteClient {
|
pub struct GrammalecteClient {
|
||||||
base_url: String,
|
base_url: String,
|
||||||
@ -335,7 +358,7 @@ impl GrammalecteClient {
|
|||||||
///
|
///
|
||||||
/// Python 3.7 or higher must is required at runtime
|
/// Python 3.7 or higher must is required at runtime
|
||||||
#[cfg(feature = "embedded-server")]
|
#[cfg(feature = "embedded-server")]
|
||||||
pub fn start_server() -> Result<Self, Box<dyn Error>> {
|
pub fn start_server() -> Result<Self, Error> {
|
||||||
let server = EmbeddedServer::start()?;
|
let server = EmbeddedServer::start()?;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
base_url: server.base_url(),
|
base_url: server.base_url(),
|
||||||
@ -344,7 +367,7 @@ impl GrammalecteClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Run spell check on text
|
/// 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
|
self.spell_check_with_options(text, &HashMap::new()).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -353,7 +376,7 @@ impl GrammalecteClient {
|
|||||||
&self,
|
&self,
|
||||||
text: &str,
|
text: &str,
|
||||||
options: &HashMap<GramOpt, bool>,
|
options: &HashMap<GramOpt, bool>,
|
||||||
) -> Result<CheckResult, Box<dyn Error>> {
|
) -> Result<CheckResult, Error> {
|
||||||
let url = format!("{}/gc_text/fr", self.base_url);
|
let url = format!("{}/gc_text/fr", self.base_url);
|
||||||
log::debug!("Will use URL {} for spell check", url);
|
log::debug!("Will use URL {} for spell check", url);
|
||||||
|
|
||||||
@ -361,7 +384,7 @@ impl GrammalecteClient {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|t| (t.0.id(), t.1))
|
.map(|t| (t.0.id(), t.1))
|
||||||
.collect::<HashMap<_, _>>();
|
.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();
|
let mut params = HashMap::new();
|
||||||
params.insert("text", text);
|
params.insert("text", text);
|
||||||
@ -371,15 +394,17 @@ impl GrammalecteClient {
|
|||||||
.post(url)
|
.post(url)
|
||||||
.form(¶ms)
|
.form(¶ms)
|
||||||
.send()
|
.send()
|
||||||
.await?
|
.await
|
||||||
|
.map_err(Error::RequestSendCheckWithOptions)?
|
||||||
.json::<CheckResult>()
|
.json::<CheckResult>()
|
||||||
.await?;
|
.await
|
||||||
|
.map_err(Error::CheckResultDeserialize)?;
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ask for word suggestion
|
/// 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);
|
let url = format!("{}/suggest/fr", self.base_url);
|
||||||
log::debug!("Will use URL {} for word suggestion", url);
|
log::debug!("Will use URL {} for word suggestion", url);
|
||||||
|
|
||||||
@ -390,9 +415,11 @@ impl GrammalecteClient {
|
|||||||
.post(&url)
|
.post(&url)
|
||||||
.form(¶ms)
|
.form(¶ms)
|
||||||
.send()
|
.send()
|
||||||
.await?
|
.await
|
||||||
|
.map_err(Error::RequestSendSuggest)?
|
||||||
.json()
|
.json()
|
||||||
.await?)
|
.await
|
||||||
|
.map_err(Error::SuggestDeserialize)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,44 @@
|
|||||||
use crate::server::utils::{get_free_port, wait_for_port};
|
use crate::server::utils::{get_free_port, wait_for_port};
|
||||||
use mktemp::Temp;
|
use mktemp::Temp;
|
||||||
use std::error::Error;
|
use std::io::{self, Cursor, Read};
|
||||||
use std::io::{Cursor, Read};
|
|
||||||
use std::process::{Child, Stdio};
|
use std::process::{Child, Stdio};
|
||||||
|
use thiserror::Error;
|
||||||
|
use zip::result::ZipError;
|
||||||
use zip::ZipArchive;
|
use zip::ZipArchive;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error("Grammalecte-server failed to launch process")]
|
||||||
|
StartServerProcess(#[source] io::Error),
|
||||||
|
|
||||||
|
#[error("Failed to get free port")]
|
||||||
|
GetFreePort(#[source] io::Error),
|
||||||
|
|
||||||
|
#[error("Port {port} did not open in time!")]
|
||||||
|
WaitPortOpen { port: u16 },
|
||||||
|
|
||||||
|
#[error("Failed create temporary directory")]
|
||||||
|
CreateTempDir(#[source] io::Error),
|
||||||
|
|
||||||
|
#[error("TODO")]
|
||||||
|
ZipArchive(#[source] ZipError),
|
||||||
|
|
||||||
|
#[error("TODO")]
|
||||||
|
ZipFileIndex(#[source] ZipError),
|
||||||
|
|
||||||
|
// TODO: rename
|
||||||
|
#[error("TODO")]
|
||||||
|
CreateDirectory(#[source] io::Error),
|
||||||
|
|
||||||
|
// TODO: rename
|
||||||
|
#[error("TODO")]
|
||||||
|
ZipFileReadToEnd(#[source] io::Error),
|
||||||
|
|
||||||
|
// TODO: rename
|
||||||
|
#[error("TODO")]
|
||||||
|
WriteFile(#[source] io::Error),
|
||||||
|
}
|
||||||
|
|
||||||
pub struct EmbeddedServer {
|
pub struct EmbeddedServer {
|
||||||
_srv_dir: Temp,
|
_srv_dir: Temp,
|
||||||
port: u16,
|
port: u16,
|
||||||
@ -13,29 +47,30 @@ pub struct EmbeddedServer {
|
|||||||
|
|
||||||
impl EmbeddedServer {
|
impl EmbeddedServer {
|
||||||
/// Start embedded Grammalecte server on a random free port
|
/// Start embedded Grammalecte server on a random free port
|
||||||
pub fn start() -> Result<Self, Box<dyn Error>> {
|
pub fn start() -> Result<Self, Error> {
|
||||||
Self::start_listen_on_port(get_free_port()?)
|
Self::start_listen_on_port(get_free_port())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start embedded Grammalecte server on a given port
|
/// Start embedded Grammalecte server on a given port
|
||||||
pub fn start_listen_on_port(port: u16) -> Result<Self, Box<dyn Error>> {
|
pub fn start_listen_on_port(port: u16) -> Result<Self, Error> {
|
||||||
log::info!("Will start server");
|
log::info!("Will start server");
|
||||||
// First, unpack server
|
// First, unpack server
|
||||||
let dest = mktemp::Temp::new_dir()?;
|
let dest = mktemp::Temp::new_dir().map_err(Error::CreateTempDir)?;
|
||||||
let cursor = Cursor::new(include_bytes!("GrammalecteDist.zip"));
|
let cursor = Cursor::new(include_bytes!("GrammalecteDist.zip"));
|
||||||
let mut zip = ZipArchive::new(cursor)?;
|
let mut zip = ZipArchive::new(cursor).map_err(Error::ZipArchive)?;
|
||||||
for i in 0..zip.len() {
|
for i in 0..zip.len() {
|
||||||
let mut file = zip.by_index(i)?;
|
let mut file = zip.by_index(i).map_err(Error::ZipFileIndex)?;
|
||||||
if file.is_dir() {
|
if file.is_dir() {
|
||||||
log::debug!("Create directory: {}", file.name());
|
log::debug!("Create directory: {}", file.name());
|
||||||
std::fs::create_dir_all(dest.join(file.name()))?;
|
std::fs::create_dir_all(dest.join(file.name())).map_err(Error::CreateDirectory)?;
|
||||||
} else {
|
} else {
|
||||||
log::debug!("Decompress file: {}", file.name());
|
log::debug!("Decompress file: {}", file.name());
|
||||||
|
|
||||||
let mut buff = Vec::with_capacity(file.size() as usize);
|
let mut buff = Vec::with_capacity(file.size() as usize);
|
||||||
file.read_to_end(&mut buff)?;
|
file.read_to_end(&mut buff)
|
||||||
|
.map_err(Error::ZipFileReadToEnd)?;
|
||||||
|
|
||||||
std::fs::write(dest.join(file.name()), buff)?;
|
std::fs::write(dest.join(file.name()), buff).map_err(Error::WriteFile)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +89,8 @@ impl EmbeddedServer {
|
|||||||
.arg(port.to_string())
|
.arg(port.to_string())
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
.stderr(Stdio::null())
|
.stderr(Stdio::null())
|
||||||
.spawn()?;
|
.spawn()
|
||||||
|
.map_err(Error::StartServerProcess)?;
|
||||||
|
|
||||||
wait_for_port(port)?;
|
wait_for_port(port)?;
|
||||||
|
|
||||||
@ -78,11 +114,12 @@ impl Drop for EmbeddedServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mod utils {
|
mod utils {
|
||||||
use std::io::ErrorKind;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use super::Error;
|
||||||
|
|
||||||
/// Get a free port
|
/// Get a free port
|
||||||
pub fn get_free_port() -> std::io::Result<u16> {
|
pub fn get_free_port() -> u16 {
|
||||||
let mut port = 0;
|
let mut port = 0;
|
||||||
|
|
||||||
while !(2000..=64000).contains(&port) {
|
while !(2000..=64000).contains(&port) {
|
||||||
@ -93,10 +130,10 @@ mod utils {
|
|||||||
port += 1;
|
port += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(port)
|
port
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn wait_for_port(port: u16) -> std::io::Result<()> {
|
pub fn wait_for_port(port: u16) -> Result<(), Error> {
|
||||||
for _ in 0..50 {
|
for _ in 0..50 {
|
||||||
if port_scanner::scan_port(port) {
|
if port_scanner::scan_port(port) {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@ -104,9 +141,6 @@ mod utils {
|
|||||||
std::thread::sleep(Duration::from_millis(100));
|
std::thread::sleep(Duration::from_millis(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(std::io::Error::new(
|
Err(Error::WaitPortOpen { port })
|
||||||
ErrorKind::Other,
|
|
||||||
format!("Port {} did not open in time!", port),
|
|
||||||
))?
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user