qubit-budget 0.4.0

Dependency-light resource limit and budget accounting primitives for Qubit Rust crates
Documentation
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Builds UTF-8 byte limits for one string value.

use super::StringLimits;
use crate::resource::ResourceLimit;
use crate::resource::ResourceQuantity;

/// Builder for [`StringLimits`].
///
/// # Type Parameters
///
/// * `R` - Caller-defined resource identity retained by limits and errors.
/// * `Q` - Exact unsigned quantity used for measurements and accounting.
///
/// # Examples
///
/// ```
/// use qubit_budget::ResourceLimit;
/// use qubit_budget::StringLimitsBuilder;
///
/// let limits = StringLimitsBuilder::new()
///     .utf8_bytes_limit(ResourceLimit::new("string bytes", 4_u64))
///     .build();
/// assert_eq!(limits.utf8_bytes_limit().unwrap().maximum(), 4);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StringLimitsBuilder<R, Q = u64>
where
    Q: ResourceQuantity,
{
    /// Limit configuration accumulated by chained builder calls.
    limits: StringLimits<R, Q>,
}

impl<R, Q> Default for StringLimitsBuilder<R, Q>
where
    Q: ResourceQuantity,
{
    /// Creates an empty builder through the standard [`Default`] interface.
    ///
    /// # Returns
    ///
    /// Creates an empty builder through the standard [`Default`] interface.
    fn default() -> Self {
        Self::new()
    }
}

impl<R, Q> StringLimitsBuilder<R, Q>
where
    Q: ResourceQuantity,
{
    /// Creates an empty string-limits builder.
    ///
    /// # Returns
    ///
    /// Creates an empty string-limits builder.
    #[inline]
    #[must_use]
    pub const fn new() -> Self {
        Self {
            limits: StringLimits::new(),
        }
    }

    /// Creates a builder retaining an existing limit configuration.
    ///
    /// # Parameters
    ///
    /// * `limits` - Existing string limits whose configuration is copied into
    ///   this builder.
    ///
    /// # Returns
    ///
    /// Creates a builder retaining an existing limit configuration.
    #[inline]
    #[must_use]
    pub(crate) const fn from_limits(limits: StringLimits<R, Q>) -> Self {
        Self { limits }
    }

    /// Sets the inclusive UTF-8 byte limit.
    ///
    /// # Parameters
    ///
    /// * `limit` - Resource-bound UTF-8 byte limit to install.
    ///
    /// # Returns
    ///
    /// The builder with the described setting applied.
    #[inline]
    #[must_use]
    pub fn utf8_bytes_limit(mut self, limit: ResourceLimit<R, Q>) -> Self {
        self.limits.set_utf8_bytes_limit(limit);
        self
    }

    /// Builds the configured string limits.
    ///
    /// # Returns
    ///
    /// Builds the configured string limits.
    #[inline]
    #[must_use]
    pub fn build(self) -> StringLimits<R, Q> {
        self.limits
    }
}