rust: Prompting the consumer for enter with rprompt and loop

Given the prospective document_title we now
wish to ask the consumer if the name continues to be
what they wish to use in order that we will be able to write
the record to disk. This may increasingly contain two
paths relying on if we now have an current
possible possibility or now not.

We will be able to want to use ask_for_filename in
confirm_filename, so we’re going to write that department
first.

let filename = fit document_title { Some(raw_title) => { // confirm_filename()? todo!() } None => ask_for_filename(),
};

rprompt permits us to invite for a small quantity
of enter from the consumer (in comparison to the a lot
greater enter conceivable through passing regulate to
the consumer’s editor).

We will use rprompt::prompt_reply_stderr to
get a reaction from the consumer, and .wrap_err
so as to add context to the mistake if anything else is going
improper.

fn ask_for_filename() -> End result<String> { rprompt::prompt_reply_stderr( "
Input filename
> ", ) .wrap_err("Did not get filename")
}

We will use the similar habits for the primary
a part of confirm_filename. We will additionally
want to use our first lifetime to account
for the shared reference argument.

fit can fit in opposition to a couple of values
for a similar department, so we’re going to take merit
of that to deal with branches for Ns and Ys, as
neatly because the default case. If anything else is going
improper, comparable to any individual inputting an “a”,
we’re going to fall thru the use of loop and ask once more
till we get a usable resolution

fn confirm_filename(raw_title: &str) -> End result<String> { loop { // urged defaults to uppercase personality in query // this can be a conference, now not a demand enforced through // the code let outcome = rprompt::prompt_reply_stderr(&layout!( "
present name: `{}`
Do you wish to have a special name? (y/N): ", raw_title, )) .wrap_err("Did not get enter for y/n query")?; fit outcome.as_str() { "y" | "Y" => destroy ask_for_filename(), "n" | "N" | "" => { // the capital N within the urged approach "default", // so we deal with "" as enter right here destroy Adequate(slug::slugify(raw_title)); } _ => { // ask once more as a result of one thing went improper } }; }
}

Whilst filenames can technically have areas
in them, we are going to slugify our filenames
like urls for a couple of causes. One is that
when beginning out, many programmers fail to
quote filename arguments of their bash scripts,
which ends up in filenames with areas being
handled as separate arguments. Some other is
that this can be a virtual lawn CLI, and
virtual gardens are regularly saved in git,
accessed from a couple of record methods, as neatly
as from URLs. Whilst strictly talking we do not
want to slugify our filenames, we can right here
as a way to adhere to a more secure set of characters.

shipment upload slug

We will map over the End result from rprompt
which permits us to perform at the inside
worth if it is Adequate.

fn ask_for_filename() -> End result<String> name

We will additionally use slugify in confirm_filename

fit outcome.as_str() { "y" | "Y" => destroy ask_for_filename(), "n" | "N" | "" => { // the capital N within the urged approach "default", // so we deal with "" as enter right here destroy Adequate(slug::slugify(raw_title)); } _ => { // ask once more as a result of one thing went improper }
};

The truth that confirm_filename and
ask_for_filename have the similar go back kind
is necessary for the reason that branches of a fit
want to go back the similar kind.