use custom polodb build

This commit is contained in:
DataHearth 2023-05-22 21:54:22 +02:00
parent e89b0fe30e
commit bb6952533e
No known key found for this signature in database
GPG Key ID: E88FD356ACC5F3C4
4 changed files with 46 additions and 21 deletions

18
Cargo.lock generated
View File

@ -30,6 +30,15 @@ dependencies = [
"version_check", "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]] [[package]]
name = "android_system_properties" name = "android_system_properties"
version = "0.1.5" version = "0.1.5"
@ -656,6 +665,7 @@ checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"hashbrown 0.12.3", "hashbrown 0.12.3",
"serde",
] ]
[[package]] [[package]]
@ -919,20 +929,20 @@ dependencies = [
[[package]] [[package]]
name = "polodb_core" name = "polodb_core"
version = "4.2.0" version = "4.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "318217d967df760a6e1b3bf3702b8ba7806474bcad383c47b982e8a30027c124"
dependencies = [ dependencies = [
"bson", "bson",
"byteorder", "byteorder",
"crc64fast", "crc64fast",
"getrandom", "getrandom",
"hashbrown 0.13.2", "hashbrown 0.13.2",
"indexmap",
"js-sys", "js-sys",
"libc", "libc",
"lz4_flex", "lz4_flex",
"memmap2", "memmap2",
"num_enum", "num_enum",
"regex",
"serde", "serde",
"serde-wasm-bindgen", "serde-wasm-bindgen",
"smallvec", "smallvec",
@ -1029,6 +1039,8 @@ version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370" checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370"
dependencies = [ dependencies = [
"aho-corasick",
"memchr",
"regex-syntax", "regex-syntax",
] ]

View File

@ -8,6 +8,7 @@ description = "A Discord bot for the Tech channel"
[dependencies] [dependencies]
once_cell = "1.17.1" once_cell = "1.17.1"
poise = "0.5" 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"] } serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.28.0", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.28.0", features = ["macros", "rt-multi-thread"] }

View File

@ -87,14 +87,24 @@ pub async fn list(ctx: Context<'_>) -> Result<(), Error> {
pub async fn search( pub async fn search(
ctx: Context<'_>, ctx: Context<'_>,
#[description = "Technology name"] technology: String, #[description = "Technology name"] technology: String,
#[description = "Regex options"] options: Option<String>,
) -> Result<(), Error> { ) -> Result<(), Error> {
if let Some(tech) = search_tech(technology.clone())? { let found_techs = search_tech(technology, options.map_or(String::new(), |opts| opts))?;
ctx.say(format!("Name: {}\nLink: {}", tech.name, tech.link)) if found_techs.len() == 0 {
.await?; ctx.say("No technologies found").await?;
} else { return Ok(());
ctx.say("No technology found").await?;
} }
ctx.say(format!(
"Found technologies: {}",
found_techs
.iter()
.map(|tech| format!("Name: {} => Link: {}", tech.name, tech.link))
.collect::<Vec<String>>()
.join("\n")
))
.await?;
Ok(()) Ok(())
} }

View File

@ -1,5 +1,8 @@
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use polodb_core::{bson::doc, Database}; use polodb_core::{
bson::{doc, Regex},
Database,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::io::{Error, ErrorKind}; use std::io::{Error, ErrorKind};
@ -49,21 +52,20 @@ pub fn list_tech() -> Result<Vec<Technology>, Error> {
.collect()) .collect())
} }
pub fn search_tech(name: String) -> Result<Option<Technology>, Error> { pub fn search_tech(name: String, options: String) -> Result<Vec<Technology>, Error> {
if let Some(tech) = DB Ok(DB
.get() .get()
.unwrap() .unwrap()
.collection::<Technology>("technologies") .collection::<Technology>("technologies")
.find(doc! { "name": {"$eq": name} }) .find(doc! { "name": {"$regex": Regex {
pattern: name,
options: options,
}} })
.map_err(|err| Error::new(ErrorKind::InvalidInput, err))? .map_err(|err| Error::new(ErrorKind::InvalidInput, err))?
.next() .map(
{ |doc| doc.unwrap(), // todo: find a way to handle error
Ok(Some( )
tech.map_err(|err| Error::new(ErrorKind::InvalidInput, err))?, .collect::<Vec<Technology>>())
))
} else {
Ok(None)
}
} }
pub fn set_auth_user(discord_id: String) -> Result<(), Error> { pub fn set_auth_user(discord_id: String) -> Result<(), Error> {