diff --git a/Cargo.lock b/Cargo.lock index 5741e45..eb64a00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,6 +30,15 @@ dependencies = [ "version_check", ] +[[package]] +name = "aho-corasick" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04" +dependencies = [ + "memchr", +] + [[package]] name = "android_system_properties" version = "0.1.5" @@ -656,6 +665,7 @@ checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", "hashbrown 0.12.3", + "serde", ] [[package]] @@ -919,20 +929,20 @@ dependencies = [ [[package]] name = "polodb_core" -version = "4.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "318217d967df760a6e1b3bf3702b8ba7806474bcad383c47b982e8a30027c124" +version = "4.3.1" dependencies = [ "bson", "byteorder", "crc64fast", "getrandom", "hashbrown 0.13.2", + "indexmap", "js-sys", "libc", "lz4_flex", "memmap2", "num_enum", + "regex", "serde", "serde-wasm-bindgen", "smallvec", @@ -1029,6 +1039,8 @@ version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370" dependencies = [ + "aho-corasick", + "memchr", "regex-syntax", ] diff --git a/Cargo.toml b/Cargo.toml index fdb3463..91058e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ description = "A Discord bot for the Tech channel" [dependencies] once_cell = "1.17.1" poise = "0.5" -polodb_core = "4.2.0" +polodb_core = { path = "../PoloDB/src/polodb_core" } +# polodb_core = "4.2.0" serde = { version = "1.0", features = ["derive"] } tokio = { version = "1.28.0", features = ["macros", "rt-multi-thread"] } diff --git a/src/commands.rs b/src/commands.rs index 42732cc..bf381bb 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -87,14 +87,24 @@ pub async fn list(ctx: Context<'_>) -> Result<(), Error> { pub async fn search( ctx: Context<'_>, #[description = "Technology name"] technology: String, + #[description = "Regex options"] options: Option, ) -> Result<(), Error> { - if let Some(tech) = search_tech(technology.clone())? { - ctx.say(format!("Name: {}\nLink: {}", tech.name, tech.link)) - .await?; - } else { - ctx.say("No technology found").await?; + let found_techs = search_tech(technology, options.map_or(String::new(), |opts| opts))?; + if found_techs.len() == 0 { + ctx.say("No technologies found").await?; + return Ok(()); } + ctx.say(format!( + "Found technologies: {}", + found_techs + .iter() + .map(|tech| format!("Name: {} => Link: {}", tech.name, tech.link)) + .collect::>() + .join("\n") + )) + .await?; + Ok(()) } diff --git a/src/database.rs b/src/database.rs index 516b65a..52770c5 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,5 +1,8 @@ use once_cell::sync::OnceCell; -use polodb_core::{bson::doc, Database}; +use polodb_core::{ + bson::{doc, Regex}, + Database, +}; use serde::{Deserialize, Serialize}; use std::io::{Error, ErrorKind}; @@ -49,21 +52,20 @@ pub fn list_tech() -> Result, Error> { .collect()) } -pub fn search_tech(name: String) -> Result, Error> { - if let Some(tech) = DB +pub fn search_tech(name: String, options: String) -> Result, Error> { + Ok(DB .get() .unwrap() .collection::("technologies") - .find(doc! { "name": {"$eq": name} }) + .find(doc! { "name": {"$regex": Regex { + pattern: name, + options: options, + }} }) .map_err(|err| Error::new(ErrorKind::InvalidInput, err))? - .next() - { - Ok(Some( - tech.map_err(|err| Error::new(ErrorKind::InvalidInput, err))?, - )) - } else { - Ok(None) - } + .map( + |doc| doc.unwrap(), // todo: find a way to handle error + ) + .collect::>()) } pub fn set_auth_user(discord_id: String) -> Result<(), Error> {