Skip to main content

braces

Function braces 

Source
pub fn braces(layout: Box<Layout>) -> Box<Layout>
Expand description

Wraps a layout in curly braces.

This convenience function surrounds the given layout with curly braces, using unpadded compositions to ensure no extra spaces are added. This is commonly used for code blocks, object literals, set notation, scope delimiters, and other brace-delimited constructs.

§Parameters

  • layout - The layout to wrap in curly braces

§Returns

A layout wrapped in curly braces: {content}

§Examples

use typeset::*;

// Simple code block
let block = braces(
    nest(join_with_lines(vec![
        text_str("let x = 1;"),
        text_str("println!(\"{}\", x);")
    ]))
);

let result = format_layout(block, 2, 40);
// Output:
// {
//   let x = 1;
//   println!("{}", x);
// }
use typeset::*;

// JSON-like object
let object = braces(
    nest(join_with_commas(vec![
        comp(text_str("\"name\""), text_str(": \"John\""), true, false),
        comp(text_str("\"age\""), text_str(": 30"), true, false)
    ]))
);

// When it fits: '{"name": "John", "age": 30}'
// When it breaks:
// {
//   "name": "John",
//   "age": 30
// }
use typeset::*;

// Function definition
let function = comp(
    text_str("fn main()"),
    braces(
        nest(text_str("println!(\"Hello, world!\");"))
    ),
    true, false
);

// Output:
// fn main() {
//   println!("Hello, world!");
// }

§Behavior

  • Uses unpadded compositions (pad=false) to avoid spaces around braces
  • The wrapped content can break internally based on its own structure
  • Braces themselves never break apart from their content
  • Commonly combined with [nest] for proper indentation of block content
  • No additional spacing is introduced

§See Also

  • parens - For wrapping in parentheses ()
  • brackets - For wrapping in square brackets []
  • [nest] - Often used inside braces for proper indentation
  • [join_with_lines] - Common for multi-statement blocks