[][src]Macro pact_consumer::json_pattern

macro_rules! json_pattern {
    ($($json:tt)+) => { ... };
}

Construct a JsonPattern object using a convenient syntax.

// Place this declaration in your top-level `main.rs` or `lib.rs` file.
#[macro_use]
extern crate pact_consumer;

json_pattern!({
    "message": "Hello, world!",
    "location": { "x": 1, "y": 2 },
    "tags": ["interesting"]
});

The json_pattern! macro supports nested Rust expressions:

// Place these declarations in your top-level `main.rs` or `lib.rs` file.
#[macro_use]
extern crate pact_consumer;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate serde_json;

use pact_consumer::prelude::*;

#[derive(Serialize)]
struct Point {
   x: f32,
   y: f32,
}

json_pattern!({
    // You can use Rust expressions, as long as they support
    // `Into<JsonPattern>`.
    "message": format!("Hello, {}!", "world"),

    // You can also nest the `json!` macro to embed types which
    // support `Serialize`.
    "location": json!(Point { x: 1.0, y: 2.0 }),

    // You can use `something_like` to match by type only.
    "comment": like!("A comment goes here"),

    // You can use `array_like` to match an array of values which
    // look like the example.
    "tags": each_like!("tag"),
});