diff --git a/README.md b/README.md index 91e2a79..564e63e 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ 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: @@ -134,12 +134,12 @@ 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| { diff --git a/docs/llm/plans/2026/05/21-add-session-sub-to-user-id-and-session-user-to-sub-fns.md b/docs/llm/plans/2026/05/21-add-session-sub-to-user-id-and-session-user-to-sub-fns.md index 27cbe39..73d8147 100644 --- a/docs/llm/plans/2026/05/21-add-session-sub-to-user-id-and-session-user-to-sub-fns.md +++ b/docs/llm/plans/2026/05/21-add-session-sub-to-user-id-and-session-user-to-sub-fns.md @@ -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. @@ -20,7 +20,7 @@ pub struct DpsConfig { // ... existing fields ... // Session conversion functions - session_sub_to_user_id_fn: Box u64 + Send + Sync>, + session_sub_to_user_id_fn: Box i64 + Send + Sync>, session_user_to_sub_fn: Box String + Send + Sync>, } ``` @@ -34,7 +34,7 @@ Self { // ... existing initializations ... session_sub_to_user_id_fn: Box::new(|sub: &str| { - sub.parse::().unwrap_or(0) + sub.parse::().unwrap_or(0) }), session_user_to_sub_fn: Box::new(|record: &serde_json::Value| { record @@ -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); } @@ -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); } @@ -133,7 +133,7 @@ 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: @@ -141,12 +141,12 @@ 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| { diff --git a/src/lib.rs b/src/lib.rs index fbd45e6..95bfe01 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,7 +60,7 @@ pub struct DpsConfig { auth_api_session_ttl_seconds: Option, // Session conversion functions - session_sub_to_user_id_fn: Box u64 + Send + Sync>, + session_sub_to_user_id_fn: Box i64 + Send + Sync>, session_user_to_sub_fn: Box String + Send + Sync>, } @@ -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::().unwrap_or(0)), + session_sub_to_user_id_fn: Box::new(|sub: &str| sub.parse::().unwrap_or(0)), session_user_to_sub_fn: Box::new(|record: &serde_json::Value| { record .get("id") @@ -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); } @@ -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); }