13.json (9412B)
1 { 2 "number": 13, 3 "title": "Add a way to add custom state and update sdk", 4 "state": "closed", 5 "diff_file": "13.diff", 6 "author": "MTRNord", 7 "created_at": "2022-03-12T18:54:50Z", 8 "closed_at": "2025-06-16T22:41:06Z", 9 "merged_at": null, 10 "base_ref": "main", 11 "head_ref": "MTRNord/issue-12", 12 "labels": [ 13 "enhancement" 14 ], 15 "assignees": [ 16 "MTRNord" 17 ], 18 "requested_reviewers": [], 19 "body": "This is heavily building upon what http and axum do. In fact there is a lot of reused code.\r\n\r\nThis allows for function defined like this:\r\n\r\n```rust\r\n#[command(help = \"`!hello_world` - Prints \\\"hello world\\\".\")]\r\npub async fn hello_world<'a>(\r\n Extension(tx): Extension<mrsbfh::Sender>,\r\n) -> Result<(), Error> {\r\n let content = RoomMessageEventContent::notice_plain(\"Hello World!\");\r\n\r\n tx.lock().await.send(content).await?;\r\n Ok(())\r\n}\r\n```\r\n\r\nDownsides: \r\n\r\n- It adds a Mutex to mutable stuff.\r\n- It needs stuff to be strictly async (I guess that was needed before too?)\r\n- A little more verbose?\r\n\r\nPros:\r\n\r\n- You can pass anything to it as long as it is defined in your sync handler. (the macro auto extracts the arguments from it)\r\n- You don't need to pass all things to every command\r\n\r\nFixes #12 \r\n\r\nMissing is:\r\n\r\n- [x] Getting user supplied things from the sync handler and passing it on.\r\n\r\nTests seem to work:\r\n\r\n\r\n", 20 "comments": [ 21 { 22 "author": "MTRNord", 23 "created_at": "2022-03-12T19:54:27Z", 24 "body": "Followup issue: #14 " 25 }, 26 { 27 "author": "MTRNord", 28 "created_at": "2022-03-13T20:13:23Z", 29 "body": "Some feedback from jplatte:\r\n\r\n> One thing I noticed is that you seem to have an automatic 'extension' of Arc<Mutex<mrsbfh::Sender>>. I would recomment instead making the Arc<Mutex<_>> part internal so that the sender is Clone + Send + Sync and you make it an extractor by itself (no Extension wrapping needed)\r\n\r\n> Extension can still make sense if you want users to be able to add their own context\r\n\r\n> In the SDK the Ctx type and register_event_handler_context fills the same role" 30 }, 31 { 32 "author": "MTRNord", 33 "created_at": "2024-05-24T23:10:22Z", 34 "body": "Main missing TODO is now fixing the comments I think" 35 } 36 ], 37 "review_comments": [ 38 { 39 "author": "MTRNord", 40 "created_at": "2022-03-12T18:57:36Z", 41 "body": "@donicrosby This is basically where now your wish comes in. I basically need to do this dynamically for all the things that the function provides when you use the macro. This is currently still hardcoded to only do the config. But the example bot should give you an idea how the api will look like. Which means it should be fairly similiar as before with not too many changes needed", 42 "path": "mrsbfh-macros/src/lib.rs", 43 "line": 275, 44 "diff_hunk": "@@ -267,15 +266,18 @@ pub fn commands(_: TokenStream, input: TokenStream) -> TokenStream {\n tracing::info!(\"Got command: {}\", command);\n }\n // Make sure this is immutable\n- let args: Vec<&str> = split.collect();\n+ let args_raw: Vec<String> = split.collect();\n+ let args: std::sync::Arc<Vec<String>> = std::sync::Arc::new(args_raw.clone());\n+ let tx = std::sync::Arc::new(std::sync::Mutex::new(tx));\n+\n+ let mut msg = mrsbfh::commands::Message::new();\n+ // TODO insert all the things in the function args\n+ msg.extensions_mut().insert(std::sync::Arc::clone(&args));" 45 }, 46 { 47 "author": "MTRNord", 48 "created_at": "2022-03-12T19:28:20Z", 49 "body": "Pushed the rest now. So in theory this PR should work. It compiles but I didnt actually test if it works in practice", 50 "path": "mrsbfh-macros/src/lib.rs", 51 "line": 275, 52 "diff_hunk": "@@ -267,15 +266,18 @@ pub fn commands(_: TokenStream, input: TokenStream) -> TokenStream {\n tracing::info!(\"Got command: {}\", command);\n }\n // Make sure this is immutable\n- let args: Vec<&str> = split.collect();\n+ let args_raw: Vec<String> = split.collect();\n+ let args: std::sync::Arc<Vec<String>> = std::sync::Arc::new(args_raw.clone());\n+ let tx = std::sync::Arc::new(std::sync::Mutex::new(tx));\n+\n+ let mut msg = mrsbfh::commands::Message::new();\n+ // TODO insert all the things in the function args\n+ msg.extensions_mut().insert(std::sync::Arc::clone(&args));" 53 }, 54 { 55 "author": "MTRNord", 56 "created_at": "2022-03-12T19:46:34Z", 57 "body": "One issue is that mutex stuff needs to already be `Arc<Mutex<>>` to work. It should however then clone the arc as required", 58 "path": "mrsbfh-macros/src/lib.rs", 59 "line": 275, 60 "diff_hunk": "@@ -267,15 +266,18 @@ pub fn commands(_: TokenStream, input: TokenStream) -> TokenStream {\n tracing::info!(\"Got command: {}\", command);\n }\n // Make sure this is immutable\n- let args: Vec<&str> = split.collect();\n+ let args_raw: Vec<String> = split.collect();\n+ let args: std::sync::Arc<Vec<String>> = std::sync::Arc::new(args_raw.clone());\n+ let tx = std::sync::Arc::new(std::sync::Mutex::new(tx));\n+\n+ let mut msg = mrsbfh::commands::Message::new();\n+ // TODO insert all the things in the function args\n+ msg.extensions_mut().insert(std::sync::Arc::clone(&args));" 61 }, 62 { 63 "author": "donicrosby", 64 "created_at": "2022-03-12T20:24:01Z", 65 "body": "That shouldn't be too difficult, I think that's fairly standard for something like this.\r\n\r\nThanks for the quick fix! I'll test it out later tonight! ", 66 "path": "mrsbfh-macros/src/lib.rs", 67 "line": 275, 68 "diff_hunk": "@@ -267,15 +266,18 @@ pub fn commands(_: TokenStream, input: TokenStream) -> TokenStream {\n tracing::info!(\"Got command: {}\", command);\n }\n // Make sure this is immutable\n- let args: Vec<&str> = split.collect();\n+ let args_raw: Vec<String> = split.collect();\n+ let args: std::sync::Arc<Vec<String>> = std::sync::Arc::new(args_raw.clone());\n+ let tx = std::sync::Arc::new(std::sync::Mutex::new(tx));\n+\n+ let mut msg = mrsbfh::commands::Message::new();\n+ // TODO insert all the things in the function args\n+ msg.extensions_mut().insert(std::sync::Arc::clone(&args));" 69 }, 70 { 71 "author": "MTRNord", 72 "created_at": "2022-03-12T20:25:00Z", 73 "body": "Sure no problem :)", 74 "path": "mrsbfh-macros/src/lib.rs", 75 "line": 275, 76 "diff_hunk": "@@ -267,15 +266,18 @@ pub fn commands(_: TokenStream, input: TokenStream) -> TokenStream {\n tracing::info!(\"Got command: {}\", command);\n }\n // Make sure this is immutable\n- let args: Vec<&str> = split.collect();\n+ let args_raw: Vec<String> = split.collect();\n+ let args: std::sync::Arc<Vec<String>> = std::sync::Arc::new(args_raw.clone());\n+ let tx = std::sync::Arc::new(std::sync::Mutex::new(tx));\n+\n+ let mut msg = mrsbfh::commands::Message::new();\n+ // TODO insert all the things in the function args\n+ msg.extensions_mut().insert(std::sync::Arc::clone(&args));" 77 }, 78 { 79 "author": "MTRNord", 80 "created_at": "2022-03-12T20:25:34Z", 81 "body": "If you have any bugs feel free to mention them in this PR :) I only ran the example bot as I have no other to test with currently. So there may be stuff i missed", 82 "path": "mrsbfh-macros/src/lib.rs", 83 "line": 275, 84 "diff_hunk": "@@ -267,15 +266,18 @@ pub fn commands(_: TokenStream, input: TokenStream) -> TokenStream {\n tracing::info!(\"Got command: {}\", command);\n }\n // Make sure this is immutable\n- let args: Vec<&str> = split.collect();\n+ let args_raw: Vec<String> = split.collect();\n+ let args: std::sync::Arc<Vec<String>> = std::sync::Arc::new(args_raw.clone());\n+ let tx = std::sync::Arc::new(std::sync::Mutex::new(tx));\n+\n+ let mut msg = mrsbfh::commands::Message::new();\n+ // TODO insert all the things in the function args\n+ msg.extensions_mut().insert(std::sync::Arc::clone(&args));" 85 } 86 ] 87 }