Skip to content

Commit

Permalink
feat(filters): add option to set command description
Browse files Browse the repository at this point in the history
Added the ability to set a description for commands.
  • Loading branch information
AndrielFR committed Jan 5, 2025
1 parent 5fd31e2 commit efbfea3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/ferogram-lua/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ crate-type = ["cdylib"]
[dependencies]
ferogram = { path = "../ferogram", features = ["lua"] }

mlua = { version = "*", features = ["async", "lua54", "module"] }
mlua = { version = "^0.10", features = ["async", "lua54", "module"] }

[target.x86_64-apple-darwin]
rustflags = [
Expand Down
2 changes: 1 addition & 1 deletion lib/ferogram-python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ crate-type = ["cdylib"]
[dependencies]
ferogram = { path = "../ferogram", features = ["python"] }

pyo3 = { version = "*", features = ["extension-module", "macros"] }
pyo3 = { version = "^0.23", features = ["extension-module", "macros"] }
22 changes: 11 additions & 11 deletions lib/ferogram/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ ferogram-macros = { path = "../ferogram-macros", optional = true }
grammers-client = { git = "https://github.com/Lonami/grammers.git" }
grammers-mtsender = { git = "https://github.com/Lonami/grammers.git" }

log = "*"
mlua = { version = "*", features = ["async", "lua54", "module"], optional = true }
pyo3 = { version = "*", features = ["experimental-async", "macros"], optional = true }
regex = "1"
tokio = { version = "1", features = ["fs", "rt", "signal", "sync"] }
rpassword = "7"
async-trait = "*"
futures-util = { version = "*", default-features = false, features = ["alloc"] }
async-recursion = "1"
pyo3-async-runtimes = { version = "*", features = ["tokio-runtime"], optional = true }
log = "0.4.22"
mlua = { version = "^0.10", features = ["async", "lua54", "module"], optional = true }
pyo3 = { version = "^0.23", features = ["experimental-async", "macros"], optional = true }
regex = "1.11.1"
tokio = { version = "^1.42", features = ["fs", "rt", "signal", "sync"] }
rpassword = "7.3.1"
async-trait = "^0.1"
futures-util = { version = "^0.3", default-features = false, features = ["alloc"] }
async-recursion = "^1.1"
pyo3-async-runtimes = { version = "^0.23", features = ["tokio-runtime"], optional = true }

[dev-dependencies]
tokio = { version = "1", features = ["macros"] }
tokio = { version = "^1.42", features = ["macros"] }
21 changes: 21 additions & 0 deletions lib/ferogram/src/filters/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,31 @@ use crate::{Filter, Flow};
pub struct Command {
pub(crate) prefixes: Vec<String>,
pub(crate) command: String,
pub(crate) description: String,

pub(crate) username: Arc<Mutex<Option<String>>>,
}

impl Command {
/// Sets the description for the command.
///
/// # Arguments
///
/// * `description` - A string slice that holds the description of the command.
///
/// # Example
///
/// ```rust
/// use ferogram::filter::command;
///
/// let mut command = command("hello").description("Say hello to the user.");
/// ```
pub fn description(mut self, description: &str) -> Self {
self.description = description.to_string();
self
}
}

#[async_trait]
impl Filter for Command {
async fn check(&mut self, client: Client, update: Update) -> Flow {
Expand Down
14 changes: 10 additions & 4 deletions lib/ferogram/src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ pub fn regex(pat: &'static str) -> impl Filter {
/// Pass if the message matches the specified command.
///
/// This filter is a custom [`regex`] filter, so it accepts regex syntax.
pub fn command(pat: &'static str) -> impl Filter {
pub fn command(pat: &'static str) -> Command {
Command {
prefixes: DEFAULT_PREFIXES.into_iter().map(regex::escape).collect(),
command: pat.to_owned(),
description: String::new(),

username: Arc::new(Mutex::new(None)),
}
Expand All @@ -132,31 +133,36 @@ pub fn command(pat: &'static str) -> impl Filter {
/// Pass if the message matches the specified command with custom prefixes.
///
/// This filter is a custom [`regex`] filter, so it accepts a bit of regex syntax.
pub fn command_with(pres: &'static [&'static str], pat: &'static str) -> impl Filter {
pub fn command_with(pres: &'static [&'static str], pat: &'static str) -> Command {
Command {
prefixes: pres.iter().map(|pre| regex::escape(pre)).collect(),
command: pat.to_owned(),
description: String::new(),

username: Arc::new(Mutex::new(None)),
}
}

/// Pass if the message matches any of the specified commands.
pub fn commands(pats: &'static [&'static str]) -> impl Filter {
pub fn commands(pats: &'static [&'static str]) -> Command {
Command {
prefixes: DEFAULT_PREFIXES.into_iter().map(regex::escape).collect(),
command: pats.join("|"),
description: String::new(),

username: Arc::new(Mutex::new(None)),
}
}

/// Pass if the message matches any of the specified commands with custom prefixes.
///
/// This filter is a custom [`regex`] filter, so it accepts a bit of regex syntax.
pub fn commands_with(pres: &'static [&'static str], pats: &'static [&'static str]) -> impl Filter {
pub fn commands_with(pres: &'static [&'static str], pats: &'static [&'static str]) -> Command {
Command {
prefixes: pres.iter().map(|pre| regex::escape(pre)).collect(),
command: pats.join("|"),
description: String::new(),

username: Arc::new(Mutex::new(None)),
}
}
Expand Down

0 comments on commit efbfea3

Please sign in to comment.