Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,20 @@ These properties are only available in the Rust implementation. The Bun/TypeScri

| Property | Default | Description |
|----------|---------|-------------|
| `session_sub_to_user_id_fn` | Parses string as u64, returns 0 on failure | Function that converts a session `sub` string to a `u64` user ID |
| `session_sub_to_user_id_fn` | Parses string as i64, returns 0 on failure | Function that converts a session `sub` string to an `i64` user ID |
| `session_user_to_sub_fn` | Returns `id` property as string | Function that extracts a `sub` string from a JSON record (`serde_json::Value`) |

Example usage:

```rust
let mut config = DpsConfig::new();

// Use default: parses sub as u64
// Use default: parses sub as i64
let to_user_id = config.get_session_sub_to_user_id_fn();
assert_eq!(to_user_id("42"), 42);

// Custom converter: use length of sub as user ID
config.set_session_sub_to_user_id_fn(|sub| sub.len() as u64);
config.set_session_sub_to_user_id_fn(|sub| sub.len() as i64);

// Custom sub extractor from JSON record
config.set_session_user_to_sub_fn(|record| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Add two new function-typed properties to the Rust `DpsConfig` struct:

1. **`session_sub_to_user_id_fn`** — A function that takes a `&str` (session sub) and returns `u64` (user ID). Default: identity function that parses the string as `u64`.
1. **`session_sub_to_user_id_fn`** — A function that takes a `&str` (session sub) and returns `i64` (user ID). Default: identity function that parses the string as `i64`.
2. **`session_user_to_sub_fn`** — A function that takes a JSON-like record (`serde_json::Value`) and returns a `String` (the sub). Default: returns the `id` property of the record as a string.

These are Rust-only features. The Bun/TypeScript implementation remains unchanged.
Expand All @@ -20,7 +20,7 @@ pub struct DpsConfig {
// ... existing fields ...

// Session conversion functions
session_sub_to_user_id_fn: Box<dyn Fn(&str) -> u64 + Send + Sync>,
session_sub_to_user_id_fn: Box<dyn Fn(&str) -> i64 + Send + Sync>,
session_user_to_sub_fn: Box<dyn Fn(&serde_json::Value) -> String + Send + Sync>,
}
```
Expand All @@ -34,7 +34,7 @@ Self {
// ... existing initializations ...

session_sub_to_user_id_fn: Box::new(|sub: &str| {
sub.parse::<u64>().unwrap_or(0)
sub.parse::<i64>().unwrap_or(0)
}),
session_user_to_sub_fn: Box::new(|record: &serde_json::Value| {
record
Expand All @@ -55,11 +55,11 @@ Self {

```rust
// session_sub_to_user_id_fn
pub fn get_session_sub_to_user_id_fn(&self) -> &dyn Fn(&str) -> u64 {
pub fn get_session_sub_to_user_id_fn(&self) -> &dyn Fn(&str) -> i64 {
self.session_sub_to_user_id_fn.as_ref()
}

pub fn set_session_sub_to_user_id_fn(&mut self, f: impl Fn(&str) -> u64 + Send + Sync + 'static) {
pub fn set_session_sub_to_user_id_fn(&mut self, f: impl Fn(&str) -> i64 + Send + Sync + 'static) {
self.session_sub_to_user_id_fn = Box::new(f);
}

Expand Down Expand Up @@ -98,7 +98,7 @@ fn test_session_sub_to_user_id_fn_default() {
fn test_session_sub_to_user_id_fn_custom() {
let mut config = DpsConfig::new();
config.set_session_sub_to_user_id_fn(|sub| {
sub.len() as u64
sub.len() as i64
});
assert_eq!(config.get_session_sub_to_user_id_fn()("hello"), 5);
}
Expand Down Expand Up @@ -133,20 +133,20 @@ These properties are only available in the Rust implementation. The Bun/TypeScri

| Property | Default | Description |
|----------|---------|-------------|
| `session_sub_to_user_id_fn` | Parses string as u64, returns 0 on failure | Function that converts a session `sub` string to a `u64` user ID |
| `session_sub_to_user_id_fn` | Parses string as i64, returns 0 on failure | Function that converts a session `sub` string to an `i64` user ID |
| `session_user_to_sub_fn` | Returns `id` property as string | Function that extracts a `sub` string from a JSON record (`serde_json::Value`) |

Example usage:

```rust
let mut config = DpsConfig::new();

// Use default: parses sub as u64
// Use default: parses sub as i64
let to_user_id = config.get_session_sub_to_user_id_fn();
assert_eq!(to_user_id("42"), 42);

// Custom converter: use length of sub as user ID
config.set_session_sub_to_user_id_fn(|sub| sub.len() as u64);
config.set_session_sub_to_user_id_fn(|sub| sub.len() as i64);

// Custom sub extractor from JSON record
config.set_session_user_to_sub_fn(|record| {
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct DpsConfig {
auth_api_session_ttl_seconds: Option<u32>,

// Session conversion functions
session_sub_to_user_id_fn: Box<dyn Fn(&str) -> u64 + Send + Sync>,
session_sub_to_user_id_fn: Box<dyn Fn(&str) -> i64 + Send + Sync>,
session_user_to_sub_fn: Box<dyn Fn(&serde_json::Value) -> String + Send + Sync>,
}

Expand Down Expand Up @@ -106,7 +106,7 @@ impl DpsConfig {
auth_api_sqlite_session_pool_size: load_env_u16("DPS_AUTH_API_SQLITE_SESSION_POOL_SIZE"),
auth_api_session_secret: load_env_string("DPS_AUTH_API_SESSION_SECRET"),
auth_api_session_ttl_seconds: load_env_u32("DPS_AUTH_API_SESSION_TTL_SECONDS"),
session_sub_to_user_id_fn: Box::new(|sub: &str| sub.parse::<u64>().unwrap_or(0)),
session_sub_to_user_id_fn: Box::new(|sub: &str| sub.parse::<i64>().unwrap_or(0)),
session_user_to_sub_fn: Box::new(|record: &serde_json::Value| {
record
.get("id")
Expand Down Expand Up @@ -369,15 +369,15 @@ impl DpsConfig {
// Session conversion functions
// --------------------

/// Returns the function that converts a session `sub` string to a `u64` user ID.
/// Returns the function that converts a session `sub` string to an `i64` user ID.
///
/// Default: Parses the string as `u64`, returning `0` on failure.
pub fn get_session_sub_to_user_id_fn(&self) -> &dyn Fn(&str) -> u64 {
/// Default: Parses the string as `i64`, returning `0` on failure.
pub fn get_session_sub_to_user_id_fn(&self) -> &dyn Fn(&str) -> i64 {
self.session_sub_to_user_id_fn.as_ref()
}

/// Set the session sub to user ID conversion function.
pub fn set_session_sub_to_user_id_fn(&mut self, f: impl Fn(&str) -> u64 + Send + Sync + 'static) {
pub fn set_session_sub_to_user_id_fn(&mut self, f: impl Fn(&str) -> i64 + Send + Sync + 'static) {
self.session_sub_to_user_id_fn = Box::new(f);
}

Expand Down Expand Up @@ -761,7 +761,7 @@ mod tests {
#[test]
fn test_session_sub_to_user_id_fn_custom() {
let mut config = DpsConfig::new();
config.set_session_sub_to_user_id_fn(|sub| sub.len() as u64);
config.set_session_sub_to_user_id_fn(|sub| sub.len() as i64);
assert_eq!(config.get_session_sub_to_user_id_fn()("hello"), 5);
}

Expand Down
Loading