Repository: bevyengine/bevy
File: tools/export-content/src/app.rs (Rust)

Code before:
            .wrap_err(format!("unable to access {} file", kind))?;

        // Skip directories
        if !dir_entry.path().is_file() {
            continue;
        }
        // Skip files with invalid names
        let Ok(file_name) = dir_entry.file_name().into_string() else {
            continue;
        };
        // Skip hidden files (like .gitkeep or .DS_Store)
        if file_name.starts_with(".") {
            continue;
        }

        let file_content = fs::read_to_string(&dir_entry.path())
            .into_diagnostic()
            .wrap_err(format!("unable to read {} file", kind))?;

        let caps = re.captures(&file_content).ok_or(diagnostic!(
            "failed to find frontmatter in {} file {}",
            kind,
            file_name
        ))?;

        let frontmatter = caps.get(1).unwrap().as_str();
        let metadata = serde_yml::from_str::<Metadata>(frontmatter).map_err(|e| ParseError {
            src: frontmatter.to_owned(),
            kind,
            file_name,
            err_span: e.location().map(|l| l.index()),
            error: e,
        })?;
        let content = caps.get(2).unwrap().as_str().to_owned();

        entries.push(Entry::File { metadata, content })
    }

    Ok(entries)
}

#[derive(Diagnostic, Debug, Error)]
#[error("failed to parse metadata in {kind} file {file_name}")]
pub struct ParseError {
    #[source_code]
    src: String,
    kind: &'static str,
    file_name: String,
    #[label("{error}")]
    err_span: Option<usize>,
    error: serde_yml::Error,

Review comment (suggestion):

Using named groups this would be

````rs
  let frontmatter = caps.name("frontmatter").unwrap().as_str();
  let metadata = serde_yml::from_str::<Metadata>(frontmatter).map_err(|e| ParseError {
      src: frontmatter.to_owned(),
      kind,
      file_name,
      err_span: e.location().map(|l| l.index()),
      error: e,
  })?;
  let content = caps.name("content").unwrap().as_str().to_owned();
````

Code after:
        // Skip directories
        if !dir_entry.path().is_file() {
            continue;
        }
        // Skip files with invalid names
        let Ok(file_name) = dir_entry.file_name().into_string() else {
            continue;
        };
        // Skip hidden files (like .gitkeep or .DS_Store)
        if file_name.starts_with(".") {
            continue;
        }

        let file_content = fs::read_to_string(dir_entry.path())
            .into_diagnostic()
            .wrap_err(format!("unable to read {} file", kind))?;

        let caps = re.captures(&file_content).ok_or(diagnostic!(
            "failed to find frontmatter in {} file {}",
            kind,
            file_name
        ))?;

        let frontmatter = caps.name("frontmatter").unwrap().as_str();
        let metadata = serde_yml::from_str::<Metadata>(frontmatter).map_err(|e| ParseError {
            src: NamedSource::new(
                format!("{}", dir_entry.path().display()),
                frontmatter.to_owned(),
            ),
            kind,
            file_name,
            err_span: e.location().map(|l| l.index()),
            error: e,
        })?;
        let content = caps.name("content").unwrap().as_str().to_owned();

        entries.push(Entry::File { metadata, content });
    }

    Ok(entries)
}

#[derive(Diagnostic, Debug, Error)]
#[error("failed to parse metadata in {kind} file {file_name}")]
pub struct ParseError {
    #[source_code]
    src: NamedSource<String>,
    kind: &'static str,
    file_name: String,
    #[label("{error}")]
    err_span: Option<usize>,