Skip to content

Commit

Permalink
bpaf_derive: cleanup and change MSRV to 1.34.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pacak committed Oct 6, 2022
1 parent c62abd9 commit c8be320
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bpaf_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "bpaf_derive"
version = "0.3.0"
edition = "2021"
edition = "2018"
categories = ["command-line-interface"]
description = "Derive macros for bpaf Command Line Argument Parser"
keywords = ["args", "arguments", "cli", "parser", "parse"]
Expand Down
6 changes: 3 additions & 3 deletions bpaf_derive/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ enum PostprAttr {
}

impl PostprAttr {
const fn can_derive(&self) -> bool {
fn can_derive(&self) -> bool {
match self {
PostprAttr::Many(..)
| PostprAttr::Map(..)
Expand Down Expand Up @@ -102,8 +102,8 @@ impl Parse for Doc {
fn parse(input: parse::ParseStream) -> Result<Self> {
input.parse::<Token![=]>()?;
let mut s = input.parse::<LitStr>()?.value();
if let Some(rest) = s.strip_prefix(' ') {
s = rest.to_string();
if s.starts_with(' ') {
s = s[1..].to_string();
}
Ok(Doc(s))
}
Expand Down
8 changes: 7 additions & 1 deletion bpaf_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#![doc = include_str!("../README.md")]
//! # Derive macro for bpaf command line parser
//!
//! For documentation refer to `bpaf` library docs: <https://docs.rs/bpaf/latest/bpaf/>
#![allow(clippy::manual_range_contains)]
#![allow(clippy::single_match_else)]
use quote::ToTokens;
use syn::parse_macro_input;
extern crate proc_macro;

mod field;
#[cfg(test)]
Expand Down
11 changes: 7 additions & 4 deletions bpaf_derive/src/top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,6 @@ impl Outer {
if let Some(OuterKind::Command(cmd)) = &mut res.kind {
cmd.shorts.append(&mut res.shorts);
cmd.longs.append(&mut res.longs);
} else if !(res.shorts.is_empty() && res.longs.is_empty()) {
todo!()
}

res.decor = Decor::new(&help, res.version.take());
Expand Down Expand Up @@ -446,7 +444,12 @@ impl Top {
}

let inner = match branches.len() {
0 => todo!(),
0 => {
return Err(syn::Error::new(
outer_ty.span(),
"Can't construct a parser from empty enum",
))
}
1 => branches.remove(0),
_ => BParser::Fold(branches, outer.fallback.clone()),
};
Expand Down Expand Up @@ -634,7 +637,7 @@ impl Fields {
}
}

const fn struct_definition_followed_by_semi(&self) -> bool {
fn struct_definition_followed_by_semi(&self) -> bool {
match self {
Fields::Named(_) | Fields::NoFields => false,
Fields::Unnamed(_) => true,
Expand Down
35 changes: 35 additions & 0 deletions bpaf_derive/src/top_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,41 @@ fn struct_command() {
assert_eq!(input.to_token_stream().to_string(), expected.to_string());
}

#[test]
fn struct_command_short() {
let input: Top = parse_quote! {
/// those are options
#[bpaf(command, short('x'))]
struct O{}
};

let expected = quote! {
fn o() -> impl ::bpaf::Parser<O> {
#[allow (unused_imports)]
use ::bpaf::Parser;
{
let inner_cmd =
{ ::bpaf::construct!(O{}) }
.to_options()
.descr("those are options")
;
::bpaf::command("o", inner_cmd)
.help("those are options")
.short('x')
}
}
};
assert_eq!(input.to_token_stream().to_string(), expected.to_string());
}

#[should_panic(expected = "Can't construct a parser from empty enum")]
#[test]
fn empty_enum() {
let _: Top = parse_quote! {
enum Opt { }
};
}

#[test]
fn enum_command() {
let input: Top = parse_quote! {
Expand Down
2 changes: 1 addition & 1 deletion bpaf_derive/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn to_kebab_case(input: &str) -> String {
pub fn to_custom_case(input: &str, sep: char) -> String {
let mut res = String::with_capacity(input.len() * 2);
for c in input.chars() {
if ('A'..='Z').contains(&c) {
if c >= 'A' && c <= 'Z' {
if !res.is_empty() {
res.push(sep);
}
Expand Down

0 comments on commit c8be320

Please sign in to comment.