Macro codize::cblock

source ·
macro_rules! cblock {
    ($start:expr, [] , $end:expr) => { ... };
    ($start:expr, [ $( $body:expr ),* $(,)? ] , $end:expr) => { ... };
    ($start:expr, $body:expr, $end:expr) => { ... };
}
Expand description

Macro for creating Blocks

§Examples

use codize::cblock;

let expected =
"fn main() {
    foo();
}";

let code = cblock!("fn main() {", [
   "foo();",
], "}");
assert_eq!(expected, code.to_string());

Anything that implements Into<Code> can be used in the body. It can also be anything that implements IntoIterator and returns Into<Code>.

You can call Block::connected to connect the start of the block with the end of the last block, such as an else block.

use codize::cblock;

let expected =
"fn foo(y: bool) {
    if (x()) {
        bar();
    } else if (y) {
        baz();
    }
}";

let func = "x";
let code = cblock!("fn foo(y: bool) {", [
   cblock!(format!("if ({func}()) {{"), [
      "bar();",
   ], "}"),
   cblock!("else if (y) {", [
      "baz();"
   ], "}").connected(),
], "}");
assert_eq!(expected, code.to_string());