Macro codize::block

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

Macro for creating a code block

§Examples

Single-line

use codize::{codeln, block};

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

let code = block!("\nfn main() {", [
   codeln!("foo();"),
], "}");
assert_eq!(expected, code.to_string());

Nested

use codize::{codeln, block, Codize};

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

let func = "x";
let code = block!("\nfn foo(y: bool) {", [
   block!(format!("if ({func}()) {{"), [
      codeln!("bar();"),
   ], "}"),
   block!(> "else if (y) {", [
      codeln!("baz();")
   ], "}"),
], "}");
assert_eq!(expected, code.to_string_with(&Codize::indent(2).set_trailing_newline(true)));