|
| 1 | +# Custom Functions in Casbin-RS |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Custom functions in Casbin-RS now support flexible argument types through Rhai's `Dynamic` type. This means you can create custom functions that work with: |
| 6 | + |
| 7 | +- **Strings** (as `ImmutableString`) |
| 8 | +- **Integers** (i32 or i64) |
| 9 | +- **Booleans** |
| 10 | +- **Floats** (f32 or f64) |
| 11 | +- **Arrays** |
| 12 | +- **Maps** |
| 13 | +- And more... |
| 14 | + |
| 15 | +This improvement addresses the limitation where custom functions previously only accepted `ImmutableString` arguments. |
| 16 | + |
| 17 | +## Basic Usage |
| 18 | + |
| 19 | +### Adding a Custom Function |
| 20 | + |
| 21 | +Custom functions are added using the `add_function` method on an `Enforcer` instance: |
| 22 | + |
| 23 | +```rust |
| 24 | +use casbin::prelude::*; |
| 25 | +use rhai::Dynamic; |
| 26 | + |
| 27 | +// Create your enforcer |
| 28 | +let mut e = Enforcer::new("model.conf", "policy.csv").await?; |
| 29 | + |
| 30 | +// Add a custom function |
| 31 | +e.add_function( |
| 32 | + "myFunction", |
| 33 | + OperatorFunction::Arg2(|arg1: Dynamic, arg2: Dynamic| { |
| 34 | + // Your custom logic here |
| 35 | + true.into() // Return a Dynamic value |
| 36 | + }), |
| 37 | +); |
| 38 | +``` |
| 39 | + |
| 40 | +## Examples |
| 41 | + |
| 42 | +### 1. String-based Custom Function |
| 43 | + |
| 44 | +For custom functions that work with strings, you can use the helper function `dynamic_to_str`: |
| 45 | + |
| 46 | +```rust |
| 47 | +use casbin::model::function_map::dynamic_to_str; |
| 48 | + |
| 49 | +e.add_function( |
| 50 | + "stringContains", |
| 51 | + OperatorFunction::Arg2(|haystack: Dynamic, needle: Dynamic| { |
| 52 | + let haystack_str = dynamic_to_str(&haystack); |
| 53 | + let needle_str = dynamic_to_str(&needle); |
| 54 | + haystack_str.contains(needle_str.as_ref()).into() |
| 55 | + }), |
| 56 | +); |
| 57 | +``` |
| 58 | + |
| 59 | +Or simply convert to String: |
| 60 | + |
| 61 | +```rust |
| 62 | +e.add_function( |
| 63 | + "stringMatch", |
| 64 | + OperatorFunction::Arg2(|s1: Dynamic, s2: Dynamic| { |
| 65 | + let str1 = s1.to_string(); |
| 66 | + let str2 = s2.to_string(); |
| 67 | + (str1 == str2).into() |
| 68 | + }), |
| 69 | +); |
| 70 | +``` |
| 71 | + |
| 72 | +### 2. Integer-based Custom Function |
| 73 | + |
| 74 | +```rust |
| 75 | +e.add_function( |
| 76 | + "greaterThan", |
| 77 | + OperatorFunction::Arg2(|a: Dynamic, b: Dynamic| { |
| 78 | + let a_int = a.as_int().unwrap_or(0); |
| 79 | + let b_int = b.as_int().unwrap_or(0); |
| 80 | + (a_int > b_int).into() |
| 81 | + }), |
| 82 | +); |
| 83 | +``` |
| 84 | + |
| 85 | +### 3. Boolean-based Custom Function |
| 86 | + |
| 87 | +```rust |
| 88 | +e.add_function( |
| 89 | + "customAnd", |
| 90 | + OperatorFunction::Arg2(|a: Dynamic, b: Dynamic| { |
| 91 | + let a_bool = a.as_bool().unwrap_or(false); |
| 92 | + let b_bool = b.as_bool().unwrap_or(false); |
| 93 | + (a_bool && b_bool).into() |
| 94 | + }), |
| 95 | +); |
| 96 | +``` |
| 97 | + |
| 98 | +### 4. Multi-argument Custom Function |
| 99 | + |
| 100 | +```rust |
| 101 | +e.add_function( |
| 102 | + "between", |
| 103 | + OperatorFunction::Arg3(|val: Dynamic, min: Dynamic, max: Dynamic| { |
| 104 | + let val_int = val.as_int().unwrap_or(0); |
| 105 | + let min_int = min.as_int().unwrap_or(0); |
| 106 | + let max_int = max.as_int().unwrap_or(0); |
| 107 | + (val_int >= min_int && val_int <= max_int).into() |
| 108 | + }), |
| 109 | +); |
| 110 | +``` |
| 111 | + |
| 112 | +### 5. Mixed-type Custom Function |
| 113 | + |
| 114 | +```rust |
| 115 | +e.add_function( |
| 116 | + "complexCheck", |
| 117 | + OperatorFunction::Arg3(|name: Dynamic, age: Dynamic, is_admin: Dynamic| { |
| 118 | + let name_str = name.to_string(); |
| 119 | + let age_int = age.as_int().unwrap_or(0); |
| 120 | + let admin_bool = is_admin.as_bool().unwrap_or(false); |
| 121 | + |
| 122 | + // Custom logic with different types |
| 123 | + let result = name_str.len() > 3 && age_int >= 18 && admin_bool; |
| 124 | + result.into() |
| 125 | + }), |
| 126 | +); |
| 127 | +``` |
| 128 | + |
| 129 | +## Using Custom Functions in Matchers |
| 130 | + |
| 131 | +Once registered, custom functions can be used in your policy matchers: |
| 132 | + |
| 133 | +```conf |
| 134 | +[matchers] |
| 135 | +m = greaterThan(r.age, 18) && stringContains(r.path, p.path) |
| 136 | +``` |
| 137 | + |
| 138 | +## OperatorFunction Variants |
| 139 | + |
| 140 | +The `OperatorFunction` enum supports functions with 0 to 6 arguments: |
| 141 | + |
| 142 | +- `Arg0`: `fn() -> Dynamic` |
| 143 | +- `Arg1`: `fn(Dynamic) -> Dynamic` |
| 144 | +- `Arg2`: `fn(Dynamic, Dynamic) -> Dynamic` |
| 145 | +- `Arg3`: `fn(Dynamic, Dynamic, Dynamic) -> Dynamic` |
| 146 | +- `Arg4`: `fn(Dynamic, Dynamic, Dynamic, Dynamic) -> Dynamic` |
| 147 | +- `Arg5`: `fn(Dynamic, Dynamic, Dynamic, Dynamic, Dynamic) -> Dynamic` |
| 148 | +- `Arg6`: `fn(Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic) -> Dynamic` |
| 149 | + |
| 150 | +## Working with Dynamic Types |
| 151 | + |
| 152 | +Rhai's `Dynamic` type provides several methods to extract values: |
| 153 | + |
| 154 | +- `as_int()` - Extract as integer (returns `Result<i64, &str>`) |
| 155 | +- `as_bool()` - Extract as boolean (returns `Result<bool, &str>`) |
| 156 | +- `as_float()` - Extract as float (returns `Result<f64, &str>`) |
| 157 | +- `is_string()` - Check if it's a string |
| 158 | +- `into_immutable_string()` - Convert to ImmutableString (consumes the Dynamic) |
| 159 | +- `to_string()` - Convert to String (works for any type) |
| 160 | + |
| 161 | +## Backward Compatibility |
| 162 | + |
| 163 | +All existing code continues to work. The change from `ImmutableString` to `Dynamic` is backward compatible because: |
| 164 | + |
| 165 | +1. Strings are automatically converted to `Dynamic` by Rhai |
| 166 | +2. The `dynamic_to_str` helper function makes string extraction easy |
| 167 | +3. All built-in functions have been updated and tested |
| 168 | + |
| 169 | +## Migration Guide |
| 170 | + |
| 171 | +If you have existing custom functions using `ImmutableString`, update them like this: |
| 172 | + |
| 173 | +**Before:** |
| 174 | +```rust |
| 175 | +e.add_function( |
| 176 | + "myFunc", |
| 177 | + OperatorFunction::Arg2( |
| 178 | + |s1: ImmutableString, s2: ImmutableString| { |
| 179 | + // logic here |
| 180 | + true.into() |
| 181 | + } |
| 182 | + ), |
| 183 | +); |
| 184 | +``` |
| 185 | + |
| 186 | +**After:** |
| 187 | +```rust |
| 188 | +e.add_function( |
| 189 | + "myFunc", |
| 190 | + OperatorFunction::Arg2(|s1: Dynamic, s2: Dynamic| { |
| 191 | + let str1 = s1.to_string(); |
| 192 | + let str2 = s2.to_string(); |
| 193 | + // logic here |
| 194 | + true.into() |
| 195 | + }), |
| 196 | +); |
| 197 | +``` |
| 198 | + |
| 199 | +## See Also |
| 200 | + |
| 201 | +- [Casbin Documentation](https://casbin.org/docs/function) |
| 202 | +- [Rhai Documentation](https://rhai.rs/book/) |
| 203 | +- Test: `test_custom_function_with_dynamic_types` in `src/enforcer.rs` |
0 commit comments