refactor: Renamge ValuesRange to ValueRange

Naming is hard.  I found I was writing new code without the `s` so that
suggests the name was wrong.  But we renamed `number_of_values` to
`num_args`, so should this be `ArgRange`?
This commit is contained in:
Ed Page 2022-08-05 12:00:31 -05:00
parent 58b0529d13
commit c1468d78b1
3 changed files with 38 additions and 38 deletions

View file

@ -13,7 +13,7 @@ use std::{env, ffi::OsString};
use super::{ArgFlags, ArgSettings};
use crate::builder::ArgPredicate;
use crate::builder::PossibleValue;
use crate::builder::ValuesRange;
use crate::builder::ValueRange;
use crate::util::{Id, Key};
use crate::ArgAction;
use crate::ValueHint;
@ -71,7 +71,7 @@ pub struct Arg<'help> {
pub(crate) short_aliases: Vec<(char, bool)>, // (name, visible)
pub(crate) disp_ord: Option<usize>,
pub(crate) val_names: Vec<&'help str>,
pub(crate) num_vals: Option<ValuesRange>,
pub(crate) num_vals: Option<ValueRange>,
pub(crate) val_delim: Option<char>,
pub(crate) default_vals: Vec<&'help OsStr>,
pub(crate) default_vals_ifs: Vec<(Id, ArgPredicate<'help>, Option<&'help OsStr>)>,
@ -1021,7 +1021,7 @@ impl<'help> Arg<'help> {
/// ```
#[inline]
#[must_use]
pub fn num_args(mut self, qty: impl Into<ValuesRange>) -> Self {
pub fn num_args(mut self, qty: impl Into<ValueRange>) -> Self {
let qty = qty.into();
self.num_vals = Some(qty);
self.takes_value(qty.takes_values())
@ -3691,7 +3691,7 @@ impl<'help> Arg<'help> {
/// Get the number of values for this argument.
#[inline]
pub fn get_num_args(&self) -> Option<ValuesRange> {
pub fn get_num_args(&self) -> Option<ValueRange> {
self.num_vals
}

View file

@ -23,7 +23,7 @@ pub use arg::Arg;
pub use arg_group::ArgGroup;
pub use command::Command;
pub use possible_value::PossibleValue;
pub use range::ValuesRange;
pub use range::ValueRange;
pub use value_hint::ValueHint;
pub use value_parser::_AutoValueParser;
pub use value_parser::via_prelude;

View file

@ -1,11 +1,11 @@
/// Values per occurrence for an argument
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct ValuesRange {
pub struct ValueRange {
start_inclusive: usize,
end_inclusive: usize,
}
impl ValuesRange {
impl ValueRange {
/// Create a range
///
/// # Panics
@ -15,19 +15,19 @@ impl ValuesRange {
/// # Examples
///
/// ```
/// # use clap::builder::ValuesRange;
/// let range = ValuesRange::new(5);
/// let range = ValuesRange::new(5..10);
/// let range = ValuesRange::new(5..=10);
/// let range = ValuesRange::new(5..);
/// let range = ValuesRange::new(..10);
/// let range = ValuesRange::new(..=10);
/// # use clap::builder::ValueRange;
/// let range = ValueRange::new(5);
/// let range = ValueRange::new(5..10);
/// let range = ValueRange::new(5..=10);
/// let range = ValueRange::new(5..);
/// let range = ValueRange::new(..10);
/// let range = ValueRange::new(..=10);
/// ```
///
/// While this will panic:
/// ```should_panic
/// # use clap::builder::ValuesRange;
/// let range = ValuesRange::new(10..5); // Panics!
/// # use clap::builder::ValueRange;
/// let range = ValueRange::new(10..5); // Panics!
/// ```
pub fn new(range: impl Into<Self>) -> Self {
range.into()
@ -56,11 +56,11 @@ impl ValuesRange {
/// # Examples
///
/// ```
/// # use clap::builder::ValuesRange;
/// let range = ValuesRange::new(5);
/// # use clap::builder::ValueRange;
/// let range = ValueRange::new(5);
/// assert!(range.takes_values());
///
/// let range = ValuesRange::new(0);
/// let range = ValueRange::new(0);
/// assert!(!range.takes_values());
/// ```
pub fn takes_values(&self) -> bool {
@ -84,7 +84,7 @@ impl ValuesRange {
}
}
impl std::ops::RangeBounds<usize> for ValuesRange {
impl std::ops::RangeBounds<usize> for ValueRange {
fn start_bound(&self) -> std::ops::Bound<&usize> {
std::ops::Bound::Included(&self.start_inclusive)
}
@ -94,19 +94,19 @@ impl std::ops::RangeBounds<usize> for ValuesRange {
}
}
impl Default for ValuesRange {
impl Default for ValueRange {
fn default() -> Self {
0.into()
}
}
impl From<usize> for ValuesRange {
impl From<usize> for ValueRange {
fn from(fixed: usize) -> Self {
(fixed..=fixed).into()
}
}
impl From<std::ops::Range<usize>> for ValuesRange {
impl From<std::ops::Range<usize>> for ValueRange {
fn from(range: std::ops::Range<usize>) -> Self {
let start_inclusive = range.start;
let end_inclusive = range.end.saturating_sub(1);
@ -114,7 +114,7 @@ impl From<std::ops::Range<usize>> for ValuesRange {
}
}
impl From<std::ops::RangeFull> for ValuesRange {
impl From<std::ops::RangeFull> for ValueRange {
fn from(_: std::ops::RangeFull) -> Self {
let start_inclusive = 0;
let end_inclusive = usize::MAX;
@ -122,7 +122,7 @@ impl From<std::ops::RangeFull> for ValuesRange {
}
}
impl From<std::ops::RangeFrom<usize>> for ValuesRange {
impl From<std::ops::RangeFrom<usize>> for ValueRange {
fn from(range: std::ops::RangeFrom<usize>) -> Self {
let start_inclusive = range.start;
let end_inclusive = usize::MAX;
@ -130,7 +130,7 @@ impl From<std::ops::RangeFrom<usize>> for ValuesRange {
}
}
impl From<std::ops::RangeTo<usize>> for ValuesRange {
impl From<std::ops::RangeTo<usize>> for ValueRange {
fn from(range: std::ops::RangeTo<usize>) -> Self {
let start_inclusive = 0;
let end_inclusive = range.end.saturating_sub(1);
@ -138,7 +138,7 @@ impl From<std::ops::RangeTo<usize>> for ValuesRange {
}
}
impl From<std::ops::RangeInclusive<usize>> for ValuesRange {
impl From<std::ops::RangeInclusive<usize>> for ValueRange {
fn from(range: std::ops::RangeInclusive<usize>) -> Self {
let start_inclusive = *range.start();
let end_inclusive = *range.end();
@ -146,7 +146,7 @@ impl From<std::ops::RangeInclusive<usize>> for ValuesRange {
}
}
impl From<std::ops::RangeToInclusive<usize>> for ValuesRange {
impl From<std::ops::RangeToInclusive<usize>> for ValueRange {
fn from(range: std::ops::RangeToInclusive<usize>) -> Self {
let start_inclusive = 0;
let end_inclusive = range.end;
@ -154,7 +154,7 @@ impl From<std::ops::RangeToInclusive<usize>> for ValuesRange {
}
}
impl std::fmt::Display for ValuesRange {
impl std::fmt::Display for ValueRange {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.start_inclusive.fmt(f)?;
if !self.is_fixed() {
@ -165,7 +165,7 @@ impl std::fmt::Display for ValuesRange {
}
}
impl std::fmt::Debug for ValuesRange {
impl std::fmt::Debug for ValueRange {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self)
}
@ -179,7 +179,7 @@ mod test {
#[test]
fn from_fixed() {
let range: ValuesRange = 5.into();
let range: ValueRange = 5.into();
assert_eq!(range.start_bound(), std::ops::Bound::Included(&5));
assert_eq!(range.end_bound(), std::ops::Bound::Included(&5));
assert!(range.is_fixed());
@ -190,7 +190,7 @@ mod test {
#[test]
fn from_fixed_empty() {
let range: ValuesRange = 0.into();
let range: ValueRange = 0.into();
assert_eq!(range.start_bound(), std::ops::Bound::Included(&0));
assert_eq!(range.end_bound(), std::ops::Bound::Included(&0));
assert!(range.is_fixed());
@ -201,7 +201,7 @@ mod test {
#[test]
fn from_range() {
let range: ValuesRange = (5..10).into();
let range: ValueRange = (5..10).into();
assert_eq!(range.start_bound(), std::ops::Bound::Included(&5));
assert_eq!(range.end_bound(), std::ops::Bound::Included(&9));
assert!(!range.is_fixed());
@ -212,7 +212,7 @@ mod test {
#[test]
fn from_range_inclusive() {
let range: ValuesRange = (5..=10).into();
let range: ValueRange = (5..=10).into();
assert_eq!(range.start_bound(), std::ops::Bound::Included(&5));
assert_eq!(range.end_bound(), std::ops::Bound::Included(&10));
assert!(!range.is_fixed());
@ -223,7 +223,7 @@ mod test {
#[test]
fn from_range_full() {
let range: ValuesRange = (..).into();
let range: ValueRange = (..).into();
assert_eq!(range.start_bound(), std::ops::Bound::Included(&0));
assert_eq!(range.end_bound(), std::ops::Bound::Included(&usize::MAX));
assert!(!range.is_fixed());
@ -234,7 +234,7 @@ mod test {
#[test]
fn from_range_from() {
let range: ValuesRange = (5..).into();
let range: ValueRange = (5..).into();
assert_eq!(range.start_bound(), std::ops::Bound::Included(&5));
assert_eq!(range.end_bound(), std::ops::Bound::Included(&usize::MAX));
assert!(!range.is_fixed());
@ -245,7 +245,7 @@ mod test {
#[test]
fn from_range_to() {
let range: ValuesRange = (..10).into();
let range: ValueRange = (..10).into();
assert_eq!(range.start_bound(), std::ops::Bound::Included(&0));
assert_eq!(range.end_bound(), std::ops::Bound::Included(&9));
assert!(!range.is_fixed());
@ -256,7 +256,7 @@ mod test {
#[test]
fn from_range_to_inclusive() {
let range: ValuesRange = (..=10).into();
let range: ValueRange = (..=10).into();
assert_eq!(range.start_bound(), std::ops::Bound::Included(&0));
assert_eq!(range.end_bound(), std::ops::Bound::Included(&10));
assert!(!range.is_fixed());