pemistahl/grex · error

Minimum substring length must be greater than zero

Error message

Minimum substring length must be greater than zero

What it means

RegExpBuilder::with_minimum_substring_length panics with MINIMUM_SUBSTRING_LENGTH_MESSAGE when length is 0. Substrings of length zero are meaningless for the repeated-substring conversion, so zero is rejected eagerly. The default, if unset, is 1.

Solutions

  1. Pass a length >= 1; omit the call entirely to use the default of 1
  2. Clamp the input (length.max(1)) before calling the method
  3. Validate the config/CLI value at parse time and reject 0 with a clear user-facing error

Example fix

// before
builder.with_minimum_substring_length(min_len); // min_len may be 0
// after
let min_len = min_len.max(1);
builder.with_minimum_substring_length(min_len);
Defensive patterns

Strategy: validation

Validate before calling

if min_len == 0 { return Err(anyhow!("--min-length must be >= 1")); }
builder.with_minimum_substring_length(min_len);

Try / catch

let b = std::panic::catch_unwind(|| { let mut b = RegExpBuilder::from(&cases); b.with_minimum_substring_length(l); b })
    .map_err(|_| "minimum substring length must be > 0".to_string());

Prevention

When it happens

Trigger: Calling builder.with_minimum_substring_length(0), usually from an unvalidated CLI flag or config value that defaulted to zero.

Common situations: An optional config key like min_substring_length absent from the file and parsed as 0; a compute expression evaluating to zero; user typing --min-length 0.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of pemistahl/grex@99cc347707 (2026-09-13). Data as JSON: /api/errors/3ed5bb4f122dbf50. Report an issue: GitHub.

Appendix: source

Thrown at src/builder.rs:202

    ///
    /// ⚠ Panics if `quantity` is zero.
    pub fn with_minimum_repetitions(&mut self, quantity: u32) -> &mut Self {
        if quantity == 0 {
            panic!("{}", MINIMUM_REPETITIONS_MESSAGE);
        }
        self.config.minimum_repetitions = quantity;
        self
    }

    /// Specifies the minimum length a repeated substring must have in order to be converted if
    /// [`with_conversion_of_repetitions`](Self::with_conversion_of_repetitions) is set.
    ///
    /// If the length is not explicitly set with this method, a default value of 1 will be used.
    ///
    /// ⚠ Panics if `length` is zero.
    pub fn with_minimum_substring_length(&mut self, length: u32) -> &mut Self {
        if length == 0 {
            panic!("{}", MINIMUM_SUBSTRING_LENGTH_MESSAGE);
        }
        self.config.minimum_substring_length = length;
        self
    }

    /// Converts non-ASCII characters to unicode escape sequences.
    /// The parameter `use_surrogate_pairs` specifies whether to convert astral code planes
    /// (range `U+010000` to `U+10FFFF`) to surrogate pairs.
    pub fn with_escaping_of_non_ascii_chars(&mut self, use_surrogate_pairs: bool) -> &mut Self {
        self.config.is_non_ascii_char_escaped = true;
        self.config.is_astral_code_point_converted_to_surrogate = use_surrogate_pairs;
        self
    }

    /// Produces a nicer looking regular expression in verbose mode.
    pub fn with_verbose_mode(&mut self) -> &mut Self {
        self.config.is_verbose_mode_enabled = true;
        self

View on GitHub (pinned to 99cc347707)