forked from pierre/GrammalecteClient
First request to Grammalecte server
This commit is contained in:
112
src/lib.rs
Normal file
112
src/lib.rs
Normal file
@ -0,0 +1,112 @@
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
|
||||
/// Check spelling result
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct CheckResult {
|
||||
pub program: String,
|
||||
pub version: String,
|
||||
pub lang: String,
|
||||
pub error: String,
|
||||
#[serde(rename = "data")]
|
||||
pub paragraphs: Vec<Paragraph>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Paragraph {
|
||||
#[serde(rename = "iParagraph")]
|
||||
pub num: usize,
|
||||
#[serde(rename = "lGrammarErrors")]
|
||||
pub grammars: Vec<GrammarError>,
|
||||
#[serde(rename = "lSpellingErrors")]
|
||||
pub spelling: Vec<SpellingError>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct GrammarError {
|
||||
#[serde(rename = "nStart")]
|
||||
pub offset_start: usize,
|
||||
#[serde(rename = "nEnd")]
|
||||
pub offset_end: usize,
|
||||
#[serde(rename = "sLineId")]
|
||||
pub rule_line_id: String,
|
||||
#[serde(rename = "sRuleId")]
|
||||
pub rule_id: String,
|
||||
#[serde(rename = "sType")]
|
||||
pub rule_type: String,
|
||||
#[serde(rename = "aColor")]
|
||||
pub rule_underline_color: Vec<u8>,
|
||||
#[serde(rename = "sMessage")]
|
||||
pub message: String,
|
||||
#[serde(rename = "aSuggestions")]
|
||||
pub suggestions: Vec<String>,
|
||||
#[serde(rename = "URL")]
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct SpellingError {
|
||||
pub i: usize,
|
||||
#[serde(rename = "nStart")]
|
||||
pub offset_start: usize,
|
||||
#[serde(rename = "nEnd")]
|
||||
pub offset_end: usize,
|
||||
#[serde(rename = "sValue")]
|
||||
pub bad_word: String,
|
||||
#[serde(rename = "sType")]
|
||||
pub error_type: String,
|
||||
}
|
||||
|
||||
pub struct GrammalecteClient {
|
||||
base_url: String,
|
||||
}
|
||||
|
||||
impl Default for GrammalecteClient {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
base_url: "http://localhost:8080".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GrammalecteClient {
|
||||
/// Construct a new Grammalecte client, with a custom server URL
|
||||
pub fn new(base_url: &str) -> Self {
|
||||
Self {
|
||||
base_url: base_url.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Run spell check on text
|
||||
pub async fn spell_check(&self, text: &str) -> Result<CheckResult, Box<dyn Error>> {
|
||||
let url = format!("{}/gc_text/fr", self.base_url);
|
||||
log::info!("Will use URL {}", url);
|
||||
|
||||
let mut params = HashMap::new();
|
||||
params.insert("text", text);
|
||||
|
||||
let result = reqwest::Client::new()
|
||||
.post(url)
|
||||
.form(¶ms)
|
||||
.send()
|
||||
.await?
|
||||
.json::<CheckResult>()
|
||||
.await?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::GrammalecteClient;
|
||||
|
||||
#[tokio::test]
|
||||
async fn simple_correction() {
|
||||
let _ = env_logger::builder().is_test(true).try_init();
|
||||
|
||||
let msg = "Les ange sont inssuportables!";
|
||||
let res = GrammalecteClient::default().spell_check(msg).await.unwrap();
|
||||
println!("RESULT = {:#?}", res);
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
Reference in New Issue
Block a user