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

chore: reconfigure project #31

Merged
merged 2 commits into from
Apr 18, 2023
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
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = tab
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = false
4 changes: 4 additions & 0 deletions .githooks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*

!.git*
!pre-commit
12 changes: 12 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

for FILE in `git diff --cached --name-only`; do
if [ -f $FILE ]; then
case $FILE in
*.rs)
rustfmt --unstable-features --skip-children $FILE ;;
esac
perl -pi -e 'chomp if eof' $FILE
git add $FILE
fi
done
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ Cargo.lock
.*

!.git*
!.vscode
!.vscode
!.editorconfig
14 changes: 9 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"editor.formatOnSave": true,
"files.removeFinalNewlines": true,
"rust-analyzer.diagnostics.disabled": [
"macro-error"
],
"editor.formatOnSave": true,
"files.removeFinalNewlines": true,
"rust-analyzer.imports.granularity.enforce": true,
"rust-analyzer.imports.granularity.group": "module",
"rust-analyzer.imports.group.enable": false,
"rust-analyzer.imports.prefix": "crate",
"trailing-spaces.includeEmptyLines": false,
"editor.renderWhitespace": "all",
"git.ignoreLimitWarning": true
}
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ repository = "https://github.com/yuhr/fncmd"
categories = ["command-line-interface"]
keywords = ["cli", "macro", "clap"]
edition = "2021"
exclude = [".*", "rust-toolchain", "rustfmt.toml", "release.toml", "justfile"]
exclude = [".*", "rust-toolchain", "*.toml", "justfile"]

[dependencies]
clap = { version = "3.0.7", features = ["derive"] }
fncmd-impl = { path = "impl", version = "=1.2.5" }
once_cell = "1.9.0"

[workspace]
members = ["impl"]

[dev-dependencies]
git_hooks = "0.1.3"
members = ["impl"]
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,12 @@ This concept is tremendously inspired by [`argopt`](https://crates.io/crates/arg

**This crate is nightly-only**. Make sure you have set up your toolchain as nightly before using (e.g. having [`rust-toolchain`](https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file) file). You might be interested in [Why nightly](#why-nightly).

To install, if you use [`cargo-edit`](https://crates.io/crates/cargo-edit):
To install, run:

```sh
cargo add fncmd
```

Or you can manually edit `Cargo.toml`:

```toml
[dependencies]
fncmd = "1.2.5"
```

## Basics

This crate exposes just a single attribute macro, [`fncmd`], which can **only** be attached to the `main` function:
Expand Down Expand Up @@ -177,7 +170,7 @@ pub async fn main(hello: String) -> anyhow::Result<()> {
}
```

This is because ~~Rust requires procedural macros to produce legal code *for each* macroexpansion~~Sorry this is wrong, it's not Rust but the macros like `#[tokio::main]` *do* some assertions on their own, so we need to feed them a well-mannered version of `main` function, e.g. removing parameters.
This is because the macros like `#[tokio::main]` do some assertions on their own, so we need to feed them a well-mannered version of `main` function, e.g. removing parameters.

Position of the doc comment doesn't matter.

Expand Down
3 changes: 3 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module_inception = false
collapsible_else_if = false
write_with_newline = false
6 changes: 0 additions & 6 deletions hooks/pre-commit

This file was deleted.

6 changes: 1 addition & 5 deletions impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ pub fn fncmd(attr: TokenStream, item: TokenStream) -> TokenStream {
.targets
.iter()
.filter_map(|target| {
target
.kind
.iter()
.any(|k| k == "bin" || k == "example")
.then(|| target)
target.kind.iter().any(|k| k == "bin" || k == "example").then(|| target)
})
.collect::<Vec<_>>();
let self_version = package.version.to_string();
Expand Down
4 changes: 1 addition & 3 deletions impl/src/models/fncmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ impl Fncmd {
}

impl From<Fncmd> for proc_macro::TokenStream {
fn from(from: Fncmd) -> proc_macro::TokenStream {
quote!(#from).into()
}
fn from(from: Fncmd) -> proc_macro::TokenStream { quote!(#from).into() }
}

impl ToTokens for Fncmd {
Expand Down
20 changes: 3 additions & 17 deletions impl/src/models/fncmd_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ impl FncmdArg {
let mut attrs = Vec::new();

for attr in arg.attrs.iter() {
match attr
.path
.get_ident()
.map(|ident| ident.to_string())
.as_deref()
{
match attr.path.get_ident().map(|ident| ident.to_string()).as_deref() {
Some("doc") => {
doc = Some(quote! { #attr });
}
Expand Down Expand Up @@ -73,23 +68,14 @@ impl FncmdArg {
mutability: pat_ident.mutability,
}
} else {
abort!(
pat.span(),
"pattern types other than a simple `Ident` are not supported yet"
);
abort!(pat.span(), "pattern types other than a simple `Ident` are not supported yet");
}
}
}

impl ToTokens for FncmdArg {
fn to_tokens(&self, tokens: &mut TokenStream) {
let Self {
name,
documentation,
attributes,
ty,
..
} = self;
let Self { name, documentation, attributes, ty, .. } = self;
quote! {
#documentation
#(#attributes)*
Expand Down
15 changes: 4 additions & 11 deletions impl/src/models/fncmd_subcmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,20 @@ impl FncmdSubcmds {

// Remove all subcommands that are prefixed with any other target
let table = self.0.clone();
self.0
.retain(|name, _| !table.iter().any(|(other, _)| is_subcommand(name, other)));
self.0.retain(|name, _| !table.iter().any(|(other, _)| is_subcommand(name, other)));

// Remove all non-`pub` targets
self.0.retain(|_, (is_pub, _)| *is_pub);

self
}

pub fn iter(&self) -> impl Iterator<Item = (&String, &(bool, PathBuf))> {
self.0.iter()
}
pub fn iter(&self) -> impl Iterator<Item = (&String, &(bool, PathBuf))> { self.0.iter() }
}

/// Check if `it` is a subcommand of `of`.
fn is_subcommand(it: &str, of: &str) -> bool {
it.len() > of.len() && it.starts_with(of)
}
fn is_subcommand(it: &str, of: &str) -> bool { it.len() > of.len() && it.starts_with(of) }

impl From<HashMap<String, (bool, PathBuf)>> for FncmdSubcmds {
fn from(from: HashMap<String, (bool, PathBuf)>) -> Self {
FncmdSubcmds(from)
}
fn from(from: HashMap<String, (bool, PathBuf)>) -> Self { FncmdSubcmds(from) }
}
6 changes: 1 addition & 5 deletions impl/src/models/fncmd_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ pub struct FncmdVisitor<'ast> {
}

impl FncmdVisitor<'_> {
pub fn new() -> Self {
Self {
functions: Vec::new(),
}
}
pub fn new() -> Self { Self { functions: Vec::new() } }
}

impl<'ast> Visit<'ast> for FncmdVisitor<'ast> {
Expand Down
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@_:
just --list

setup:
chmod +x .githooks/*
git config --local core.hooksPath .githooks

bump *ARGS:
cargo release --workspace --sign-commit --no-tag --no-push --no-publish {{ARGS}}

Expand Down
1 change: 0 additions & 1 deletion release.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pre-release-replacements = [
{ file = "README.md", search = "fncmd = \".+\"", replace = "{{crate_name}} = \"{{version}}\"" },
{ file = "Cargo.toml", search = "fncmd-impl = \\{ path = \"impl\", version = \"=.+\"\\}", replace = "fncmd-impl = { path = \"impl\", version = \"={{version}}\" }" },
]
pre-release-commit-message = "chore: bump version to {{version}}"
Expand Down
12 changes: 11 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
brace_style = "PreferSameLine"
control_brace_style = "AlwaysSameLine"
fn_single_line = true
hard_tabs = true
newline_style = "Unix"
match_arm_blocks = false
max_width = 100
newline_style = "Unix"
overflow_delimited_expr = true
trim_final_newlines = "Always"
use_field_init_shorthand = true
use_small_heuristics = "Max"
where_single_line = true
8 changes: 2 additions & 6 deletions src/exit_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ pub trait IntoExitCode {
}

impl<T: std::process::Termination> IntoExitCode for T {
fn into_exit_code(self) -> ExitCode {
ExitCode(self.report())
}
fn into_exit_code(self) -> ExitCode { ExitCode(self.report()) }
}

impl std::process::Termination for ExitCode {
fn report(self) -> std::process::ExitCode {
self.0
}
fn report(self) -> std::process::ExitCode { self.0 }
}
2 changes: 2 additions & 0 deletions taplo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[formatting]
trailing_newline = false