Skip to main content

qubit_budget/structure/
structure_limits_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//! Builds optional structural input limits.
9
10use super::StructureLimits;
11use super::StructureResource;
12use crate::resource::ResourceLimit;
13use crate::resource::ResourceQuantity;
14
15/// Builder for [`StructureLimits`].
16///
17/// # Type Parameters
18///
19/// * `R` - Caller-defined resource identity retained by limits and errors.
20/// * `Q` - Exact unsigned quantity used for measurements and accounting.
21///
22/// # Examples
23///
24/// ```
25/// use qubit_budget::StructureLimitsBuilder;
26///
27/// let limits = StructureLimitsBuilder::new().max_depth(4).max_nodes(16).build();
28/// assert_eq!(limits.max_depth(), Some(4));
29/// assert_eq!(limits.max_nodes(), Some(16));
30/// ```
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
32pub struct StructureLimitsBuilder<R = StructureResource, Q = usize>
33where
34    Q: ResourceQuantity,
35{
36    /// Limit configuration accumulated by chained builder calls.
37    limits: StructureLimits<R, Q>,
38}
39
40impl<R, Q> Default for StructureLimitsBuilder<R, Q>
41where
42    Q: ResourceQuantity,
43{
44    /// Creates an empty builder through the standard [`Default`] interface.
45    ///
46    /// # Returns
47    ///
48    /// Creates an empty builder through the standard [`Default`] interface.
49    fn default() -> Self {
50        Self::new()
51    }
52}
53
54impl<R, Q> From<StructureLimitsBuilder<R, Q>> for StructureLimits<R, Q>
55where
56    Q: ResourceQuantity,
57{
58    /// Finishes the builder and returns its structural limit configuration.
59    ///
60    /// # Parameters
61    ///
62    /// * `builder` - Builder whose accumulated configuration is consumed.
63    ///
64    /// # Returns
65    ///
66    /// Finishes the builder and returns its structural limit configuration.
67    fn from(builder: StructureLimitsBuilder<R, Q>) -> Self {
68        builder.build()
69    }
70}
71
72impl<R, Q> StructureLimitsBuilder<R, Q>
73where
74    Q: ResourceQuantity,
75{
76    /// Creates an empty structural-limits builder.
77    ///
78    /// # Returns
79    ///
80    /// Creates an empty structural-limits builder.
81    #[inline]
82    #[must_use]
83    pub const fn new() -> Self {
84        Self {
85            limits: StructureLimits::new(),
86        }
87    }
88
89    /// Creates a builder retaining an existing limit configuration.
90    ///
91    /// # Parameters
92    ///
93    /// * `limits` - Existing structural limits whose configuration is copied
94    ///   into this builder.
95    ///
96    /// # Returns
97    ///
98    /// Creates a builder retaining an existing limit configuration.
99    #[inline]
100    #[must_use]
101    pub(crate) const fn from_limits(limits: StructureLimits<R, Q>) -> Self {
102        Self { limits }
103    }
104
105    /// Sets the depth limit.
106    ///
107    /// # Parameters
108    ///
109    /// * `limit` - Resource-bound nesting-depth limit to install.
110    ///
111    /// # Returns
112    ///
113    /// The builder with the described setting applied.
114    #[inline]
115    #[must_use]
116    pub fn depth_limit(mut self, limit: ResourceLimit<R, Q>) -> Self {
117        self.limits.set_depth_limit(limit);
118        self
119    }
120
121    /// Sets the node limit.
122    ///
123    /// # Parameters
124    ///
125    /// * `limit` - Resource-bound cumulative node limit to install.
126    ///
127    /// # Returns
128    ///
129    /// The builder with the described setting applied.
130    #[inline]
131    #[must_use]
132    pub fn nodes_limit(mut self, limit: ResourceLimit<R, Q>) -> Self {
133        self.limits.set_nodes_limit(limit);
134        self
135    }
136
137    /// Sets the sequence-item limit.
138    ///
139    /// # Parameters
140    ///
141    /// * `limit` - Resource-bound sequence-item limit to install.
142    ///
143    /// # Returns
144    ///
145    /// The builder with the described setting applied.
146    #[inline]
147    #[must_use]
148    pub fn sequence_items_limit(mut self, limit: ResourceLimit<R, Q>) -> Self {
149        self.limits.set_sequence_items_limit(limit);
150        self
151    }
152
153    /// Sets the map-entry limit.
154    ///
155    /// # Parameters
156    ///
157    /// * `limit` - Resource-bound map-entry limit to install.
158    ///
159    /// # Returns
160    ///
161    /// The builder with the described setting applied.
162    #[inline]
163    #[must_use]
164    pub fn map_entries_limit(mut self, limit: ResourceLimit<R, Q>) -> Self {
165        self.limits.set_map_entries_limit(limit);
166        self
167    }
168
169    /// Sets the structural-key limit.
170    ///
171    /// # Parameters
172    ///
173    /// * `limit` - Resource-bound structural-key limit to install.
174    ///
175    /// # Returns
176    ///
177    /// The builder with the described setting applied.
178    #[inline]
179    #[must_use]
180    pub fn key_bytes_limit(mut self, limit: ResourceLimit<R, Q>) -> Self {
181        self.limits.set_key_bytes_limit(limit);
182        self
183    }
184
185    /// Builds the configured structural limits.
186    ///
187    /// # Returns
188    ///
189    /// Builds the configured structural limits.
190    #[inline]
191    #[must_use]
192    pub fn build(self) -> StructureLimits<R, Q> {
193        self.limits
194    }
195}
196
197impl StructureLimitsBuilder<StructureResource, usize> {
198    /// Sets the maximum nesting depth.
199    ///
200    /// # Parameters
201    ///
202    /// * `maximum` - Inclusive maximum to configure.
203    ///
204    /// # Returns
205    ///
206    /// The builder with the described setting applied.
207    #[inline]
208    #[must_use]
209    pub const fn max_depth(mut self, maximum: usize) -> Self {
210        self.limits.set_max_depth(maximum);
211        self
212    }
213
214    /// Sets the maximum number of processed nodes.
215    ///
216    /// # Parameters
217    ///
218    /// * `maximum` - Inclusive maximum to configure.
219    ///
220    /// # Returns
221    ///
222    /// The builder with the described setting applied.
223    #[inline]
224    #[must_use]
225    pub const fn max_nodes(mut self, maximum: usize) -> Self {
226        self.limits.set_max_nodes(maximum);
227        self
228    }
229
230    /// Sets the maximum number of items in one sequence.
231    ///
232    /// # Parameters
233    ///
234    /// * `maximum` - Inclusive maximum to configure.
235    ///
236    /// # Returns
237    ///
238    /// The builder with the described setting applied.
239    #[inline]
240    #[must_use]
241    pub const fn max_sequence_items(mut self, maximum: usize) -> Self {
242        self.limits.set_max_sequence_items(maximum);
243        self
244    }
245
246    /// Sets the maximum number of entries in one map.
247    ///
248    /// # Parameters
249    ///
250    /// * `maximum` - Inclusive maximum to configure.
251    ///
252    /// # Returns
253    ///
254    /// The builder with the described setting applied.
255    #[inline]
256    #[must_use]
257    pub const fn max_map_entries(mut self, maximum: usize) -> Self {
258        self.limits.set_max_map_entries(maximum);
259        self
260    }
261
262    /// Sets the maximum byte length of one structural key.
263    ///
264    /// # Parameters
265    ///
266    /// * `maximum` - Inclusive maximum to configure.
267    ///
268    /// # Returns
269    ///
270    /// The builder with the described setting applied.
271    #[inline]
272    #[must_use]
273    pub const fn max_key_bytes(mut self, maximum: usize) -> Self {
274        self.limits.set_max_key_bytes(maximum);
275        self
276    }
277}