-
Notifications
You must be signed in to change notification settings - Fork 252
chore: file.rs - Remove add_dummy_extension() helper
#678
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -59,9 +59,10 @@ impl FileSourceFile { | |||||||||||||||||||||||||||||||||||
| ))) | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| // Adding a dummy extension will make sure we will not override secondary extensions, i.e. "file.local" | ||||||||||||||||||||||||||||||||||||
| // This will make the following set_extension function calls to append the extension. | ||||||||||||||||||||||||||||||||||||
| let mut filename = add_dummy_extension(filename); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Append an extension that will be replaced when calling `set_extension()`: | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
| // Append an extension that will be replaced when calling `set_extension()`: | |
| // Add a fake extension which will be replaced by `set_extension()` calls. | |
| // Prevents accidentally replacing a portion of the base filename with an existing `.` (ref: #306) |
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.
If we're working to improve it, rather than preserve it (since it was sufficient)
| // Append an extension that will be replaced when calling `set_extension()`: | |
| // Preserve any extension-like text within the provided file stem (e.g. `file.local`) by appending a fake extension which will be replaced by `set_extension()` calls (e.g. `file.local.json`). |
Outdated
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.
Alternatively, I think using a conditional communicates the intention better 😅 (even though it's safe to just append always in our case)
| // Append an extension that will be replaced when calling `set_extension()`: | |
| let mut filename = filename; | |
| filename.as_mut_os_string().push(".ext_placeholder"); | |
| let mut filename = filename; | |
| // Avoid accidental inference of an extension by appending a placeholder for `set_extension()` to replace instead (ref: #306) | |
| if filename.extension().is_some() { | |
| filename.as_mut_os_string().push(".placeholder"); | |
| } |
Comment with original PR reference of the issue for added context.
I'm not wanting to bike shed this though, so feel free to prefer your suggestion or the original and I'm happy to go with either of those too 👍 (I personally did not find the file.local example helpful, it's definitely not about existing extensions so much as it is a file name with one or more . getting it's final suffix replaced accidentally)
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.
I still prefer my version but can go either way on the location.
I personally did not find the file.local example helpful, it's definitely not about existing extensions so much as it is a file name with one or more . getting it's final suffix replaced accidentally
When you have a file stem with a . it can be mistaken for an extension which is what I was trying to convey in my text and with the example
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.
I still prefer my version but can go either way on the location.
"either way on location"? Sorry not sure what you meant by that.
Did you mean you would like your version, and are flexible with where it's located in the file? 🤔 (presumably because my last suggestion moved the comment under let mut filename instead of before it as it once was)
I'll assume that and go with your comment 👍
The remainder of this response is just sharing my opinion on why I think my version was simpler to grok (with minimal familiarity required to the reader).
When you have a file stem with a
.it can be mistaken for an extension
It's technically not the file stem though, it's the full PathBuf 😅
If we used filename.file_stem() we'd get the stem, but with that same concern of the last . inferring an extension. So you're right in that at this point we want to think of filename as the file stem with the placeholder appended as the extension to be replaced.
The conditional statement added now better communicates this expectation of a preventative measure for an inferred extension? Which the associated comment provides additional clarity.
// Preserve any extension-like text within the provided file stem (e.g. `file.local`) by appending a fake extension which will be replaced by `set_extension()` calls (e.g. `file.local.json`).
This reads with more mental overhead to me at least 😅
- "file stem"
file.localimplicitly referring tofilename file.local.jsonas example for output after aset_extension()call replaces the fake extension.
// Adding a dummy extension will make sure we will not override secondary extensions, i.e. "file.local"
This original comment didn't explain the "why" that well and I had to use git blame to lookup the commit/PR for extra context.
This one reads like myfile.tar as a 2nd extension to myfile.tar.gz, but I don't really think of extensions in a filename liberally using . either like my.file.name.here 😅
Accidental inference of an extension reads much more clearly (it's only a 2ndry extension in the context of intent to later append the config extensions via set_extension()).
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 has swapped out my comment for yours, with a slight modification to better convey the expected transformation for a file stem file.local. Hopefully a good compromise 😅
| // Append an extension that will be replaced when calling `set_extension()`: | |
| let mut filename = filename; | |
| filename.as_mut_os_string().push(".ext_placeholder"); | |
| let mut filename = filename; | |
| // Preserve any extension-like text within the provided file stem by appending a fake extension | |
| // which will be replaced by `set_extension()` calls (e.g. `file.local.placeholder` => `file.local.json`) | |
| if filename.extension().is_some() { | |
| filename.as_mut_os_string().push(".placeholder"); | |
| } |
I'll commit and squash this, but I don't mind ammending the change again if you're not happy with it 👍
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.
I think I prefer the original comment