-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Find syntax ignoring known backup/template filename suffixes #1687
Conversation
For example, fall back to `foo.extension` for `foo.extension~`, `foo.extension.orig`, `foo.extension.in.in` etc.
Thanks a lot for taking the time to make a contribution! I like this idea in general. Since you first try to find a supported syntax with an unmodified file extension, it should also be future proof in terms of support for new syntaxes. For example, right now we do not have any language that uses the Not sure when I will get the time for a detailed code review (maybe someone else will soon), but just wanted to say that on a high level it looks good to me. |
Yes, that's the intent here. For the same reason we strip the minimum amount of info on each round and try each result in turn -- one suffix at a time even if there would be many, and retaining rest of the full file path as we go. |
To make it easier to make further enhancements. See e.g. sharkdp#1687.
I have taken a more detailed look now, and noticed that you use recursion. It would be nice to get rid of the recursion. Just for brainstorming purposes, what do you think of basing a solution on this base sketch implementation of get_extension_syntax() instead?
A commit with this is found here: Enselic@311f561. This commit passes all CI checks. The idea is that instead of using recursion, you can add code that will simply add more items to It would also be great to add regression tests for this. You can find a bunch of tests in |
Unless I'm missing something, this approach couldn't be used to strip arbitrary combinations of ignored file suffixes, including arbitrary number of them. But it's certainly debatable whether that's something we want/"need" to support. Practical use cases I have in mind are I have a nagging feeling that if we're only working with extensions, But what about globs involving file paths, they're not dealing with extensions, and yet I think we'd want the ignored suffix behavior to work with them, too? Didn't check the code, just saying as I happened to think of it. I believe the recursive approach in my current revision doesn't have any of the above issues. Not that I'm at all insisting on covering everything it does, and eliminating recursion would be nice I guess, but it would be cool to get the above mentioned special cases working too.
I certainly can, but not sure exactly when; possibly sometime this week. I don't mind if you or someone else beats me to it though, let's drop a note here whoever starts working on it first to avoid duplicate work though. |
BTW adding "real" extensions or filenames for the ignored suffixes statically would make the |
Aha ok. I didn’t think of arbitrarily deep “backup extension on backup extension” scenarios. For that recursion makes more sense. It would actually be good to get tests in place before we iterate on the implementation. Because we also need to iterate on how this should behave from a user point of view. I’m thinking we don’t want to list backup extensions with |
I took a brief look how would it look like to add some tests, but But along those lines, I suppose we could add some files with suffixes to ignore in their names to the Any other ideas how to go about testing this? Did I miss something? |
I took a fresh look at Your proposal to base tests on Feel free to experiment to find a way that feels right. Also feel free to frequently iterate on your ideas here so that you don't spend a lot of effort on an approach that we end up not using. |
Added some tests using the |
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.
Thank you very much for your contribution! This looks great. I added two minor inline comments.
src/assets.rs
Outdated
if file_str.ends_with(suffix) { | ||
return self.get_extension_syntax( | ||
OsStr::new( | ||
file_str | ||
.strip_suffix(suffix) | ||
.unwrap_or_default() | ||
)); | ||
} |
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.
Instead of using .ends_with(suffix)
, .strip_suffix(suffix)
and finally .unwrap*
, I think you could simply call strip_suffix
and decide based on the result. Something like:
if let Some(stripped_filename) = file_str.strip_suffix(suffix) {
return self.get_extension_syntax(OsStr::new(stripped_filename));
}
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.
Good stuff, TIL, done.
src/assets.rs
Outdated
.extension() | ||
.and_then(|x| x.to_str()) | ||
.unwrap_or_default(), | ||
) | ||
).or_else(|| { | ||
let file_str = file_path.to_str().unwrap_or_default(); |
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 will lead to an empty string if file_path
contains invalid UTF-8 (which is possible). Instead of directly aborting here, we would still run the full for
loop below. I think the behavior would still be okay, but it feels a bit strange. And could possibly lead to errors in the future if the code below is changed. I think I would prefer something like:
if let Some(file_str) = file_path.to_str() {
// …
}
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.
SGTM this one too, done.
Thanks a bunch for the guidance and opportunity to learn a bit of Rust 👍
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.
Thank you!
Released in v0.18.2 |
For example, fall back to
foo.extension
forfoo.extension~
,foo.extension.orig
,foo.extension.in.in
etc.Rust newbie alert, likely clumsy/not idiomatic, but appears to work. Critique/guidance very much welcome, for example redundant/repeated conversion between
OsStr
,Path
, andstr
doesn't seem that clean to me.