vortaroboto

Log | Files | Refs | README

commit 9c2cd5f53f3797ebf88596384101de1f989b6637
parent 5c041add51536fb4d0fddb57185d0e34f4ccbe0c
Author: tomvig38@gmail.com <tomvig38@gmail.com>
Date:   Wed, 20 Oct 2021 08:36:53 +0000

Aldonu etimologi'funkcion
Diffstat:
MCargo.lock | 17+++++++++++++++++
MCargo.toml | 2++
Aetim.bincode | 0
Msrc/main.rs | 30++++++++++++++++++++++++++----
4 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -26,6 +26,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" [[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + +[[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -834,6 +843,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ecab6c735a6bb4139c0caafd0cc3635748bbb3acf4550e8138122099251f309" [[package]] +name = "snap" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45456094d1983e2ee2a18fdfebce3189fa451699d0502cb8e3b49dba5ba41451" + +[[package]] name = "socket2" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1047,6 +1062,7 @@ dependencies = [ name = "vortaroboto" version = "0.1.0" dependencies = [ + "bincode", "futures", "irc", "log", @@ -1054,6 +1070,7 @@ dependencies = [ "serde", "serde_json", "simple_logger", + "snap", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml @@ -14,3 +14,5 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.68" log = "0.4" simple_logger = "1.13.0" +bincode = "1.3.3" +snap = "1.0.5" diff --git a/etim.bincode b/etim.bincode Binary files differ. diff --git a/src/main.rs b/src/main.rs @@ -1,7 +1,7 @@ use futures::prelude::*; use irc::client::prelude::*; use serde::Deserialize; -use std::str::FromStr; +use std::{collections::HashMap, str::FromStr}; use log::{debug, info}; use simple_logger::SimpleLogger; @@ -165,8 +165,8 @@ macro_rules! parse_or_default { } macro_rules! wrap_handler { - ($f:ident; $w:expr; $($args:expr),+) => { - match $f($w, $($args),+).await { + ($f:ident; $w:expr; $($args:expr),*) => { + match $f($w, $($args),*).await { Ok(r) => Some(r), Err(e) => Some(format!("Nenio trovata pri: {} ({})", $w, e.to_string())), } @@ -197,7 +197,14 @@ async fn handle_command(cmd: &str) -> Option<String> { let index = parse_or_default!(NumerSelektilo, splitted); wrap_handler!(vortfarado; w; index) } else { - Some(String::from("Uzo: vortfarado [vorto]")) + Some(String::from("Uzo: vortfarado {vorto} [numero]")) + } + } + Some("etimologio" | "etim" | "e") => { + if let Some(w) = splitted.next() { + wrap_handler!(etimologio; w;) + } else { + Some(String::from("Uzo: etimologio {vorto}")) } } Some(u) => Some(format!("Mi ne scias kiel respondi al: {}", u)), @@ -226,6 +233,10 @@ fn helpu() -> String { &["numero"], "doni la vortfarado [numero] por {vorto}", ), + ("etimolodio", + &["vorto"], + &[], + "Doni la etimolodio de {vorto}") ]; fn format_args(am: &[&str], left: char, right: char, c: ColorKind) -> String { @@ -283,6 +294,17 @@ fn helpu() -> String { lineoj.join(LINEO_SEP) } +async fn etimologio(vorto: &str) -> Result<String, String> { + let r = snap::read::FrameDecoder::new( + std::fs::File::open("etim.bincode").map_err(|e| e.to_string())?, + ); + let h: HashMap<String, String> = bincode::deserialize_from(r).map_err(|e| e.to_string())?; + + Ok(h.get(vorto) + .map(|e| e.to_owned()) + .unwrap_or(format!("Nenio trovata por {}", vorto))) +} + async fn define_word(vorto: &str, difino: NumerSelektilo) -> Result<String, String> { let res: Vorto = trovu(vorto).await?;