1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Defines the builder for [`NormalizingJsonDecodePolicy`].
use super::DiagnosticPolicy;
use super::MarkdownFencePolicy;
use super::NormalizingJsonDecodePolicy;
/// Builder for [`NormalizingJsonDecodePolicy`].
///
/// # Examples
///
/// ```
/// use qubit_json::decode::NormalizingJsonDecodePolicyBuilder;
///
/// let policy = NormalizingJsonDecodePolicyBuilder::new()
/// .strip_utf8_bom(false)
/// .build();
/// assert!(!policy.strip_utf8_bom());
/// ```
#[must_use = "store the configured builder or call build to use the policy"]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NormalizingJsonDecodePolicyBuilder {
/// Policy under construction.
policy: NormalizingJsonDecodePolicy,
}
impl NormalizingJsonDecodePolicyBuilder {
/// Creates a builder initialized with the lenient policy.
///
/// # Returns
///
/// A builder ready for selective policy customization.
#[inline]
pub const fn new() -> Self {
Self {
policy: NormalizingJsonDecodePolicy::lenient(),
}
}
/// Configures whether surrounding whitespace is removed.
///
/// # Parameters
///
/// * `enabled` - Whether to trim leading and trailing whitespace.
///
/// # Returns
///
/// This builder with the updated setting.
#[inline]
pub const fn trim_whitespace(mut self, enabled: bool) -> Self {
self.policy.set_trim_whitespace(enabled);
self
}
/// Configures whether a leading UTF-8 byte order mark is removed.
///
/// # Parameters
///
/// * `enabled` - Whether to strip a leading UTF-8 BOM.
///
/// # Returns
///
/// This builder with the updated setting.
#[inline]
pub const fn strip_utf8_bom(mut self, enabled: bool) -> Self {
self.policy.set_strip_utf8_bom(enabled);
self
}
/// Configures how one outer Markdown code fence is handled.
///
/// # Parameters
///
/// * `policy` - Fence policy applied before JSON parsing.
///
/// # Returns
///
/// This builder with the updated setting.
#[inline]
pub const fn markdown_fence_policy(mut self, policy: MarkdownFencePolicy) -> Self {
self.policy.set_markdown_fence_policy(policy);
self
}
/// Configures whether raw control characters in strings are escaped.
///
/// # Parameters
///
/// * `enabled` - Whether raw string control characters are rewritten.
///
/// # Returns
///
/// This builder with the updated setting.
#[inline]
pub const fn escape_control_chars_in_strings(mut self, enabled: bool) -> Self {
self.policy.set_escape_control_chars_in_strings(enabled);
self
}
/// Configures the error diagnostic policy.
///
/// # Parameters
///
/// * `policy` - Diagnostic detail retention policy.
///
/// # Returns
///
/// This builder with the updated setting.
#[inline]
pub const fn diagnostic_policy(mut self, policy: DiagnosticPolicy) -> Self {
self.policy.set_diagnostic_policy(policy);
self
}
/// Builds the configured policy.
///
/// # Returns
///
/// An immutable normalization policy containing all settings selected on
/// this builder.
#[inline]
pub const fn build(self) -> NormalizingJsonDecodePolicy {
self.policy
}
}
impl Default for NormalizingJsonDecodePolicyBuilder {
/// Creates a builder initialized with the lenient policy.
#[inline]
fn default() -> Self {
Self::new()
}
}