simple CLI app
This commit is contained in:
parent
1088b94577
commit
9421d61b91
1194
Cargo.lock
generated
1194
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
|
@ -4,8 +4,6 @@ version = "0.1.0"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4"
|
||||
anyhow = "1.0.95"
|
||||
argh = "0.1.13"
|
||||
rand = "0.8.5"
|
||||
rust-embed = "8.5.0"
|
||||
|
|
29
README.md
29
README.md
|
@ -2,24 +2,31 @@
|
|||
|
||||
## Utilisation
|
||||
|
||||
Les deux arguments sont facultatifs
|
||||
```sh
|
||||
./rust-name-gen --host 127.0.0.1 --port 8080
|
||||
./rust-name-gen --sequence "prenom patronyme"
|
||||
```
|
||||
|
||||
### Query params pour générer les noms
|
||||
### Pendant le developpement
|
||||
|
||||
- `qty` - Nombre de noms générés, entre `1` et `50`. Défaut `10`.
|
||||
- `sep` - Le caractère de séparation entre les sections des noms générés. Utilise l'espace par défaut.
|
||||
- `gender` - Genre des noms générés, `m` ou `f`. Défaut au hasard.
|
||||
- `sequence` - Une suite de mots-clés pour déterminer la structure du nom, séparés par un espace. Valeurs acceptés : `prenom patronyme commun adjectif ppasse ppresent article virgule`
|
||||
```sh
|
||||
cargo run -- --sequence "prenom patronyme"
|
||||
```
|
||||
|
||||
### Arguments
|
||||
|
||||
Tous les arguments sont facultatifs
|
||||
|
||||
- `--qty` - Nombre de noms générés, entre `1` et `50`. Défaut `10`.
|
||||
- `--sep` - Le caractère de séparation entre les sections des noms générés. Utilise l'espace par défaut.
|
||||
- `--gender` - Genre des noms générés, `m` ou `f`. Défaut au hasard.
|
||||
- `--sequence` - Une suite de mots-clés pour déterminer la structure du nom, séparés par un espace. Valeurs acceptés : `prenom patronyme commun adjectif ppasse ppresent article virgule`. Défaut `prenom patronyme`
|
||||
- `ppasse`/`ppresent` pour "participe passé/présent".
|
||||
- `article` sera transformé en `le`/`la`/`l'`.
|
||||
- `virgule` est... une virgule.
|
||||
|
||||
Exemple :
|
||||
```http
|
||||
GET 127.0.0.1:8080?qty=5&sequence=prenom%20patronyme%20virgule%20article%20commun%20adjectif%20ppresent%20ppasse
|
||||
./rust-name-gen --qty 5 --sequence "prenom patronyme virgule article commun adjectif ppresent ppasse"
|
||||
```
|
||||
|
||||
Résultat :
|
||||
|
@ -33,12 +40,6 @@ Résultat :
|
|||
]
|
||||
```
|
||||
|
||||
## Live reload
|
||||
|
||||
```
|
||||
cargo watch -x run
|
||||
```
|
||||
|
||||
## Build de production
|
||||
|
||||
```
|
||||
|
|
64
src/main.rs
64
src/main.rs
|
@ -1,10 +1,8 @@
|
|||
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 = "<article>";
|
||||
static GENDER_M: &str = "masculins";
|
||||
|
@ -13,27 +11,22 @@ static RANDOM: &str = "random";
|
|||
|
||||
#[derive(FromArgs)]
|
||||
#[argh(description = "arguments")]
|
||||
struct Args {
|
||||
#[argh(option, short = 'h', description = "host")]
|
||||
host: Option<String>,
|
||||
#[argh(option, short = 'p', description = "port")]
|
||||
port: Option<u16>,
|
||||
struct Arguments {
|
||||
#[argh(option, description = "nombre de noms à générer")]
|
||||
qty: Option<u8>,
|
||||
#[argh(option, description = "separateur")]
|
||||
sep: Option<String>,
|
||||
#[argh(option, description = "gender")]
|
||||
gender: Option<String>,
|
||||
#[argh(option, description = "sequence")]
|
||||
sequence: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Embed)]
|
||||
#[folder = "dictionaries/"]
|
||||
struct Dictionaries;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct NameGenQuery {
|
||||
sequence: Option<String>,
|
||||
gender: Option<String>,
|
||||
qty: Option<u8>,
|
||||
sep: Option<String>,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn index(query: web::Query<NameGenQuery>) -> HttpResponse {
|
||||
fn main() {
|
||||
// Load the dictionaries into memory
|
||||
let mut dictionaries = HashMap::new();
|
||||
for filename in Dictionaries::iter() {
|
||||
|
@ -46,16 +39,17 @@ async fn index(query: web::Query<NameGenQuery>) -> HttpResponse {
|
|||
}
|
||||
}
|
||||
|
||||
let qty = query.0.qty.unwrap_or(10).min(50).max(1);
|
||||
let sep = query.0.sep.unwrap_or(" ".to_string());
|
||||
let gender = match query.0.gender.as_ref().map(String::as_ref) {
|
||||
let args: Arguments = argh::from_env();
|
||||
let qty = args.qty.unwrap_or(10).min(50).max(1);
|
||||
let sep = args.sep.unwrap_or(" ".to_string());
|
||||
let gender = match args.gender.as_ref().map(String::as_ref) {
|
||||
Some("m") => GENDER_M,
|
||||
Some("f") => GENDER_F,
|
||||
_ => RANDOM,
|
||||
};
|
||||
|
||||
// Limit to 10 parts max
|
||||
let q_dicts = query.0.sequence.unwrap_or("prenom patronyme".to_string());
|
||||
let q_dicts = args.sequence.unwrap_or("prenom patronyme".to_string());
|
||||
let mut chosen_dicts = q_dicts.split(" ").collect::<Vec<_>>();
|
||||
chosen_dicts.truncate(10);
|
||||
|
||||
|
@ -71,7 +65,6 @@ async fn index(query: web::Query<NameGenQuery>) -> HttpResponse {
|
|||
} else {
|
||||
gender
|
||||
};
|
||||
println!("{gender}");
|
||||
|
||||
for dict in chosen_dicts.iter() {
|
||||
let filename = match *dict {
|
||||
|
@ -100,12 +93,12 @@ async fn index(query: web::Query<NameGenQuery>) -> HttpResponse {
|
|||
// 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)
|
||||
} && !next.starts_with("hy")) {
|
||||
to_replace.push((i, "l'"));
|
||||
} else {
|
||||
to_replace.push((i, (if gender == "masculins" { "le" } else { "la" })))
|
||||
}
|
||||
vec!['a', 'e', 'i', 'o', 'u', 'é', 'è', 'ê', 'h'].contains(&c)
|
||||
} && !next.starts_with("hy")) {
|
||||
to_replace.push((i, "l'"));
|
||||
} else {
|
||||
to_replace.push((i, (if gender == "masculins" { "le" } else { "la" })))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -122,18 +115,5 @@ async fn index(query: web::Query<NameGenQuery>) -> HttpResponse {
|
|||
|
||||
names.push(joined);
|
||||
}
|
||||
|
||||
HttpResponse::Ok().json(names)
|
||||
}
|
||||
|
||||
#[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((host, port))?
|
||||
.run()
|
||||
.await
|
||||
println!("{}", serde_json::to_string(&names).unwrap());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user