vortaroboto

Log | Files | Refs | README

commit 606c0aa70c46d415b5009ac907d11a8445632cc2
parent daf09e376707b134a362f74f9a2815a88cc1dfee
Author: tomvig38@gmail.com <tomvig38@gmail.com>
Date:   Mon, 18 Oct 2021 06:15:16 +0000

Aldonu vort'faradan komandon
Diffstat:
Msrc/main.rs | 136+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
1 file changed, 98 insertions(+), 38 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -53,6 +53,20 @@ struct Vorto { vorto: String, } +async fn trovu(vorto: &str) -> Result<Vorto, String> { + reqwest::get(format!("{}/{}", VORTO_URL, vorto)) + .and_then(|j| j.json::<Vorto>()) + .map_err(|e| e.to_string()) + .await +} + +async fn difinu(vorto: &str) -> Result<Trovo, String> { + reqwest::get(format!("{}/{}", TROVI_URL, vorto)) + .and_then(|j| j.json::<Trovo>()) + .map_err(|e| e.to_string()) + .await +} + #[tokio::main] async fn main() -> irc::error::Result<()> { let config = Config { @@ -81,7 +95,7 @@ async fn main() -> irc::error::Result<()> { if let Some(answer) = handle_command(cmd).await { let r = match (target.starts_with('#'), message.source_nickname()) { (true, Some(n)) => format!("{}:\r\n{}", n, answer), - _ => answer + _ => answer, }; sender.send_privmsg(target, r)?; } @@ -100,19 +114,25 @@ async fn handle_command(cmd: &str) -> Option<String> { Some("helpu") => Some(String::from( r#" helpu: montri ĉi tio mesaĝo - difinu [vorto]: difini vorton + difinu [vorto] [numero]: difini [vorto], uzas la ebla [numero] + traduku [vorto] [lingvo]: traduko [vorto] al la (ebla) [lingvo] "#, )), Some("difinu" | "d") => { if let Some(w) = splitted.next() { - match define_word(w).await { + let index = if let Some(s) = splitted.next() { + s.parse::<usize>().ok() + } else { + None + }; + match define_word(w, index).await { Ok(r) => Some(r), Err(e) => Some(format!("Nenio trovata pri: {} ({})", w, e.to_string())), } } else { - Some(String::from("Uzo: difinu [vorto]")) + Some(String::from("Uzo: difinu [vorto] [numero]")) } - }, + } Some("traduku" | "trad" | "t") => { if let Some(w) = splitted.next() { match traduki(w, splitted.next()).await { @@ -120,36 +140,47 @@ async fn handle_command(cmd: &str) -> Option<String> { Err(e) => Some(format!("Nenio trovata pri: {} ({})", w, e.to_string())), } } else { - Some(String::from("Uzo: traduku [vorto]")) + Some(String::from("Uzo: traduku [vorto] [lingv'kodo]")) + } + } + Some("vortfarado" | "v" | "vf") => { + if let Some(w) = splitted.next() { + match vortfarado(w).await { + Ok(r) => Some(r), + Err(e) => Some(format!("Nenio trovata pri: {} ({})", w, e.to_string())), + } + } else { + Some(String::from("Uzo: vortfarado [vorto]")) } - }, + } Some(u) => Some(format!("Mi ne scias kio respondi al: {}", u)), None => None, } } -async fn define_word(word: &str) -> Result<String, String> { - let res = reqwest::get(format!("{}/{}", VORTO_URL, word)) - .await - .map_err(|e| e.to_string())? - .json::<Vorto>() - .await - .map_err(|e| e.to_string())?; +async fn define_word(vorto: &str, difino: Option<usize>) -> Result<String, String> { + let res: Vorto = trovu(vorto).await?; + + if res.difinoj.is_empty() { + return Err(format!("{} ne havas difino.", vorto)); + } - if let Some(d) = res.difinoj.get(0) { - Ok(format!("{}: {}", word, d.difino)) + let index = difino.unwrap_or(0).clamp(0, res.difinoj.len()); + if let Some(d) = res.difinoj.get(index) { + Ok(format!( + "Difino {} el {} por {}: {}", + index + 1, + res.difinoj.len(), + vorto, + d.difino + )) } else { - Err(format!("{} ne havas difino.", word)) + unreachable!(); } } async fn traduki(vorto: &str, fonto: Option<&str>) -> Result<String, String> { - let res = reqwest::get(format!("{}/{}", TROVI_URL, vorto)) - .await - .map_err(|e| e.to_string())? - .json::<Trovo>() - .await - .map_err(|e| e.to_string())?; + let res: Trovo = difinu(vorto).await?; if res.tradukoj.is_empty() { return Err(format!("Nenio traduko trovita por {}", vorto)); @@ -157,26 +188,55 @@ async fn traduki(vorto: &str, fonto: Option<&str>) -> Result<String, String> { let tradukoj = res.tradukoj.iter(); let tradukoj: Vec<String> = if let Some(f) = fonto { - tradukoj.filter_map(|t: &Traduko| { - if let Some(v) = &t.vorto { - if f == t.kodo { - Some(v.clone()) + tradukoj + .filter_map(|t: &Traduko| { + if let Some(v) = &t.vorto { + if f == t.kodo { + Some(v.clone()) + } else { + None + } } else { None } - } else { - None - } - }).collect() + }) + .collect() } else { - tradukoj.filter_map(|t: &Traduko| { - if let Some(v) = &t.vorto { - Some(format!("{} \"{}\": {}", t.lingvo, t.traduko, v)) - } else { - None - } - }).collect() + tradukoj + .filter_map(|t: &Traduko| { + if let Some(v) = &t.vorto { + Some(format!("{} \"{}\": {}", t.lingvo, t.traduko, v)) + } else { + None + } + }) + .collect() }; Ok(tradukoj.join("\r\n")) } + +async fn vortfarado(vorto: &str) -> Result<String, String> { + let res: Trovo = difinu(vorto).await?; + + let vf = res.vortfarado; + + if let Some(v) = vf.get(0) { + let v = v + .partoj + .iter() + .map(|p| { + if let Some(pv) = &p.vorto { + format!("{} ({})", p.parto, pv) + } else { + p.parto.to_owned() + } + }) + .collect::<Vec<String>>() + .join(" + "); + + Ok(v) + } else { + Err(format!("{} ne havas vort'faradon.", vorto)) + } +}