pemistahl/grex · error

{}

Error message

{}

What it means

This is the catch-all branch of RegExpBuilder::from_file: any std::io::Error whose kind is not NotFound, InvalidData, or PermissionDenied is interpolated directly into the panic message ('{}'). It surfaces the raw OS error text, e.g. 'Is a directory (os error 21)'.

Solutions

  1. Confirm the path points to a regular file (Path::is_file) and not a directory or device
  2. Read the file yourself with std::fs::read_to_string and match on the io::Error to log a contextual message
  3. Inspect the raw os error code in the panic message to identify the specific filesystem problem

Example fix

// before
let builder = RegExpBuilder::from_file("data"); // data is a directory
// after
let path = Path::new("data/examples.txt");
assert!(path.is_file(), "not a regular file: {}", path.display());
let builder = RegExpBuilder::from_file(path);
Defensive patterns

Strategy: validation

Validate before calling

let meta = std::fs::metadata(path)?;
if !meta.is_file() { return Err(anyhow!("not a regular file: {}", path.display())); }

Try / catch

std::panic::catch_unwind(|| RegExpBuilder::from_file(path))
    .map_err(|_| format!("failed to read {}: verify it is a readable regular file", path.display()))

Prevention

When it happens

Trigger: Calling from_file with a path to a directory (IsADirectory), a path with too many symlinks, an interrupted-but-undetectable io error, or any platform-specific io failure outside the three special-cased kinds.

Common situations: Passing a directory path by mistake; a path that resolves through a symlink loop; exotic filesystems returning unusual error kinds.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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

Appendix: source

Thrown at src/builder.rs:83

    /// ⚠ Panics if:
    /// - the file cannot be found
    /// - the file's encoding is not valid UTF-8 data
    /// - the file cannot be opened because of conflicting permissions
    pub fn from_file<T: Into<PathBuf>>(file_path: T) -> Self {
        match std::fs::read_to_string(file_path.into()) {
            Ok(file_content) => Self {
                test_cases: file_content.lines().map(|it| it.to_string()).collect_vec(),
                config: RegExpConfig::new(),
            },
            Err(error) => match error.kind() {
                ErrorKind::NotFound => panic!("The specified file could not be found"),
                ErrorKind::InvalidData => {
                    panic!("The specified file's encoding is not valid UTF-8")
                }
                ErrorKind::PermissionDenied => {
                    panic!("Permission denied: The specified file could not be opened")
                }
                _ => panic!("{}", error),
            },
        }
    }

    /// Converts any Unicode decimal digit to character class `\d`.
    ///
    /// This method takes precedence over
    /// [`with_conversion_of_words`](Self::with_conversion_of_words) if both are set.
    /// Decimal digits are converted to `\d`, the remaining word characters to `\w`.
    ///
    /// This method takes precedence over
    /// [`with_conversion_of_non_whitespace`](Self::with_conversion_of_non_whitespace) if both are set.
    /// Decimal digits are converted to `\d`, the remaining non-whitespace characters to `\S`.
    pub fn with_conversion_of_digits(&mut self) -> &mut Self {
        self.config.is_digit_converted = true;
        self
    }

View on GitHub (pinned to 99cc347707)