Skip to main content

qubit_json/format/
json_pretty_options.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Options for whitespace-only JSON token formatting.
9
10use super::JsonPrettyOptionsBuilder;
11
12/// Controls indentation and the optional final newline in pretty output.
13///
14/// The default is two spaces per level, no displayed-depth cap, and no final
15/// newline. A displayed-depth cap changes indentation only; it does not limit
16/// the actual nesting depth accepted by the formatter.
17///
18/// # Examples
19///
20/// ```
21/// use qubit_json::format::{format_json_tokens, JsonPrettyOptions};
22///
23/// let options = JsonPrettyOptions::builder()
24///     .indent_width(2)
25///     .trailing_newline(true)
26///     .build()?;
27/// let output = format_json_tokens(b"[1]", &options, None)?;
28/// assert_eq!(output, b"[\n  1\n]\n");
29/// # Ok::<(), Box<dyn std::error::Error>>(())
30/// ```
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub struct JsonPrettyOptions {
33    /// Spaces used per displayed indentation level.
34    pub(super) indent_width: usize,
35    /// Optional cap on displayed indentation depth.
36    pub(super) max_indent_levels: Option<usize>,
37    /// Whether to append one newline after the formatted value.
38    pub(super) trailing_newline: bool,
39}
40
41impl JsonPrettyOptions {
42    /// Creates a builder using two spaces, unlimited displayed depth, and no
43    /// final newline.
44    ///
45    /// # Returns
46    ///
47    /// A builder initialized with the documented formatting defaults.
48    pub fn builder() -> JsonPrettyOptionsBuilder {
49        JsonPrettyOptionsBuilder::default()
50    }
51}