Skip to content

Conversation

@nuts-rice
Copy link

Addresses #2136 by:

  • Adding errors for empty dirs, invalid svg format + Failed Dir validation
  • Doing basic SVG format validation and warning on fails for individual files
  • Then validates whole directory , warns if empty, and provides summary of validated or failed files, tracks files that appear to be pre-generated
  • comprehensive warning messages if files appear to be pre-generated

Please let me know if this ought to be edited or the validation could be moved elsewhere.
Thanks!

Copilot AI review requested due to automatic review settings September 19, 2025 17:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements comprehensive sprite directory validation by adding SVG format checking and detailed warning messages for empty directories and pre-generated sprite files. It addresses issue #2136 by providing better feedback when sprite sources are misconfigured.

  • Adds validation for sprite source directories including SVG format checking
  • Introduces detailed error handling for empty directories and invalid SVG files
  • Provides comprehensive warnings when directories contain pre-generated sprite files instead of source SVGs

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
martin-core/src/resources/sprites/mod.rs Implements directory validation, SVG format checking, and enhanced warning messages for sprite sources
martin-core/src/resources/sprites/error.rs Adds new error types for empty directories, invalid SVG format, and directory validation failures

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines 103 to 106
let filename = entry_path
.file_stem()
.map(|s| s.to_string_lossy().to_string())
.unwrap_or_default();
Copy link

Copilot AI Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic for detecting pre-generated sprite files is duplicated in multiple places (lines 103-110, 190-197). Consider extracting this into a helper function to improve maintainability.

Copilot uses AI. Check for mistakes.

/// Validates an individual SVG file for format.
async fn validate_svg_file(&self, path: &PathBuf) -> Result<(), SpriteError> {
let on_err = |e| SpriteError::IoError(e, path.clone());
Copy link

Copilot AI Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SVG validation is too restrictive. Valid SVG files can start with whitespace, comments, or DOCTYPE declarations before the <?xml or <svg tags. Consider using trim() before checking or using a more flexible approach.

Copilot uses AI. Check for mistakes.
Copy link
Author

@nuts-rice nuts-rice Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let content = content.trim();
is already before the check, correct?

}

/// Validates a sprite source directory to ensure it contains valid SVG files.
/// Checks include:
Copy link

Copilot AI Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter should be path: &Path instead of path: &PathBuf. Taking a reference to PathBuf is unnecessary since Path is the borrowed version that should be used in function parameters.

Copilot uses AI. Check for mistakes.
} else {
warn!(
"No SVG files found in sprite directory: {disp_path}. Please ensure the directory contains .svg files."
);
Copy link

Copilot AI Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter should be path: &Path instead of path: &PathBuf. Taking a reference to PathBuf is unnecessary since Path is the borrowed version that should be used in function parameters.

Copilot uses AI. Check for mistakes.
} else {
warn!(
"Sprite source validation completed with errors: {}/{} sources valid, {} errors encountered",
valid_sources,
Copy link

Copilot AI Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter should be path: &Path instead of path: &PathBuf. Taking a reference to PathBuf is unnecessary since Path is the borrowed version that should be used in function parameters.

Copilot uses AI. Check for mistakes.
Copy link
Member

@CommanderStorm CommanderStorm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there so much duplicate code? This does not seem very maintainable.
Please do a polishing pass 😉

Let's also likely change add_sprite to be a Result<(), SpritError> function as users likely want this instead.

return;
}

match std::fs::read_dir(&path) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this validation to a new function. It is kind of messy to read otherwise

v.key(),
v.get().path.display()
);
return Err(SpriteError::DirectoryValidationFailed(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a warning is mutually exclusive with an error - either it fails, in which case the error message will be printed or handled by the caller, or it continues the execution but prints a warning message. Both do not make sense

}

/// Validates all configured sprite sources and logs a summary.
pub async fn validate_all_sources(&self) -> Result<(), SpriteError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validate_all_sources is never used.
As I asked before, please do a polishing pass.

);
return Err(SpriteError::DirectoryValidationFailed(
path,
"Path is not a directory".to_string(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a minor nit, but makes for a better pattern -- if you have several similar errors with some magical string, easier to just have dedicated errors one for each. This way they are much faster to create (you don't need to allocate a new string on each error), and all content of an error is in the error description only

Copy link
Member

@CommanderStorm CommanderStorm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but this still needs a deeper scrubbing.

Please add a unit test for each new behaviour (to prevent regressions and go over the code you are trying to add again.

/// - Directory existence and accessibility
/// - Presence of SVG files
/// - Basic SVG format validation
pub async fn validate_source_directory(&self, path: &PathBuf) -> Result<(), SpriteError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add dead code.
As I said above, this is never used in the codebase.

async fn test_sprites() {
let mut sprites = SpriteSources::default();
sprites.add_source(
let _ = sprites.add_source(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's unwrap instead to make sure this is not an error

@CommanderStorm CommanderStorm marked this pull request as draft September 23, 2025 00:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants