diff --git a/Cargo.lock b/Cargo.lock index 6082c65..bf8f65e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "actix-codec" @@ -243,6 +243,38 @@ version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04" +[[package]] +name = "argh" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34ff18325c8a36b82f992e533ece1ec9f9a9db446bd1c14d4f936bac88fcd240" +dependencies = [ + "argh_derive", + "argh_shared", + "rust-fuzzy-search", +] + +[[package]] +name = "argh_derive" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb7b2b83a50d329d5d8ccc620f5c7064028828538bdf5646acd60dc1f767803" +dependencies = [ + "argh_shared", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "argh_shared" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a464143cc82dedcdc3928737445362466b7674b5db4e2eb8e869846d6d84f4f6" +dependencies = [ + "serde", +] + [[package]] name = "autocfg" version = "1.4.0" @@ -1046,12 +1078,19 @@ dependencies = [ "walkdir", ] +[[package]] +name = "rust-fuzzy-search" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a157657054ffe556d8858504af8a672a054a6e0bd9e8ee531059100c0fa11bb2" + [[package]] name = "rust-name-gen" version = "0.1.0" dependencies = [ "actix-web", "anyhow", + "argh", "rand", "rust-embed", "serde", diff --git a/Cargo.toml b/Cargo.toml index 01e6fd7..43eee3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] actix-web = "4" anyhow = "1.0.95" +argh = "0.1.13" rand = "0.8.5" rust-embed = "8.5.0" serde = { version = "1.0", features = ["derive"] } diff --git a/bruno/NameGen/index.bru b/bruno/NameGen/index.bru index 6ecffae..1a204d4 100644 --- a/bruno/NameGen/index.bru +++ b/bruno/NameGen/index.bru @@ -5,15 +5,12 @@ meta { } get { - url: 127.0.0.1:8080 + url: 127.0.0.1:8080?gender=f&dictionaries=prenom patronyme article commun adjectif body: json auth: none } -body:json { - { - "qty": 50, - "gender": "f", - "dictionaries": ["prenom", "patronyme", "virgule", "article", "commun", "adjectif", "ppasse", "ppresent"] - } +params:query { + gender: f + dictionaries: prenom patronyme article commun adjectif } diff --git a/src/main.rs b/src/main.rs index 4f9b206..a00572e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,43 +1,60 @@ -use std::{collections::HashMap, str::from_utf8}; +use std::{collections::HashMap, str::from_utf8, vec}; use actix_web::{get, web, App, HttpResponse, HttpServer}; +use argh::FromArgs; use rand::seq::SliceRandom; use rust_embed::Embed; use serde::{Deserialize, Serialize}; static ARTICLE: &str = "
"; +#[derive(FromArgs)] +#[argh(description = "arguments")] +struct Args { + #[argh(option, short = 'h', description = "host")] + host: Option, + #[argh(option, short = 'p', description = "port")] + port: Option, +} + #[derive(Embed)] #[folder = "dictionaries/"] struct Dictionaries; #[derive(Debug, Serialize, Deserialize)] struct NameGenQuery { - dictionaries: Vec, - gender: String, + dictionaries: Option, + gender: Option, qty: Option, } #[get("/")] -async fn index(query: web::Json) -> HttpResponse { +async fn index(query: web::Query) -> HttpResponse { // Load the dictionaries into memory let mut dictionaries = HashMap::new(); for filename in Dictionaries::iter() { if let Some(file) = Dictionaries::get(&filename) { let data = from_utf8(file.data.as_ref()).unwrap(); - dictionaries.insert(filename.to_string(), serde_json::from_str::>(data).unwrap()); + dictionaries.insert( + filename.to_string(), + serde_json::from_str::>(data).unwrap(), + ); } } let qty = query.0.qty.unwrap_or(10).min(50).max(1); - let gender = match query.0.gender.as_str() { + let gender = match query.0.gender.unwrap_or("m".to_string()).as_str() { "m" => "masculins", "f" => "feminins", _ => "masculins", }; // Limit to 10 parts max - let mut chosen_dicts = query.0.dictionaries; + let q_dicts = query + .0 + .dictionaries + .unwrap_or("prenom patronyme".to_string()); + let mut chosen_dicts = q_dicts.split(" ").collect::>(); chosen_dicts.truncate(10); let mut names = vec![]; @@ -46,7 +63,7 @@ async fn index(query: web::Json) -> HttpResponse { let mut parts = vec![]; for dict in chosen_dicts.iter() { - let filename = match dict.as_str() { + let filename = match *dict { "adjectif" | "commun" | "ppasse" | "ppresent" | "prenom" => { Some(format!("{dict}s_{gender}.json")) } @@ -71,9 +88,9 @@ async fn index(query: web::Json) -> HttpResponse { if item == ARTICLE { // Check the next word if let Some(next) = parts.get(i + 1) { - if next - .starts_with(|c| vec!['a', 'e', 'i', 'o', 'u', 'é', 'è', 'ê', 'h'].contains(&c)) - { + if next.starts_with(|c| { + vec!['a', 'e', 'i', 'o', 'u', 'é', 'è', 'ê', 'h'].contains(&c) + }) { to_replace.push((i, "l'")); } else { to_replace.push((i, (if gender == "masculins" { "le" } else { "la" }))) @@ -100,8 +117,12 @@ async fn index(query: web::Json) -> HttpResponse { #[actix_web::main] // or #[tokio::main] async fn main() -> std::io::Result<()> { + let args: Args = argh::from_env(); + let host = args.host.unwrap_or("127.0.0.1".to_string()); + let port = args.port.unwrap_or(8080); + HttpServer::new(|| App::new().service(index)) - .bind(("127.0.0.1", 8080))? + .bind((host, port))? .run() .await }