Skip to main content

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#[must_use = "store the configured builder or call build to use the policy"]
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct NormalizingJsonDecodePolicyBuilder {
29    /// Policy under construction.
30    policy: NormalizingJsonDecodePolicy,
31}
32
33impl NormalizingJsonDecodePolicyBuilder {
34    /// Creates a builder initialized with the lenient policy.
35    ///
36    /// # Returns
37    ///
38    /// A builder ready for selective policy customization.
39    #[inline]
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    pub const fn trim_whitespace(mut self, enabled: bool) -> Self {
57        self.policy.set_trim_whitespace(enabled);
58        self
59    }
60
61    /// Configures whether a leading UTF-8 byte order mark is removed.
62    ///
63    /// # Parameters
64    ///
65    /// * `enabled` - Whether to strip a leading UTF-8 BOM.
66    ///
67    /// # Returns
68    ///
69    /// This builder with the updated setting.
70    #[inline]
71    pub const fn strip_utf8_bom(mut self, enabled: bool) -> Self {
72        self.policy.set_strip_utf8_bom(enabled);
73        self
74    }
75
76    /// Configures how one outer Markdown code fence is handled.
77    ///
78    /// # Parameters
79    ///
80    /// * `policy` - Fence policy applied before JSON parsing.
81    ///
82    /// # Returns
83    ///
84    /// This builder with the updated setting.
85    #[inline]
86    pub const fn markdown_fence_policy(mut self, policy: MarkdownFencePolicy) -> Self {
87        self.policy.set_markdown_fence_policy(policy);
88        self
89    }
90
91    /// Configures whether raw control characters in strings are escaped.
92    ///
93    /// # Parameters
94    ///
95    /// * `enabled` - Whether raw string control characters are rewritten.
96    ///
97    /// # Returns
98    ///
99    /// This builder with the updated setting.
100    #[inline]
101    pub const fn escape_control_chars_in_strings(mut self, enabled: bool) -> Self {
102        self.policy.set_escape_control_chars_in_strings(enabled);
103        self
104    }
105
106    /// Configures the error diagnostic policy.
107    ///
108    /// # Parameters
109    ///
110    /// * `policy` - Diagnostic detail retention policy.
111    ///
112    /// # Returns
113    ///
114    /// This builder with the updated setting.
115    #[inline]
116    pub const fn diagnostic_policy(mut self, policy: DiagnosticPolicy) -> Self {
117        self.policy.set_diagnostic_policy(policy);
118        self
119    }
120
121    /// Builds the configured policy.
122    ///
123    /// # Returns
124    ///
125    /// An immutable normalization policy containing all settings selected on
126    /// this builder.
127    #[inline]
128    pub const fn build(self) -> NormalizingJsonDecodePolicy {
129        self.policy
130    }
131}
132
133impl Default for NormalizingJsonDecodePolicyBuilder {
134    /// Creates a builder initialized with the lenient policy.
135    #[inline]
136    fn default() -> Self {
137        Self::new()
138    }
139}