Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tiny cli] Handle different struct visibilities #28

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tiny-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tiny-cli"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
license = "MPL-2.0"
readme = "../Readme.md"
Expand Down
5 changes: 5 additions & 0 deletions tiny-cli/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

## [v0.3.1] - 2024-08-26

### Fixed
- Correctly handles different visibilities on Arg structs

## [v0.3.0] - 2024-08-24

### Fixed
Expand Down
30 changes: 29 additions & 1 deletion tiny-cli/src/derive_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,36 @@ fn parse_annotation_group(g: &Group) -> GroupParseResult {
}
}

#[allow(clippy::too_many_lines)]
fn parse_member<I: Iterator<Item = TokenTree>>(ident: &Ident, it: &mut I) -> ParsedMember {
let field_name = ident.to_string();
let mut field_name = ident.to_string();
if field_name == "pub" {
match it.next() {
None => {
panic!("Expected visibility to be followed by an ident found nothing");
}
Some(tt) => match tt {
TokenTree::Group(g) => {
let count = g.stream().into_iter().count();
assert_eq!(
1, count,
"Expected a single identifier after 'pub(', found {g}"
);
field_name =
pop_ident(it, "Expected to find an ident after visibility").to_string();
}
TokenTree::Ident(id) => {
field_name = id.to_string();
}
TokenTree::Punct(p) => {
panic!("Found punctuation '{p}' after visibility specifier, expected an ident");
}
TokenTree::Literal(l) => {
panic!("Found literal {l} after visibility specifier, expected an ident");
}
},
}
}
pop_expect_punct(it, ':', "Failed to parse member, expected ':' punctuation");
let next = it
.next()
Expand Down
4 changes: 2 additions & 2 deletions tiny-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn pop_expect_punct<I: Iterator<Item = TokenTree>, D: Display>(
assert_eq!(p.as_char(), expect, "{err_msg}");
} else {
panic!(
"[ArgParse derive] Expected punctation with {expect}, found: {punct:?}, ctx: {err_msg}"
"[ArgParse derive] Expected punctuation with {expect}, found: {punct:?}, ctx: {err_msg}"
);
}
}
Expand All @@ -54,7 +54,7 @@ fn pop_ident<I: Iterator<Item = TokenTree>, D: Display>(stream: &mut I, err_msg:
if let TokenTree::Ident(ident) = ident {
ident
} else {
panic!("[ArgParse derive] Expected ident, found {ident:?}, ctx: {err_msg}");
panic!("[ArgParse derive] Expected ident, found '{ident:?}', ctx: {err_msg}");
}
}

Expand Down
4 changes: 2 additions & 2 deletions tiny-cli/tests/derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ Required argument 'pos_two' not supplied.",
#[derive(ArgParse)]
#[cli(help_path = "tiny-cli")]
pub struct MultiArgOptLast {
pos_one: String,
pos_two: i64,
pub pos_one: String,
pub(crate) pos_two: i64,
pos_three: Option<usize>,
}

Expand Down
Loading