-
-
Notifications
You must be signed in to change notification settings - Fork 297
Bad sprite dir warnings and svg validation #2211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
for more information, see https://pre-commit.ci
There was a problem hiding this 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.
| let filename = entry_path | ||
| .file_stem() | ||
| .map(|s| s.to_string_lossy().to_string()) | ||
| .unwrap_or_default(); |
Copilot
AI
Sep 19, 2025
There was a problem hiding this comment.
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.
|
|
||
| /// 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()); |
Copilot
AI
Sep 19, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: |
Copilot
AI
Sep 19, 2025
There was a problem hiding this comment.
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.
| } else { | ||
| warn!( | ||
| "No SVG files found in sprite directory: {disp_path}. Please ensure the directory contains .svg files." | ||
| ); |
Copilot
AI
Sep 19, 2025
There was a problem hiding this comment.
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.
| } else { | ||
| warn!( | ||
| "Sprite source validation completed with errors: {}/{} sources valid, {} errors encountered", | ||
| valid_sources, |
Copilot
AI
Sep 19, 2025
There was a problem hiding this comment.
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.
…rtin into sprite_dir_warnings
for more information, see https://pre-commit.ci
CommanderStorm
left a comment
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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
ef0fe33 to
3db4484
Compare
| v.key(), | ||
| v.get().path.display() | ||
| ); | ||
| return Err(SpriteError::DirectoryValidationFailed( |
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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.
3f1c5f7 to
1631a89
Compare
| ); | ||
| return Err(SpriteError::DirectoryValidationFailed( | ||
| path, | ||
| "Path is not a directory".to_string(), |
There was a problem hiding this comment.
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
a4ded48 to
abb1e01
Compare
CommanderStorm
left a comment
There was a problem hiding this 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> { |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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
2cd72c6 to
af5b91d
Compare
for more information, see https://pre-commit.ci
Addresses #2136 by:
Please let me know if this ought to be edited or the validation could be moved elsewhere.
Thanks!