Skip to content

Commit

Permalink
chore: s/not/non and s/R/P
Browse files Browse the repository at this point in the history
  • Loading branch information
ozwaldorf committed Jul 21, 2024
1 parent 338cf8f commit 4868361
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
10 changes: 5 additions & 5 deletions bpaf_derive/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl ToTokens for PostParse {
PostParse::Optional { .. } => quote!(optional()),
PostParse::Parse { f, .. } => quote!(parse(#f)),
PostParse::Strict { .. } => quote!(strict()),
PostParse::NotStrict { .. } => quote!(not_strict()),
PostParse::NonStrict { .. } => quote!(non_strict()),
PostParse::Anywhere { .. } => quote!(anywhere()),
}
.to_tokens(tokens);
Expand Down Expand Up @@ -257,7 +257,7 @@ pub(crate) enum PostParse {
Optional { span: Span },
Parse { span: Span, f: Box<Expr> },
Strict { span: Span },
NotStrict { span: Span },
NonStrict { span: Span },
Anywhere { span: Span },
}
impl PostParse {
Expand All @@ -273,7 +273,7 @@ impl PostParse {
| Self::Optional { span }
| Self::Parse { span, .. }
| Self::Strict { span }
| Self::NotStrict { span }
| Self::NonStrict { span }
| Self::Anywhere { span } => *span,
}
}
Expand Down Expand Up @@ -483,8 +483,8 @@ impl PostParse {
Self::Parse { span, f }
} else if kw == "strict" {
Self::Strict { span }
} else if kw == "not_strict" {
Self::NotStrict { span }
} else if kw == "non_strict" {
Self::NonStrict { span }
} else if kw == "some" {
let msg = parse_arg(input)?;
Self::Some_ { span, msg }
Expand Down
6 changes: 3 additions & 3 deletions bpaf_derive/src/field_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,13 @@ fn strict_positional_named_fields() {
}

#[test]
fn not_strict_positional_named_fields() {
fn non_strict_positional_named_fields() {
let input: NamedField = parse_quote! {
#[bpaf(positional("ARG"), not_strict)]
#[bpaf(positional("ARG"), non_strict)]
name: String
};
let output = quote! {
::bpaf::positional::<String>("ARG").not_strict()
::bpaf::positional::<String>("ARG").non_strict()
};
assert_eq!(input.to_token_stream().to_string(), output.to_string());
}
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub(crate) enum Message {
StrictPos(usize, Metavar),

/// Tried to consume a non-strict positional argument, but the value was strict
NotStrictPos(usize, Metavar),
NonStrictPos(usize, Metavar),

/// Parser provided by user failed to parse a value
ParseFailed(Option<usize>, String),
Expand Down Expand Up @@ -92,7 +92,7 @@ impl Message {
| Message::ParseFail(_)
| Message::Missing(_)
| Message::PureFailed(_)
| Message::NotStrictPos(_, _) => true,
| Message::NonStrictPos(_, _) => true,
Message::StrictPos(_, _)
| Message::ParseFailed(_, _)
| Message::GuardFailed(_, _)
Expand Down Expand Up @@ -331,7 +331,7 @@ impl Message {
}

// Error: FOO expected to be on the left side of --
Message::NotStrictPos(_ix, metavar) => {
Message::NonStrictPos(_ix, metavar) => {
doc.text("expected ");
doc.token(Token::BlockStart(Block::TermRef));
doc.metavar(metavar);
Expand Down
31 changes: 17 additions & 14 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,15 +716,15 @@ pub(crate) fn build_positional<T>(metavar: &'static str) -> ParsePositional<T> {
/// Parse a positional item, created with [`positional`](crate::positional)
///
/// You can add extra information to positional parsers with [`help`](Self::help),
/// [`strict`](Self::strict), or [`not_strict`](Self::not_strict) on this struct.
/// [`strict`](Self::strict), or [`non_strict`](Self::non_strict) on this struct.
#[derive(Clone)]
pub struct ParsePositional<T, R: PositionMarker = Unrestricted> {
pub struct ParsePositional<T, P: PositionMarker = Unrestricted> {
metavar: &'static str,
help: Option<Doc>,
ty: PhantomData<(T, R)>,
ty: PhantomData<(T, P)>,
}

impl<T, R: PositionMarker> ParsePositional<T, R> {
impl<T, P: PositionMarker> ParsePositional<T, P> {
/// Add a help message to a [`positional`] parser
///
/// `bpaf` converts doc comments and string into help by following those rules:
Expand All @@ -747,7 +747,7 @@ impl<T, R: PositionMarker> ParsePositional<T, R> {
}

fn meta(&self) -> Meta {
R::map_meta(Meta::from(Item::Positional {
P::map_meta(Meta::from(Item::Positional {
metavar: Metavar(self.metavar),
help: self.help.clone(),
}))
Expand Down Expand Up @@ -776,6 +776,7 @@ impl<T> ParsePositional<T> {
/// `bpaf` would display such positional elements differently in usage line as well.
#[cfg_attr(not(doctest), doc = include_str!("docs2/positional_strict.md"))]
#[must_use]
#[inline(always)]
pub fn strict(self) -> ParsePositional<T, Strict> {
ParsePositional {
metavar: self.metavar,
Expand All @@ -790,7 +791,8 @@ impl<T> ParsePositional<T> {
/// Essentially the inverse operation to [`ParsePositional::strict`]. Can be used next to strict
/// positional arg(s) to have a clear separation and parsing between contexts.
#[must_use]
pub fn not_strict(self) -> ParsePositional<T, NotStrict> {
#[inline(always)]
pub fn non_strict(self) -> ParsePositional<T, NonStrict> {
ParsePositional {
metavar: self.metavar,
help: self.help,
Expand Down Expand Up @@ -818,11 +820,11 @@ pub struct Unrestricted;
impl PositionMarker for Unrestricted {}

/// Non-strict positional marker type
pub struct NotStrict;
impl PositionMarker for NotStrict {
pub struct NonStrict;
impl PositionMarker for NonStrict {
#[inline(always)]
fn check(metavar: Metavar, ix: usize, is_strict: bool) -> Option<Error> {
is_strict.then(|| Error(Message::NotStrictPos(ix, metavar)))
is_strict.then(|| Error(Message::NonStrictPos(ix, metavar)))
}
}

Expand All @@ -841,14 +843,14 @@ impl PositionMarker for Strict {
}
}

fn parse_pos_word<R: PositionMarker>(
fn parse_pos_word<P: PositionMarker>(
args: &mut State,
metavar: Metavar,
help: &Option<Doc>,
) -> Result<OsString, Error> {
match args.take_positional_word(metavar) {
Ok((ix, is_strict, word)) => {
if let Some(err) = R::check(metavar, ix, is_strict) {
if let Some(err) = P::check(metavar, ix, is_strict) {
#[cfg(feature = "autocomplete")]
args.push_pos_sep();
return Err(err);
Expand All @@ -872,20 +874,21 @@ fn parse_pos_word<R: PositionMarker>(
}
}

impl<T, R> Parser<T> for ParsePositional<T, R>
impl<T, P> Parser<T> for ParsePositional<T, P>
where
T: FromStr + 'static,
<T as std::str::FromStr>::Err: std::fmt::Display,
R: PositionMarker,
P: PositionMarker,
{
fn eval(&self, args: &mut State) -> Result<T, Error> {
let os = parse_pos_word::<R>(args, Metavar(self.metavar), &self.help)?;
let os = parse_pos_word::<P>(args, Metavar(self.metavar), &self.help)?;
match parse_os_str::<T>(os) {
Ok(ok) => Ok(ok),
Err(err) => Err(Error(Message::ParseFailed(args.current, err))),
}
}

#[inline(always)]
fn meta(&self) -> Meta {
self.meta()
}
Expand Down
4 changes: 2 additions & 2 deletions tests/positionals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ fn strictly_positional() {
}

#[test]
fn not_strictly_positional() {
let parser = positional::<String>("A").not_strict().to_options();
fn non_strictly_positional() {
let parser = positional::<String>("A").non_strict().to_options();

let r = parser.run_inner(&["a"]).unwrap();
assert_eq!(r, "a");
Expand Down

0 comments on commit 4868361

Please sign in to comment.