qubit-argument

Argument and state validation helpers for Rust applications

Rust CI Coverage Crates.io Rust License

Argument and state validation utilities for Rust.

Overview

Qubit Argument provides extension traits and checking functions for validating function arguments, configuration values, indexes, ranges, and runtime state. It uses Rust's trait extension pattern for readable validation chains while returning a small structured ArgumentError.

Design Goals

Features

Numeric Validation

String Validation

Collection and Option Validation

State and Bounds Checking

Installation

Add this to your Cargo.toml:

[dependencies]
qubit-argument = "0.3"

Quick Start

Numeric and String Validation

use qubit_argument::{
    ArgumentResult,
    NumericArgument,
    StringArgument,
};

fn validate_user(age: i32, username: &str) -> ArgumentResult<()> {
    age.require_in_closed_range("age", 0, 150)?;
    username
        .require_non_blank("username")?
        .require_length_in_range("username", 3, 20)?;
    Ok(())
}

Collection Validation

use qubit_argument::{
    ArgumentResult,
    CollectionArgument,
};

fn validate_tags(tags: &[String]) -> ArgumentResult<&[String]> {
    tags.require_non_empty("tags")?
        .require_length_at_most("tags", 10)
}

State and Bounds Checking

use qubit_argument::{
    ArgumentResult,
    check_bounds,
    check_state_with_message,
};

fn read_range(offset: usize, length: usize, total: usize) -> ArgumentResult<()> {
    check_state_with_message(total > 0, "total length must be positive")?;
    check_bounds(offset, length, total)
}

API Reference

Traits

Error Types

Functions

Testing & Code Coverage

This project maintains comprehensive test coverage for success paths, failure paths, boundary conditions, and representative type instantiations.

Running Tests

# Run all tests
cargo test

# Run with coverage report
./coverage.sh

# Generate text format report
./coverage.sh text

# Run CI checks (format, clippy, test, coverage, audit)
./ci-check.sh

Coverage Metrics

See COVERAGE.md for detailed coverage statistics.

Dependencies

Runtime dependencies are intentionally small:

License

Copyright (c) 2025 - 2026. Haixing Hu, Qubit Co. Ltd. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

See LICENSE for the full license text.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Guidelines

Author

Haixing Hu - Qubit Co. Ltd.

More Rust libraries from Qubit are published under the qubit-ltd organization on GitHub.

---

Repository: https://github.com/qubit-ltd/rs-argument