Compare commits

..

No commits in common. "5cce702f03d5bcd481bc95ba51117a45dec35c90" and "b09034d6fb41a6da024669433a2ac287362aaa94" have entirely different histories.

4 changed files with 36 additions and 118 deletions

33
Cargo.lock generated
View File

@ -359,7 +359,6 @@ dependencies = [
"reqwest",
"serde",
"serde_json",
"thiserror",
"tokio",
"zip",
]
@ -822,18 +821,18 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro2"
version = "1.0.85"
version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23"
checksum = "fa1fb82fc0c281dd9671101b66b771ebbe1eaf967b96ac8740dcba4b70005ca8"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500"
dependencies = [
"proc-macro2",
]
@ -1105,9 +1104,9 @@ checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
[[package]]
name = "syn"
version = "2.0.66"
version = "2.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5"
checksum = "a6f671d4b5ffdb8eadec19c0ae67fe2639df8684bd7bc4b83d986b8db549cf01"
dependencies = [
"proc-macro2",
"quote",
@ -1136,26 +1135,6 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "thiserror"
version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "time"
version = "0.3.21"

View File

@ -21,7 +21,6 @@ zip = { version = "0.6.3", optional = true }
mktemp = { version = "0.5.0", optional = true }
rand = { version = "0.8.5", optional = true }
port_scanner = {version = "0.1.5", optional = true}
thiserror = "1.0.61"
[features]
embedded-server = ["zip", "mktemp", "rand", "port_scanner"]

View File

@ -36,11 +36,10 @@
//! 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;
@ -303,27 +302,6 @@ pub struct SuggestResult {
pub suggestions: Vec<String>,
}
#[derive(Debug, Error)]
pub enum Error {
#[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,
@ -357,7 +335,7 @@ impl GrammalecteClient {
///
/// Python 3.7 or higher must is required at runtime
#[cfg(feature = "embedded-server")]
pub fn start_server() -> Result<Self, Error> {
pub fn start_server() -> Result<Self, Box<dyn Error>> {
let server = EmbeddedServer::start()?;
Ok(Self {
base_url: server.base_url(),
@ -366,7 +344,7 @@ impl GrammalecteClient {
}
/// Run spell check on text
pub async fn spell_check(&self, text: &str) -> Result<CheckResult, Error> {
pub async fn spell_check(&self, text: &str) -> Result<CheckResult, Box<dyn Error>> {
self.spell_check_with_options(text, &HashMap::new()).await
}
@ -375,7 +353,7 @@ impl GrammalecteClient {
&self,
text: &str,
options: &HashMap<GramOpt, bool>,
) -> Result<CheckResult, Error> {
) -> Result<CheckResult, Box<dyn Error>> {
let url = format!("{}/gc_text/fr", self.base_url);
log::debug!("Will use URL {} for spell check", url);
@ -383,7 +361,7 @@ impl GrammalecteClient {
.iter()
.map(|t| (t.0.id(), t.1))
.collect::<HashMap<_, _>>();
let options = serde_json::to_string(&options).map_err(Error::OptionJsonSerialization)?;
let options = serde_json::to_string(&options)?;
let mut params = HashMap::new();
params.insert("text", text);
@ -393,17 +371,15 @@ impl GrammalecteClient {
.post(url)
.form(&params)
.send()
.await
.map_err(Error::RequestSendCheckWithOptions)?
.await?
.json::<CheckResult>()
.await
.map_err(Error::CheckResultDeserialize)?;
.await?;
Ok(result)
}
/// Ask for word suggestion
pub async fn suggest(&self, token: &str) -> Result<SuggestResult, Error> {
pub async fn suggest(&self, token: &str) -> Result<SuggestResult, Box<dyn Error>> {
let url = format!("{}/suggest/fr", self.base_url);
log::debug!("Will use URL {} for word suggestion", url);
@ -414,11 +390,9 @@ impl GrammalecteClient {
.post(&url)
.form(&params)
.send()
.await
.map_err(Error::RequestSendSuggest)?
.await?
.json()
.await
.map_err(Error::SuggestDeserialize)?)
.await?)
}
}

View File

@ -1,44 +1,10 @@
use crate::server::utils::{get_free_port, wait_for_port};
use mktemp::Temp;
use std::io::{self, Cursor, Read};
use std::error::Error;
use std::io::{Cursor, Read};
use std::process::{Child, Stdio};
use thiserror::Error;
use zip::result::ZipError;
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 {
_srv_dir: Temp,
port: u16,
@ -47,30 +13,29 @@ pub struct EmbeddedServer {
impl EmbeddedServer {
/// Start embedded Grammalecte server on a random free port
pub fn start() -> Result<Self, Error> {
Self::start_listen_on_port(get_free_port())
pub fn start() -> Result<Self, Box<dyn Error>> {
Self::start_listen_on_port(get_free_port()?)
}
/// Start embedded Grammalecte server on a given port
pub fn start_listen_on_port(port: u16) -> Result<Self, Error> {
pub fn start_listen_on_port(port: u16) -> Result<Self, Box<dyn Error>> {
log::info!("Will start server");
// First, unpack server
let dest = mktemp::Temp::new_dir().map_err(Error::CreateTempDir)?;
let dest = mktemp::Temp::new_dir()?;
let cursor = Cursor::new(include_bytes!("GrammalecteDist.zip"));
let mut zip = ZipArchive::new(cursor).map_err(Error::ZipArchive)?;
let mut zip = ZipArchive::new(cursor)?;
for i in 0..zip.len() {
let mut file = zip.by_index(i).map_err(Error::ZipFileIndex)?;
let mut file = zip.by_index(i)?;
if file.is_dir() {
log::debug!("Create directory: {}", file.name());
std::fs::create_dir_all(dest.join(file.name())).map_err(Error::CreateDirectory)?;
std::fs::create_dir_all(dest.join(file.name()))?;
} else {
log::debug!("Decompress file: {}", file.name());
let mut buff = Vec::with_capacity(file.size() as usize);
file.read_to_end(&mut buff)
.map_err(Error::ZipFileReadToEnd)?;
file.read_to_end(&mut buff)?;
std::fs::write(dest.join(file.name()), buff).map_err(Error::WriteFile)?;
std::fs::write(dest.join(file.name()), buff)?;
}
}
@ -89,8 +54,7 @@ impl EmbeddedServer {
.arg(port.to_string())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.map_err(Error::StartServerProcess)?;
.spawn()?;
wait_for_port(port)?;
@ -114,12 +78,11 @@ impl Drop for EmbeddedServer {
}
mod utils {
use std::io::ErrorKind;
use std::time::Duration;
use super::Error;
/// Get a free port
pub fn get_free_port() -> u16 {
pub fn get_free_port() -> std::io::Result<u16> {
let mut port = 0;
while !(2000..=64000).contains(&port) {
@ -130,10 +93,10 @@ mod utils {
port += 1;
}
port
Ok(port)
}
pub fn wait_for_port(port: u16) -> Result<(), Error> {
pub fn wait_for_port(port: u16) -> std::io::Result<()> {
for _ in 0..50 {
if port_scanner::scan_port(port) {
return Ok(());
@ -141,6 +104,9 @@ mod utils {
std::thread::sleep(Duration::from_millis(100));
}
Err(Error::WaitPortOpen { port })
Err(std::io::Error::new(
ErrorKind::Other,
format!("Port {} did not open in time!", port),
))?
}
}