My impression is that it’s not that formal of a process unless the dependency is really big or un-needed (somebody correct me if I’m wrong). Probably easiest to make your code do what you want first. Then you can always see about getting feedback, pairing down dependencies, and making things smaller/faster.
On a related note, I don’t know what you need to do with it other than walk the directory, but you can always implement this yourself with std::fs::read_dir. In the docs there’s a straight-forward example that’s like ~10 lines that shows how to recursively walk a directory. Might be easier than worrying about dependencies/breakage in the future .
I don’t see anything wrong with this implementation from an API perspective, so seems fine to me for now. But I would say, in terms of future usage I agree with @joshuef that one is still a specialization of the other. It still makes sense to me to include a generic driver function, because they do duplicate an awful lot of code, even though they’re different.
I don’t want to tell you what to do, but I tend to think in terms of code so hopefully this expresses what I’m getting at. Here’s an example function signature idea that could drive both.
fn prompt_binary<T: AsRef<str>>(
prompt_msg: &str,
affirmative_strs: Iterator<Item=T>,
negative_strs: Iterator<Item=T>,
) -> Option<bool> {
//...
// call prompt_user() and do some pattern matching here
//...
}
Now you use ""
as the “default” by just adding it into affirmative_strs
or negative_strs
. Then it returns Some(true)
if the input was in the affirmative_strs
, Some(false)
if the input was in the negative_strs
and None
if it was unkown.
Thusly, prompt_yes_no
just becomes the following. You can also add stuff like default_yes
as an argument and tweak it in generaly a lot easier than before.
fn prompt_yes_no(msg: &str) -> bool {
let yes_strs = ["yes", "y", ""].iter();
let no_strs = ["no", "n"].iter();
while let maybe_resp = prompt_binary(msg, yes_strs, no_strs) {
match maybe_resp {
Some(resp) => return resp;
None => println!("Unrecognized input, please try again.");
}
}
}
And prompt_confirmation
is just
fn prompt_confirmation(msg: &str) -> bool {
prompt_binary(
msg,
["confirm"].iter(),
std::iter::empty()
)
.unwrap_or(false)
}