Parsing Utils
Utilities for parsing and processing integer sequences and ranges.
Overview
The Parsing Utils package provides:
- Integer Sequence Parsing: Parse comma-separated integer lists
- Integer Range Parsing: Parse hyphen-separated integer ranges
- Range Validation: Prevent excessive range sizes
- Error Handling: Parse error types for invalid input plus documented panic edge cases
Features
Integer Sequence Parsing
- Comma-separated Lists: Parse "1,2,3,4,5" into Vec
- Single Values: Handle single integers as sequences
- Validation: Ensure all values are valid u64 integers
Integer Range Parsing
- Hyphen-separated Ranges: Parse "1-10" into expanded sequence
- Range Limits: Prevent ranges with more than 100,000 interpolated values
- Comma-separated Integers: Handle comma-separated integers within range boundaries
Error Handling
- Parse Errors: Invalid integer format detection
- Unmatched Ranges: Detect malformed range syntax
- Size Limits: Prevent memory exhaustion from large ranges
- Panic Edge Cases:
parse_integer_rangescan panic for reverse/equal ranges
Installation
Add this to your Cargo.toml:
[]
= { = "../parsing_utils" }
Usage
Parse Integer Sequences
use parse_integer_sequences;
// Parse comma-separated integers
let result = parse_integer_sequences?;
assert_eq!;
// Parse single integer
let result = parse_integer_sequences?;
assert_eq!;
// Handle parsing errors
match parse_integer_sequences
Parse Integer Ranges
use parse_integer_ranges;
// Parse simple range
let result = parse_integer_ranges?;
assert_eq!;
// Parse comma-separated sequences (no ranges)
let result = parse_integer_ranges?;
assert_eq!;
// Parse range with comma-separated start values
let result = parse_integer_ranges?;
assert_eq!;
// Parse range with comma-separated start and end values
let result = parse_integer_ranges?;
assert_eq!;
Error Handling
use ;
// Handle different error types
match parse_integer_ranges
Range Parsing Logic
Simple Sequences
- Input:
"1,2,3,4" - Output:
[1, 2, 3, 4]
Simple Ranges
- Input:
"1-5" - Output:
[1, 2, 3, 4, 5]
Ranges with Commas
- Input:
"1,2,3-7,8" - Processing:
- Split by hyphens:
["1,2,3", "7,8"] - Parse start sequence:
"1,2,3"→[1, 2, 3] - Parse end sequence:
"7,8"→[7, 8] - Take last from start (
3) and first from end (7) - Expand range from
4to6:[4, 5, 6] - Combine:
[1, 2, 3, 4, 5, 6, 7, 8]
- Split by hyphens:
Note: The function splits on hyphens first, treating them as range delimiters. Commas are parsed within the start and end portions of a range.
Range Validation
- Maximum interpolated range span: 100,000 values
- Prevents memory exhaustion attacks
- Returns
RangeTooLargeerror for excessive ranges
Error Types
ParseIntegersError
Error Examples
use parse_integer_ranges;
// ParseId error
let result = parse_integer_ranges;
// Error: ParseId("abc")
// UnmatchedRange error (odd number of range parts > 1)
let result = parse_integer_ranges;
// Error: UnmatchedRange("1-2-3")
// RangeTooLarge error
let result = parse_integer_ranges;
// Error: RangeTooLarge("1-200000")
Performance Considerations
- Memory Usage: Large ranges are expanded into Vec
- Range Limits: 100,000 item maximum prevents excessive memory usage
- Parsing Speed: Simple split-and-parse approach for efficiency
- Error Handling: Early validation prevents unnecessary processing
Use Cases
- ID Range Processing: Parse user input for ID ranges
- Batch Operations: Process sequences of items
- Configuration Parsing: Parse numeric configuration values
- Data Import: Handle CSV-style numeric data
- API Parameters: Parse query parameters with ranges
Dependencies
- thiserror: Error handling and display traits