| Crates.io | hook |
| lib.rs | hook |
| version | 0.1.2 |
| created_at | 2025-02-09 06:10:02.55443+00 |
| updated_at | 2025-02-09 06:12:52.183784+00 |
| description | A filtering mechanism where functions (filters) can be registered, prioritized, and applied sequentially to values associated with named hooks. In Rust. |
| homepage | |
| repository | https://github.com/lotharthesavior/the-hook |
| max_upload_size | |
| id | 1548690 |
| size | 7,893 |
This Rust library provides a simple filtering mechanism where functions (filters) can be registered and applied to values. Filters are associated with hooks (unique names), allowing multiple transformations to be applied to values sequentially in order of priority.
To use this library, add it as a dependency in your Cargo project:
cargon add hook
OR
[dependencies]
hook = "0.1"
Filters are registered using add_filter. Each filter is associated with a hook (string identifier) and is executed based on its priority (lower values run earlier).
use rust_filters::{add_filter, apply_filters};
let hook = "modify_number";
add_filter(hook, 10, |v: i32| v + 5);
add_filter(hook, 20, |v: i32| v * 2);
Filters are applied using apply_filters. The value is transformed sequentially by all filters registered under the given hook.
let result = apply_filters("modify_number", 10);
assert_eq!(result, 30); // (10 + 5) * 2
A filter can be removed using its unique ID returned from add_filter.
let hook = "modify_number";
let filter_id = add_filter(hook, 10, |v: i32| v + 5);
remove_filter(hook, filter_id);
remove_all_filters("modify_number");
add_filter<T>(hook: &str, priority: i32, callback: impl Fn(T) -> T) -> u64Registers a filter callback for a specific hook.
hook: Name of the filter hook.priority: Determines the execution order (lower runs first).callback: Transformation function.apply_filters<T>(hook: &str, value: T) -> TApplies all filters registered under the given hook to the provided value.
hook: Name of the filter hook.value: Initial value before transformations.remove_filter(hook: &str, id: u64) -> boolRemoves a specific filter by ID.
hook: Name of the filter hook.id: Filter ID returned by add_filter.true if the filter was removed, false otherwise.remove_all_filters(hook: &str)Removes all filters for the specified hook.
hook: Name of the filter hook.Run tests using:
cargo test
This library is released under the MIT License.