diff --git a/Cargo.toml b/Cargo.toml index ddc6bc4..9bfe411 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [dependencies] tracing = "*" -regex = "*" +regex = "1" tokio-postgres = "*" rand = "*" diff --git a/ci/pipeline.yml b/ci/pipeline.yml index c36989f..454d370 100644 --- a/ci/pipeline.yml +++ b/ci/pipeline.yml @@ -56,11 +56,19 @@ jobs: - get: rust-latest-image trigger: false - task: format + image: rust-latest-image config: + inputs: + - name: singh3-repo + platform: linux run: - path: cargo - args: ["fmt"] - dir: singh3-bin + path: sh + args: + - -c + - | + rustup component add rustfmt + cargo fmt + dir: singh3-repo - task: build image: rust-latest-image config: diff --git a/flake.nix b/flake.nix index 3163bd6..60ce66c 100644 --- a/flake.nix +++ b/flake.nix @@ -7,31 +7,56 @@ rust-overlay.url = github:oxalica/rust-overlay; }; - outputs = { self, nixpkgs, utils, rust-overlay }: - utils.lib.eachDefaultSystem - (system: - let - overlays = [ (import rust-overlay) ]; - pkgs = import nixpkgs { - inherit system overlays; - }; - in - { - devShell = with pkgs; mkShell { - buildInputs = [ - rust-bin.nightly.latest.default - rust-analyzer - ]; - }; - defaultPackage = pkgs.rustPlatform.buildRustPackage rec { - pname = "singh3"; - version = "0.1.0"; - src = ./. ; - nativeBuildInputs = with pkgs; [ - rust-bin.nightly.latest.default - ]; - cargoSha256 = "sha256-04yTexSkFpa3KQKVvfi7NM1j4V7m08kHDqw98bxXT5M="; - }; - } - ); + outputs = { self, nixpkgs, utils, rust-overlay }: + utils.lib.eachDefaultSystem + (system: + let + overlays = [ (import rust-overlay) ]; + pkgs = import nixpkgs { + inherit system overlays; + }; + in + rec { + devShells = with pkgs; { + default = mkShell + { + buildInputs = [ + rust-bin.nightly.latest.default + rust-analyzer + postgresql + ]; + }; + withDB = mkShell + { + buildInputs = [ + rust-bin.nightly.latest.default + postgresql + ]; + }; + bare = mkShell + { + buildInputs = [ + rust-bin.nightly.latest.default + ]; + }; + withLSP = mkShell + { + buildInputs = [ + rust-bin.nightly.latest.default + rust-analyzer + ]; + }; + }; + devShell = devShells.default; + defaultPackage = pkgs.rustPlatform.buildRustPackage rec { + pname = "singh3"; + version = "0.1.0"; + src = ./.; + nativeBuildInputs = with pkgs; [ + rust-bin.nightly.latest.default + ]; + cargoSha256 = "sha256-04yTexSkFpa3KQKVvfi7NM1j4V7m08kHDqw98bxXT5M="; + }; + } + ); } diff --git a/src/commands/count.rs b/src/commands/count.rs index 2318457..ad5dcca 100644 --- a/src/commands/count.rs +++ b/src/commands/count.rs @@ -1,5 +1,6 @@ use crate::lib::components::make_terminal_components; use core::time::Duration; +use regex::Regex; use serenity::{ builder::CreateEmbed, collector::component_interaction_collector::ComponentInteractionCollectorBuilder, @@ -79,15 +80,21 @@ pub async fn cadd(ctx: &Context, msg: &Message, args: Args) -> CommandResult { .await?; return Ok(()); } - if queries[1].contains(" ") { - msg.reply(ctx, "Not a valid regex").await?; + let r = Regex::new(&format!("(?i){}", queries[1])); + + if r.is_err() { + msg.reply(ctx, "Please enter a valid regex").await?; return Ok(()); } + + let reg = r.unwrap(); + let data_read = ctx.data.read().await; let db = data_read .get::() .expect("Expected Database in TypeMap.") .clone(); + let check_existense = db .query("SELECT name, reg FROM words WHERE name=$1", &[&queries[0]]) .await?; @@ -102,11 +109,7 @@ pub async fn cadd(ctx: &Context, msg: &Message, args: Args) -> CommandResult { } db.execute( "INSERT INTO words(name, reg, owner) VALUES($1, $2, $3)", - &[ - &queries[0], - &("(?i)".to_string() + queries[1]), - &msg.author.id.to_string(), - ], + &[&queries[0], ®.to_string(), &msg.author.id.to_string()], ) .await?; msg.reply(ctx, "Added").await?;