| Crates.io | actor_attribute_macro |
| lib.rs | actor_attribute_macro |
| version | 1.0.2 |
| created_at | 2025-10-16 22:07:45.838139+00 |
| updated_at | 2025-10-16 22:34:55.757063+00 |
| description | A procedural macro for generating JSON-based actor implementations for the simple_json_server library. |
| homepage | |
| repository | https://github.com/dcsturman/simple_json_server |
| max_upload_size | |
| id | 1886854 |
| size | 24,063 |
This crate provides the #[actor] procedural macro for the simple_json_server library. The macro automatically implements the Actor trait for structs by generating a JSON-based dispatch method.
The #[actor] macro:
impl blockdispatch method that:
{"method": "method_name", "params": {...}}use simple_json_server::{Actor, actor};
#[derive(Debug, Clone)]
struct Calculator {
memory: f64,
}
#[actor]
impl Calculator {
pub async fn add(&self, a: f64, b: f64) -> f64 {
a + b
}
pub async fn get_memory(&self) -> f64 {
self.memory
}
pub async fn info(&self) -> String {
"Calculator v1.0".to_string()
}
}
For the above example, the macro generates:
#[derive(serde::Deserialize)]
struct AddMessage {
a: f64,
b: f64,
}
#[derive(serde::Deserialize)]
struct GetMemoryMessage {}
#[derive(serde::Deserialize)]
struct InfoMessage {}
impl crate::Actor for Calculator {
fn dispatch(&self, msg: &str) -> String {
// JSON parsing and method dispatch logic
}
}
The macro expects JSON messages in this format:
{
"method": "method_name",
"params": {
"param1": "value1",
"param2": "value2"
}
}
For methods with no parameters, use an empty params object:
{
"method": "info",
"params": {}
}
The macro only processes methods that are:
pub)async fn)Private methods and synchronous methods are ignored.
All return values are automatically serialized to JSON. This includes:
42 → "42""hello" → "\"hello\""Result<T, E> → {"Ok": value} or {"Err": error}Serialize)The macro handles various error cases:
This macro requires:
syn - For parsing Rust syntaxquote - For generating Rust codeproc-macro2 - For procedural macro supportserde and serde_json - For JSON serialization/deserializationtokio - For async runtime support