Compare commits

..

23 Commits

Author SHA1 Message Date
150097f77a Release v0.2.0 (#6)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Reviewed-on: #6
2024-06-14 17:00:16 +00:00
d33202816f Configure Renovate (#1)
All checks were successful
continuous-integration/drone/push Build is passing
Welcome to [Renovate](https://github.com/renovatebot/renovate)! This is an onboarding PR to help you understand and configure settings before regular Pull Requests begin.

🚦 To activate Renovate, merge this Pull Request. To disable Renovate, simply close this Pull Request unmerged.

---
### Detected Package Files

 * `Cargo.toml` (cargo)

### What to Expect

With your current configuration, Renovate will create 8 Pull Requests:

<details>
<summary>Update Rust crate log to v0.4.21</summary>

  - Schedule: ["at any time"]
  - Branch name: `renovate/log-0.x-lockfile`
  - Merge into: `master`
  - Upgrade [log](https://github.com/rust-lang/log) to `0.4.21`

</details>

<details>
<summary>Update Rust crate mktemp to v0.5.1</summary>

  - Schedule: ["at any time"]
  - Branch name: `renovate/mktemp-0.x-lockfile`
  - Merge into: `master`
  - Upgrade [mktemp](https://github.com/samgiles/rs-mktemp) to `0.5.1`

</details>

<details>
<summary>Update Rust crate serde to v1.0.203</summary>

  - Schedule: ["at any time"]
  - Branch name: `renovate/serde-1.x-lockfile`
  - Merge into: `master`
  - Upgrade [serde](https://github.com/serde-rs/serde) to `1.0.203`

</details>

<details>
<summary>Update Rust crate serde_json to v1.0.117</summary>

  - Schedule: ["at any time"]
  - Branch name: `renovate/serde_json-1.x-lockfile`
  - Merge into: `master`
  - Upgrade [serde_json](https://github.com/serde-rs/json) to `1.0.117`

</details>

<details>
<summary>Update Rust crate env_logger to 0.11.0</summary>

  - Schedule: ["at any time"]
  - Branch name: `renovate/env_logger-0.x`
  - Merge into: `master`
  - Upgrade [env_logger](https://github.com/rust-cli/env_logger) to `0.11.0`

</details>

<details>
<summary>Update Rust crate reqwest to 0.12.0</summary>

  - Schedule: ["at any time"]
  - Branch name: `renovate/reqwest-0.x`
  - Merge into: `master`
  - Upgrade [reqwest](https://github.com/seanmonstar/reqwest) to `0.12.0`

</details>

<details>
<summary>Update Rust crate tokio to v1.38.0</summary>

  - Schedule: ["at any time"]
  - Branch name: `renovate/tokio-1.x-lockfile`
  - Merge into: `master`
  - Upgrade [tokio](https://github.com/tokio-rs/tokio) to `1.38.0`

</details>

<details>
<summary>Update Rust crate zip to v2</summary>

  - Schedule: ["at any time"]
  - Branch name: `renovate/zip-2.x`
  - Merge into: `master`
  - Upgrade [zip](https://github.com/zip-rs/zip2) to `2.0.0`

</details>

<br />

🚸 Branch creation will be limited to maximum 2 per hour, so it doesn't swamp any CI resources or overwhelm the project. See docs for `prhourlylimit` for details.

---

 Got questions? Check out Renovate's [Docs](https://docs.renovatebot.com/), particularly the Getting Started section.
If you need any further assistance then you can also [request help here](https://github.com/renovatebot/renovate/discussions).

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).

<!--renovate-config-hash:94693a990c975907e7f13da3309b9d56ba02b3983519b41786edf5cf031e457c-->

Reviewed-on: #1
2024-06-14 16:48:30 +00:00
Gwen Lg
b0543aaba9 add err output when server failed to start
All checks were successful
continuous-integration/drone/push Build is passing
2024-06-14 16:46:39 +00:00
Gwen Lg
597a681801 check if server correctly start
in same time than the port was opened.
Rename function into `wait_for_server` according.
2024-06-14 16:46:39 +00:00
Gwen Lg
74ff8463ba fix grammalecte archive with python files
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
- update bottle.py dependency to v0.12.25
- fix test of python version in grammalecte-server.py
2024-06-14 14:12:16 +02:00
Gwen Lg
1ad5dfbe78 improve error management with thiserror use
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
- add thiserror dependency
- use distinct Error enum in lib and server
2024-06-14 12:05:45 +02:00
Gwen Lg
0d43729a02 update dependencies in Cargo.toml
update: zip, reqwest and env_logger
fix fmt in Cargo.toml
2024-06-14 12:05:05 +02:00
2dd1e5907a Enable CI (#2)
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #2
2024-06-14 06:43:14 +00:00
b09034d6fb Bump Grammalecte version 2023-05-23 14:50:44 +02:00
757692e054 Expose server 2023-05-23 14:46:45 +02:00
7f8a37ce01 Update dependencies 2023-05-23 14:41:19 +02:00
707821ca23 Update metadata 2022-12-20 10:18:19 +01:00
162a5e9af8 Bump version 2022-12-20 10:15:33 +01:00
991dc71c79 Fix bad structure interface 2022-12-20 10:14:57 +01:00
93bd795560 Bump version 2022-12-19 14:21:43 +01:00
17c1b69757 Minor improvments 2022-12-19 14:21:31 +01:00
4ea83ee693 Bump version 2022-12-19 14:05:42 +01:00
47f58820d9 Catch server output 2022-12-19 14:05:16 +01:00
9b13ce1dd3 Prepare for release 2022-12-19 12:34:18 +01:00
af6016ae96 Add documentation 2022-12-19 12:30:33 +01:00
d52bdbde8f Add integrated server 2022-12-19 12:20:44 +01:00
55516ba03a Can set options to spellcheck 2022-12-19 11:19:22 +01:00
8f52bbf121 Add simple suggestion 2022-12-19 10:33:05 +01:00
8 changed files with 1895 additions and 404 deletions

14
.drone.yml Normal file
View File

@@ -0,0 +1,14 @@
---
kind: pipeline
type: docker
name: default
steps:
- name: cargo_check
image: rust
commands:
- rustup component add clippy
- cargo clippy -- -D warnings
- cargo test --all-features

1689
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,19 +1,31 @@
[package] [package]
name = "grammalecte_client" name = "grammalecte_client"
version = "0.1.0" version = "0.1.5"
edition = "2021" edition = "2021"
authors = ["Pierre Hubert <pierre.git@communiquons.org>"] authors = ["Pierre Hubert <pierre.git@communiquons.org>"]
description = "Grammalecte HTTP client" description = "Grammalecte HTTP client"
license = "MIT" license = "MIT"
repository = "https://gitea.communiquons.org/pierre/GrammalecteClient"
keywords = ["grammalecte", "spell-check", "spellcheck"]
readme = "README.md"
categories = ["text-processing"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
serde_json = "1.0.89" serde_json = "1.0.96"
reqwest = { version = "0.11.13", features = ["json"] } reqwest = { version = "0.12.4", features = ["json"] }
serde = { version = "1.0.151", features = ["derive"] } serde = { version = "1.0.163", features = ["derive"] }
log = "0.4.17" log = "0.4.17"
zip = { version = "2.1.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"]
[dev-dependencies] [dev-dependencies]
env_logger = "0.10.0" env_logger = "0.11.3"
tokio = { version = "1.23.0", features = ["full"] } tokio = { version = "1.28.1", features = ["full"] }

7
README.md Normal file
View File

@@ -0,0 +1,7 @@
# Grammalecte client
[![Crate](https://img.shields.io/crates/v/grammalecte_client.svg)](https://crates.io/crates/grammalecte_client)
[![Documentation](https://docs.rs/grammalecte_client/badge.svg)](https://docs.rs/grammalecte_client/)
This crate is an Open Source [Grammalecte](https://grammalecte.net/) client, written in Rust.
Please have a look at this [crate documentation](https://docs.rs/grammalecte_client/latest/grammalecte_client/) to learn more.

9
renovate.json Normal file
View File

@@ -0,0 +1,9 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"packageRules": [
{
"matchUpdateTypes": ["major", "minor", "patch"],
"automerge": true
}
]
}

BIN
src/GrammalecteDist.zip Normal file

Binary file not shown.

View File

@@ -1,5 +1,241 @@
//! # Grammalecte Rust Client
//!
//! This crate is a Rust client to the Grammalecte server API.
//!
//! Grammalecte is an Open Source software that allows to do
//! french spell-checking.
//!
//! ## Integrated server
//! The optional feature `embedded-server` allows you to spin up an
//! temporary web server that will act as Grammalecte backend, instead
//! of targetting an existing instance:
//!
//! ```rust,ignore
//! use grammalecte_client::GrammalecteClient;
//!
//! let msg = "Les ange sont inssuportables!";
//! let res = GrammalecteClient::start_server()
//! .unwrap()
//! .spell_check(msg)
//! .await
//! .unwrap();
//! println!("RESULT = {:#?}", res);
//! ```
//!
//! ## Suggestion
//! You can also ask Grammalecte to give you valid alternatives words:
//! ```rust,ignore
//! use grammalecte_client::GrammalecteClient;
//!
//! let res = GrammalecteClient::start_server()
//! .unwrap()
//! .suggest("bonjou")
//! .await
//! .unwrap();
//! assert!(res.suggestions.contains(&"bonjour".to_string()));
//! println!("RESULT = {:#?}", res);
//! ```
use thiserror::Error;
#[cfg(feature = "embedded-server")]
use crate::server::EmbeddedServer;
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error;
#[cfg(feature = "embedded-server")]
pub mod server;
/// Spell check options
#[derive(Hash, Debug, Eq, PartialEq)]
pub enum GramOpt {
/// Signes typographiques
SignesTypographiques,
/// Apostrophes typographiques
///
/// Correction des apostrophes droites. Automatisme possible dans le menu Outils > Options dautocorrection > Options linguistiques > Guillemets simples > Remplacer (à cocher)
ApostropheTypographique,
/// Ecriture épicène
///
/// Normalisation de lécriture épicène avec points médians.
EcritureEpicene,
/// Espaces surnuméraires
///
/// Signale les espaces inutiles entre les mots, en début et en fin de ligne.
EspacesSurnumeraires,
/// Tabulations surnuméraires
///
/// Signale les tabulations inutiles en début et en fin de ligne.
TabulationsSurnumeraires,
/// Espaces insécables
///
/// Vérifie les espaces insécables avec les ponctuations « ! ? : ; » (à désactiver si vous utilisez une police Graphite)
EspacesInsecables,
/// Majuscules
///
/// Vérifie lutilisation des majuscules et des minuscules (par exemple, « la raison dÉtat », « les Européens »).
Majuscules,
/// Majuscules pour ministères
///
/// Majuscules pour les intitulés des ministères.
MajusuculesMinisteres,
/// Virgules
///
/// Vérifie sil manque une ponctuation finale au paragraphe (seulement pour les paragraphes constitués de plusieurs phrases).
Virgules,
/// Ponctuation finale [!]
///
/// Vérifie sil manque une ponctuation finale au paragraphe (seulement pour les paragraphes constitués de plusieurs phrases).
PonctuationFinale,
/// Traits dunion et soudures
///
/// Cherche les traits dunion manquants ou inutiles.
TraitsUnionEtSoudures,
/// Nombres
///
/// Espaces insécables avant unités de mesure
Nombres,
/// Espaces insécables avant unités de mesure
EspaceInsecableAvantUniteDeMesure,
/// Normes françaises
NormesFrancaises,
/// Signaler ligatures typographiques
///
/// Ligatures de fi, fl, ff, ffi, ffl, ft, st.
LigaturesTypographiques,
/// Apostrophe manquante après lettres isolées [!]
///
/// Apostrophe manquante après les lettres l d s n c j m t ç. Cette option sert surtout à repérer les défauts de numérisation des textes et est déconseillée pour les textes scientifiques.
ApostropheManquanteApresLettreIsolee,
/// Chimie
///
/// Typographie des composés chimiques (H₂O, CO₂, etc.).
Chimie,
/// Erreurs de numérisation (OCR)
///
/// Erreurs de reconnaissance optique des caractères. Beaucoup de faux positifs.
ErreurNumerisation,
/// Noms et adjectifs
Gramm,
/// Confusions et faux-amis
///
/// Cherche des erreurs souvent dues à lhomonymie (par exemple, les confusions entre « faîte » et « faite »).
ConfusionFauxAmis,
/// Locutions
///
/// Écriture des locutions usuelles.
Locutions,
/// Accords (genre et nombre)
///
/// Accords des noms et des adjectifs.
AccordsGenreEtNombre,
/// Verbes
Verbes,
/// Conjugaisons
///
/// Accord des verbes avec leur sujet.
Conjugaisons,
/// Infinitif
///
/// Confusion entre linfinitif et dautres formes.
Infinitif,
/// Impératif
///
/// Vérifie notamment la deuxième personne du singulier (par exemple, les erreurs : « vas… », « prend… », « manges… »).
Imperatif,
/// Interrogatif
///
/// Vérifie les formes interrogatives et suggère de lier les pronoms personnels avec les verbes.
Interrogatif,
/// Participes passés, adjectifs
ParticipePassesEtAdjectifs,
/// Modes verbaux
ModesVerbaux,
/// Style
Style,
/// Populaire
///
/// Souligne un langage courant considéré comme erroné, comme « malgré que ».
Populaire,
/// Pléonasmes
///
/// Repère des redondances sémantiques, comme « au jour daujourdhui », « monter en haut », etc.
Pleonasmes,
/// Élisions et euphonies
///
/// Signale les élisions incorrectes et les tournures dysphoniques.
ElisisonsEtEuphonies,
/// Adverge de négation [!}
///
/// Ne … pas, ne … jamais, etc.
AdverbesNegation,
/// Répétitions dans le paragraphe [!]
///
/// Sont exclus les mots grammaticaux, ceux commençant par une majuscule, ainsi que “être” et “avoir”.
RepetitionsDansParagraphe,
/// Répétitions dans la phrase [!]
///
/// Sont exclus les mots grammaticaux, ainsi que “être” et “avoir”.
RepetitionDansPhrase,
/// Divers
Misc,
/// Mots composés [!]
MotsComposes,
/// Validation des dates
Date,
/// Debugagge
Debug,
/// Affiche lidentifiant de la règle de contrôle dans les messages derreur.
IdControlRule,
}
impl GramOpt {
/// Get the technical ID of the Grammalecte option
pub fn id(&self) -> &'static str {
match self {
GramOpt::SignesTypographiques => "typo",
GramOpt::ApostropheTypographique => "apos",
GramOpt::EcritureEpicene => "eepi",
GramOpt::EspacesSurnumeraires => "esp",
GramOpt::TabulationsSurnumeraires => "tab",
GramOpt::EspacesInsecables => "nbsp",
GramOpt::Majuscules => "maj",
GramOpt::MajusuculesMinisteres => "minis",
GramOpt::Virgules => "virg",
GramOpt::PonctuationFinale => "poncfin",
GramOpt::TraitsUnionEtSoudures => "tu",
GramOpt::Nombres => "num",
GramOpt::EspaceInsecableAvantUniteDeMesure => "unit",
GramOpt::NormesFrancaises => "nf",
GramOpt::LigaturesTypographiques => "liga",
GramOpt::ApostropheManquanteApresLettreIsolee => "mapos",
GramOpt::Chimie => "chim",
GramOpt::ErreurNumerisation => "ocr",
GramOpt::Gramm => "gramm",
GramOpt::ConfusionFauxAmis => "conf",
GramOpt::Locutions => "loc",
GramOpt::AccordsGenreEtNombre => "gn",
GramOpt::Verbes => "verbs",
GramOpt::Conjugaisons => "conj",
GramOpt::Infinitif => "infi",
GramOpt::Imperatif => "imp",
GramOpt::Interrogatif => "inte",
GramOpt::ParticipePassesEtAdjectifs => "ppas",
GramOpt::ModesVerbaux => "vmode",
GramOpt::Style => "style",
GramOpt::Populaire => "bs",
GramOpt::Pleonasmes => "pleo",
GramOpt::ElisisonsEtEuphonies => "eleu",
GramOpt::AdverbesNegation => "neg",
GramOpt::RepetitionsDansParagraphe => "redon1",
GramOpt::RepetitionDansPhrase => "redon2",
GramOpt::Misc => "misc",
GramOpt::MotsComposes => "mc",
GramOpt::Date => "date",
GramOpt::Debug => "debug",
GramOpt::IdControlRule => "idrule",
}
}
}
/// Check spelling result /// Check spelling result
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
@@ -12,6 +248,7 @@ pub struct CheckResult {
pub paragraphs: Vec<Paragraph>, pub paragraphs: Vec<Paragraph>,
} }
/// Check spell result of a given paragraph
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Paragraph { pub struct Paragraph {
#[serde(rename = "iParagraph")] #[serde(rename = "iParagraph")]
@@ -22,6 +259,7 @@ pub struct Paragraph {
pub spelling: Vec<SpellingError>, pub spelling: Vec<SpellingError>,
} }
/// Single grammar error
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct GrammarError { pub struct GrammarError {
#[serde(rename = "nStart")] #[serde(rename = "nStart")]
@@ -35,7 +273,7 @@ pub struct GrammarError {
#[serde(rename = "sType")] #[serde(rename = "sType")]
pub rule_type: String, pub rule_type: String,
#[serde(rename = "aColor")] #[serde(rename = "aColor")]
pub rule_underline_color: Vec<u8>, pub rule_underline_color: Option<Vec<u8>>,
#[serde(rename = "sMessage")] #[serde(rename = "sMessage")]
pub message: String, pub message: String,
#[serde(rename = "aSuggestions")] #[serde(rename = "aSuggestions")]
@@ -44,6 +282,7 @@ pub struct GrammarError {
pub url: String, pub url: String,
} }
/// Spelling error information
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SpellingError { pub struct SpellingError {
pub i: usize, pub i: usize,
@@ -57,14 +296,49 @@ pub struct SpellingError {
pub error_type: String, pub error_type: String,
} }
/// Response to a suggestion request
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SuggestResult {
/// Suggestions returned by Grammalecte
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 { pub struct GrammalecteClient {
base_url: String, base_url: String,
#[cfg(feature = "embedded-server")]
_server: Option<EmbeddedServer>,
} }
impl Default for GrammalecteClient { impl Default for GrammalecteClient {
fn default() -> Self { fn default() -> Self {
Self { Self {
base_url: "http://localhost:8080".to_string(), base_url: "http://localhost:8080".to_string(),
#[cfg(feature = "embedded-server")]
_server: None,
} }
} }
} }
@@ -74,39 +348,126 @@ impl GrammalecteClient {
pub fn new(base_url: &str) -> Self { pub fn new(base_url: &str) -> Self {
Self { Self {
base_url: base_url.to_string(), base_url: base_url.to_string(),
#[cfg(feature = "embedded-server")]
_server: None,
} }
} }
/// Construct a new Grammalecte client, spinning up an associated
/// temporary web server.
///
/// Python 3.7 or higher must is required at runtime
#[cfg(feature = "embedded-server")]
pub fn start_server() -> Result<Self, Error> {
let server = EmbeddedServer::start()?;
Ok(Self {
base_url: server.base_url(),
_server: Some(server),
})
}
/// 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
}
/// Run spell check with custom options
pub async fn spell_check_with_options(
&self,
text: &str,
options: &HashMap<GramOpt, bool>,
) -> Result<CheckResult, Error> {
let url = format!("{}/gc_text/fr", self.base_url); 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 options = options
.iter()
.map(|t| (t.0.id(), t.1))
.collect::<HashMap<_, _>>();
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);
params.insert("options", &options);
let result = reqwest::Client::new() let result = reqwest::Client::new()
.post(url) .post(url)
.form(&params) .form(&params)
.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
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);
reqwest::Client::new()
.post(&url)
.form(&params)
.send()
.await
.map_err(Error::RequestSendSuggest)?
.json()
.await
.map_err(Error::SuggestDeserialize)
}
} }
#[cfg(test)] #[cfg(test)]
#[cfg(feature = "embedded-server")]
mod test { mod test {
use crate::GrammalecteClient; use crate::{GramOpt, GrammalecteClient};
use std::collections::HashMap;
#[tokio::test] #[tokio::test]
async fn simple_correction() { async fn simple_correction() {
let _ = env_logger::builder().is_test(true).try_init(); let _ = env_logger::builder().is_test(true).try_init();
let msg = "Les ange sont inssuportables!"; let msg = "Les ange sont inssuportables!";
let res = GrammalecteClient::default().spell_check(msg).await.unwrap(); let res = GrammalecteClient::start_server()
.unwrap()
.spell_check(msg)
.await
.unwrap();
println!("RESULT = {:#?}", res);
}
#[tokio::test]
async fn customize_options() {
let _ = env_logger::builder().is_test(true).try_init();
let msg = "Bonjour !";
let mut opts = HashMap::new();
opts.insert(GramOpt::EspacesInsecables, false);
let res = GrammalecteClient::start_server()
.unwrap()
.spell_check_with_options(msg, &opts)
.await
.unwrap();
println!("RESULT = {:#?}", res);
assert!(res.paragraphs.is_empty());
}
#[tokio::test]
async fn simple_suggestion() {
let _ = env_logger::builder().is_test(true).try_init();
let res = GrammalecteClient::start_server()
.unwrap()
.suggest("bonjou")
.await
.unwrap();
assert!(res.suggestions.contains(&"bonjour".to_string()));
println!("RESULT = {:#?}", res); println!("RESULT = {:#?}", res);
} }
} }

179
src/server.rs Normal file
View File

@@ -0,0 +1,179 @@
use crate::server::utils::{get_free_port, wait_for_server};
use mktemp::Temp;
use std::io::{self, Cursor, Read};
use std::process::{Child, ExitStatus, Stdio};
use thiserror::Error;
use zip::{result::ZipError, ZipArchive};
#[derive(Debug, Error)]
pub enum Error {
#[error("Grammalecte-server failed to launch process")]
StartServerProcess(#[source] io::Error),
#[error("Get an available port failed")]
GetFreePort(#[source] io::Error),
#[error("Server exit with `{status}`")]
ServerExitWithStatus { status: ExitStatus },
#[error("Server exit with `{status}` :\n{msg}")]
ServerExitWithError { status: ExitStatus, msg: String },
#[error("Error append during check grammalecte-server status")]
ServerCheckStatus(#[source] io::Error),
#[error("Port {port} did not open in time!")]
WaitPortOpen { port: u16 },
#[error("Create temporary directory failed")]
CreateTempDir(#[source] io::Error),
#[error("Zip archive loading failed")]
ZipArchiveLoading(#[source] ZipError),
#[error("Access file by index failed")]
ZipFileIndex(#[source] ZipError),
#[error("Create directory for files from zip")]
CreateDirectoryForZipFile(#[source] io::Error),
#[error("Read file from zip archive")]
ZipFileReadToEnd(#[source] io::Error),
#[error("Write file from archive on disk")]
WriteFile(#[source] io::Error),
}
pub struct EmbeddedServer {
_srv_dir: Temp,
port: u16,
child: Child,
}
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())
}
/// Start embedded Grammalecte server on a given port
pub fn start_listen_on_port(port: u16) -> Result<Self, Error> {
log::info!("Will start server");
// First, unpack server
let dest = mktemp::Temp::new_dir().map_err(Error::CreateTempDir)?;
let cursor = Cursor::new(include_bytes!("GrammalecteDist.zip"));
let mut zip = ZipArchive::new(cursor).map_err(Error::ZipArchiveLoading)?;
for i in 0..zip.len() {
let mut file = zip.by_index(i).map_err(Error::ZipFileIndex)?;
if file.is_dir() {
log::debug!("Create directory: {}", file.name());
std::fs::create_dir_all(dest.join(file.name()))
.map_err(Error::CreateDirectoryForZipFile)?;
} 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)?;
std::fs::write(dest.join(file.name()), buff).map_err(Error::WriteFile)?;
}
}
log::info!("Will start to listen on port {}", port);
let server_file = dest
.join("grammalecte/grammalecte-server.py")
.to_string_lossy()
.to_string();
log::info!("Will execute file {}", server_file);
// Start server
let mut child = std::process::Command::new("/usr/bin/python3")
.arg(server_file)
.arg("-p")
.arg(port.to_string())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.map_err(Error::StartServerProcess)?;
wait_for_server(&mut child, port)?;
Ok(Self {
_srv_dir: dest,
port,
child,
})
}
/// Get embedde instance base URL
pub fn base_url(&self) -> String {
format!("http://localhost:{}", self.port)
}
}
impl Drop for EmbeddedServer {
fn drop(&mut self) {
let _ = self.child.kill();
}
}
mod utils {
use super::Error;
use std::fmt::Write;
use std::io::{BufRead, BufReader};
use std::process::Child;
use std::time::Duration;
/// Get a free port
pub fn get_free_port() -> u16 {
let mut port = 0;
while !(2000..=64000).contains(&port) {
port = rand::random::<u16>() % 64000;
}
while port_scanner::scan_port(port) {
port += 1;
}
port
}
pub fn wait_for_server(child: &mut Child, port: u16) -> Result<(), Error> {
for _ in 0..50 {
check_server(child)?;
if port_scanner::scan_port(port) {
return Ok(());
}
std::thread::sleep(Duration::from_millis(100));
}
Err(Error::WaitPortOpen { port })
}
fn check_server(child: &mut Child) -> Result<(), Error> {
match child.try_wait().map_err(Error::ServerCheckStatus)? {
None => Ok(()), // Continue
Some(status) => {
if let Some(err) = child.stderr.take() {
let mut msg = format!("grammalecte-server exit with `{status}`");
writeln!(&mut msg, " :").unwrap();
let err = BufReader::new(err);
err.lines().for_each(|line| match line {
Ok(line) => {
writeln!(&mut msg, "\t{}", line).unwrap();
}
Err(err) => {
writeln!(&mut msg, "__{err:?}").unwrap();
}
});
Err(Error::ServerExitWithError { status, msg })
} else {
Err(Error::ServerExitWithStatus { status })
}
}
}
}
}