qubit_budget/value/string_limits_builder.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//! Builds UTF-8 byte limits for one string value.
9
10use super::StringLimits;
11use crate::resource::ResourceLimit;
12use crate::resource::ResourceQuantity;
13
14/// Builder for [`StringLimits`].
15///
16/// # Type Parameters
17///
18/// * `R` - Caller-defined resource identity retained by limits and errors.
19/// * `Q` - Exact unsigned quantity used for measurements and accounting.
20///
21/// # Examples
22///
23/// ```
24/// use qubit_budget::ResourceLimit;
25/// use qubit_budget::StringLimitsBuilder;
26///
27/// let limits = StringLimitsBuilder::new()
28/// .utf8_bytes_limit(ResourceLimit::new("string bytes", 4_u64))
29/// .build();
30/// assert_eq!(limits.utf8_bytes_limit().unwrap().maximum(), 4);
31/// ```
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
33pub struct StringLimitsBuilder<R, Q = u64>
34where
35 Q: ResourceQuantity,
36{
37 /// Limit configuration accumulated by chained builder calls.
38 limits: StringLimits<R, Q>,
39}
40
41impl<R, Q> Default for StringLimitsBuilder<R, Q>
42where
43 Q: ResourceQuantity,
44{
45 /// Creates an empty builder through the standard [`Default`] interface.
46 ///
47 /// # Returns
48 ///
49 /// Creates an empty builder through the standard [`Default`] interface.
50 fn default() -> Self {
51 Self::new()
52 }
53}
54
55impl<R, Q> StringLimitsBuilder<R, Q>
56where
57 Q: ResourceQuantity,
58{
59 /// Creates an empty string-limits builder.
60 ///
61 /// # Returns
62 ///
63 /// Creates an empty string-limits builder.
64 #[inline]
65 #[must_use]
66 pub const fn new() -> Self {
67 Self {
68 limits: StringLimits::new(),
69 }
70 }
71
72 /// Creates a builder retaining an existing limit configuration.
73 ///
74 /// # Parameters
75 ///
76 /// * `limits` - Existing string limits whose configuration is copied into
77 /// this builder.
78 ///
79 /// # Returns
80 ///
81 /// Creates a builder retaining an existing limit configuration.
82 #[inline]
83 #[must_use]
84 pub(crate) const fn from_limits(limits: StringLimits<R, Q>) -> Self {
85 Self { limits }
86 }
87
88 /// Sets the inclusive UTF-8 byte limit.
89 ///
90 /// # Parameters
91 ///
92 /// * `limit` - Resource-bound UTF-8 byte limit to install.
93 ///
94 /// # Returns
95 ///
96 /// The builder with the described setting applied.
97 #[inline]
98 #[must_use]
99 pub fn utf8_bytes_limit(mut self, limit: ResourceLimit<R, Q>) -> Self {
100 self.limits.set_utf8_bytes_limit(limit);
101 self
102 }
103
104 /// Builds the configured string limits.
105 ///
106 /// # Returns
107 ///
108 /// Builds the configured string limits.
109 #[inline]
110 #[must_use]
111 pub fn build(self) -> StringLimits<R, Q> {
112 self.limits
113 }
114}