|
515 | 515 | <a href="#515" id="515">515</a>
|
516 | 516 | <a href="#516" id="516">516</a>
|
517 | 517 | <a href="#517" id="517">517</a>
|
| 518 | +<a href="#518" id="518">518</a> |
| 519 | +<a href="#519" id="519">519</a> |
| 520 | +<a href="#520" id="520">520</a> |
| 521 | +<a href="#521" id="521">521</a> |
| 522 | +<a href="#522" id="522">522</a> |
518 | 523 | </pre><pre class="rust"><code><span class="comment">// * This file is part of the uutils coreutils package.
|
519 | 524 | // *
|
520 | 525 | // * For the full copyright and license information, please view the LICENSE
|
521 | 526 | // * file that was distributed with this source code.
|
522 | 527 |
|
523 |
| -// spell-checker:ignore (ToDO) kqueue Signum |
| 528 | +// spell-checker:ignore (ToDO) kqueue Signum fundu |
524 | 529 |
|
525 | 530 | </span><span class="kw">use </span><span class="kw">crate</span>::paths::Input;
|
526 | 531 | <span class="kw">use crate</span>::{parse, platform, Quotable};
|
527 | 532 | <span class="kw">use </span>atty::Stream;
|
528 | 533 | <span class="kw">use </span>clap::crate_version;
|
529 | 534 | <span class="kw">use </span>clap::{parser::ValueSource, Arg, ArgAction, ArgMatches, Command};
|
| 535 | +<span class="kw">use </span>fundu::DurationParser; |
530 | 536 | <span class="kw">use </span>same_file::Handle;
|
531 | 537 | <span class="kw">use </span>std::collections::VecDeque;
|
532 | 538 | <span class="kw">use </span>std::ffi::OsString;
|
|
665 | 671 | settings.retry =
|
666 | 672 | matches.get_flag(options::RETRY) || matches.get_flag(options::FOLLOW_RETRY);
|
667 | 673 |
|
668 |
| - <span class="kw">if let </span><span class="prelude-val">Some</span>(s) = matches.get_one::<String>(options::SLEEP_INT) { |
669 |
| - settings.sleep_sec = <span class="kw">match </span>s.parse::<f32>() { |
670 |
| - <span class="prelude-val">Ok</span>(s) => Duration::from_secs_f32(s), |
671 |
| - <span class="prelude-val">Err</span>(<span class="kw">_</span>) => { |
672 |
| - <span class="kw">return </span><span class="prelude-val">Err</span>(UUsageError::new( |
673 |
| - <span class="number">1</span>, |
674 |
| - <span class="macro">format!</span>(<span class="string">"invalid number of seconds: {}"</span>, s.quote()), |
675 |
| - )) |
676 |
| - } |
677 |
| - } |
| 674 | + <span class="kw">if let </span><span class="prelude-val">Some</span>(source) = matches.get_one::<String>(options::SLEEP_INT) { |
| 675 | + <span class="comment">// Advantage of `fundu` over `Duration::(try_)from_secs_f64(source.parse().unwrap())`: |
| 676 | + // * doesn't panic on errors like `Duration::from_secs_f64` would. |
| 677 | + // * no precision loss, rounding errors or other floating point problems. |
| 678 | + // * evaluates to `Duration::MAX` if the parsed number would have exceeded |
| 679 | + // `DURATION::MAX` or `infinity` was given |
| 680 | + // * not applied here but it supports customizable time units and provides better error |
| 681 | + // messages |
| 682 | + </span>settings.sleep_sec = |
| 683 | + DurationParser::without_time_units() |
| 684 | + .parse(source) |
| 685 | + .map_err(|<span class="kw">_</span>| { |
| 686 | + UUsageError::new(<span class="number">1</span>, <span class="macro">format!</span>(<span class="string">"invalid number of seconds: '{source}'"</span>)) |
| 687 | + })<span class="question-mark">?</span>; |
678 | 688 | }
|
679 | 689 |
|
680 | 690 | settings.use_polling = matches.get_flag(options::USE_POLLING);
|
|
0 commit comments