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