glzr-io/glazewm · error

Invalid regex.

Error message

Invalid regex.

What it means

`LengthValue::from_str` compiles the unit-parsing regex `([+-]?\d+)(%|px)?` with `.expect("Invalid regex.")`. This is a panic, not a returned error: the pattern is a compile-time constant and is always valid, so the panic can only fire if the regex crate itself is broken or the constant was edited incorrectly. Normal parse failures of the input string return `ParseError::Length` instead.

Solutions

  1. Verify the regex constant is exactly `r"([+-]?\d+)(%|px)?"` and restore it if modified.
  2. If it was intentionally made dynamic, use `Regex::new(...).map_err(...)` and surface a proper `ParseError` instead of `.expect`.
  3. If you merely need to fix length parsing, correct the input format (e.g. `100px`, `50%`) — this message is unrelated to bad input.
  4. Check for corrupted builds/dependency issues if this panic appears with unmodified sources.

Example fix

// before
let units_regex = Regex::new(dynamic_user_pattern).expect("Invalid regex.");
// after
let units_regex = Regex::new(r"([+-]?\d+)(%|px)?").map_err(|_| crate::ParseError::Length(unparsed.to_string()))?;
Defensive patterns

Strategy: try-catch

Validate before calling

fn is_valid_length(s: &str) -> bool {
  s.trim().parse::<i32>().is_ok() || s.ends_with("px") || s.ends_with('%')
}

Try / catch

match LengthValue::from_str(input) {
  Ok(v) => v,
  Err(crate::ParseError::Length(s)) => { eprintln!("Bad length: {s}"); default }
  Err(e) => return Err(e.into()),
}

Prevention

When it happens

Trigger: Only reachable if the hardcoded regex literal is modified to an invalid pattern, or a build/dependency issue breaks the `regex` engine — never from user input like `"100px"` or garbage strings (those yield `ParseError::Length`).

Common situations: Essentially unreachable in released builds; may appear in forked/patched code or during development when someone edits the regex constant. Otherwise, users will see `ParseError` for unparseable length strings, not this message.

Related errors


AI-assisted analysis of glzr-io/glazewm@5709ad0a3c (2026-09-08). Data as JSON: /api/errors/c180651b564f281a. Report an issue: GitHub.

Appendix: source

Thrown at packages/wm-platform/src/models/length_value.rs:69

  type Err = crate::ParseError;

  /// Parses a string containing a number followed by a unit (`px`, `%`).
  /// Allows for negative numbers.
  ///
  /// Example:
  /// ```
  /// # use wm_platform::{LengthValue, LengthUnit};
  /// # use std::str::FromStr;
  /// let check = LengthValue {
  ///   amount: 100.0,
  ///   unit: LengthUnit::Pixel,
  /// };
  /// let parsed = LengthValue::from_str("100px");
  /// assert_eq!(parsed.unwrap(), check);
  /// ```
  fn from_str(unparsed: &str) -> Result<Self, crate::ParseError> {
    let units_regex =
      Regex::new(r"([+-]?\d+)(%|px)?").expect("Invalid regex.");

    let captures = units_regex
      .captures(unparsed)
      .ok_or(crate::ParseError::Length(unparsed.to_string()))?;

    let unit = match captures.get(2).map_or("", |m| m.as_str()) {
      "px" | "" => LengthUnit::Pixel,
      "%" => LengthUnit::Percentage,
      _ => return Err(crate::ParseError::Length(unparsed.to_string())),
    };

    let amount = captures
      .get(1)
      .and_then(|m| m.as_str().parse::<f32>().ok())
      // Store percentage units as a fraction of 1.
      .map(|amount| {
        if unit == LengthUnit::Percentage {
          amount / 100.0

View on GitHub (pinned to 5709ad0a3c)