Macro publish

Source
macro_rules! publish {
    ($topic:expr) => { ... };
    ($topic:expr, $options:expr) => { ... };
    ($topic:expr, args: $args:expr) => { ... };
    ($topic:expr, kwargs: $kwargs:expr) => { ... };
    ($topic:expr, args: $args:expr, kwargs: $kwargs:expr) => { ... };
    ($topic:expr, $options:expr, args: $args:expr) => { ... };
    ($topic:expr, $options:expr, kwargs: $kwargs:expr) => { ... };
    ($topic:expr, $options:expr, $args:expr, $kwargs:expr) => { ... };
}
Expand description

§Publish Macro - wamp-proto

Publish message builder with thread safe auto-incrementing request-ids.

§Examples

use wamp_core::publish;
use wamp_core::messages::Publish;
use serde_json::{json, Value};

// Create a Publish message with default values
let publish = publish!("topic");

// Which is the same as creating this:
let publish2 = Publish {
    topic: "topic".to_string(),
    request_id: 1,
    options: json!({}),
    args: Value::Null,
    kwargs: Value::Null
};

assert_eq!(publish, publish2);

// Some other ways you can construct it using the macro

// Create a publish with custom options but empty args and kwargs
let _ = publish!("topic", json!( { "key": "value" } ));

// Create a publish with custom args or kwargs, but empty options
let _ = publish!("topic", args: json!( [ 1, 2, 3 ] ));
let _ = publish!("topic", kwargs: json!( { "key": "value" } ));

// Create a publish with custom args and kwargs, but empty options
let _ = publish!("topic", args: json!([ 1, 2, 3 ]), kwargs: json!({ "key": "value" }));

// Create a publish with custom options, and either custom args OR custom kwargs
let _ = publish!("topic", json!( { "key": "value" } ), args: json!( [ 1, 2, 3 ] ));
let _ = publish!("topic", json!( { "key": "value" } ), kwargs: json!( { "key": "value" } ));

// Create a publish with custom options, and both custom args and kwargs
// Note that when you use all "required" arguments for the struuct, keyword arguments should not be used for args and kwargs
let _ = publish!("topic", json!({}), json!([]), json!({}));