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

refactor(repo): replace build system, CI, and fix lints #2

Merged
merged 19 commits into from
Oct 27, 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: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.aarch64-apple-darwin]
linker = "rust-lld"
10 changes: 0 additions & 10 deletions .editorconfig

This file was deleted.

6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: "daily"
83 changes: 83 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: CI

on:
push:
branches:
- main
pull_request:

env:
CARGO_TERM_COLOR: always
RUSTFLAGS: -Dwarnings

jobs:
build_and_test:
name: Build & Test
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest] # macos-latest
toolchain: [stable, beta, nightly]
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- uses: extractions/setup-just@v2
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.toolchain }}

- run: just test

check_wasm_build:
name: Check WASM Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- uses: extractions/setup-just@v2
- uses: dtolnay/rust-toolchain@stable
with:
target: wasm32-unknown-unknown

- run: just check-wasm

format_and_lint:
name: Format & Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- uses: extractions/setup-just@v2
- uses: dtolnay/rust-toolchain@stable

- run: just lint

check_min_versions:
name: Check Minimal Versions
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- uses: extractions/setup-just@v2
- uses: dtolnay/rust-toolchain@nightly

- run: just min-versions

coverage:
name: Test Coverage
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- uses: extractions/setup-just@v2
- uses: taiki-e/install-action@cargo-llvm-cov
- uses: dtolnay/rust-toolchain@nightly
with:
components: llvm-tools-preview

- run: just ci-cov

- uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: codecov.json
36 changes: 0 additions & 36 deletions .github/workflows/tests.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ reference on allowed attributes and parse modes.

This crate publishes nice errors, like this:

<img width="50%" src="https://raw.githubusercontent.com/tailhook/knuffel/main/images/error.png" alt="
<img width="50%" src="https://raw.githubusercontent.com/TheLostLambda/knus/main/images/error.png" alt="
Screenshot of error. Here is how narratable printer would print the error:
Error: single char expected after `Alt+`
Diagnostic severity: error
Expand Down
2 changes: 1 addition & 1 deletion derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ documentation = "https://docs.rs/knus"
readme = "README.md"

[lib]
proc_macro = true
proc-macro = true

[dependencies]
heck = {version="0.4.1", features=["unicode"]}
Expand Down
32 changes: 10 additions & 22 deletions derive/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub enum VariantKind {
Unit,
Nested { option: bool },
Tuple(Struct),
Named(Struct),
Named,
}

pub enum ArgKind {
Expand Down Expand Up @@ -211,9 +211,6 @@ pub struct StructBuilder {

pub struct NewType {
pub ident: syn::Ident,
pub trait_props: TraitProps,
pub generics: syn::Generics,
pub option: bool,
}

pub struct Variant {
Expand All @@ -234,15 +231,15 @@ impl TraitProps {
let mut props = TraitProps {
span_type: None,
};
for attr in mem::replace(attrs, Vec::new()) {
for attr in mem::take(attrs) {
match attr.0 {
Attr::SpanType(ty) => {
props.span_type = Some(ty);
}
_ => attrs.push(attr),
}
}
return props;
props
}
}

Expand All @@ -251,7 +248,7 @@ fn err_pair(s1: &Field, s2: &Field, t1: &str, t2: &str)
{
let mut err = syn::Error::new(s1.span, t1);
err.combine(syn::Error::new(s2.span, t2));
return err;
err
}

fn is_option(ty: &syn::Type) -> bool {
Expand Down Expand Up @@ -309,13 +306,7 @@ impl Enum {
continue;
}
let kind = match var.fields {
syn::Fields::Named(n) => {
Struct::new(var.ident.clone(),
trait_props.clone(),
generics.clone(),
n.named.into_iter())
.map(VariantKind::Named)?
}
syn::Fields::Named(_) => VariantKind::Named,
syn::Fields::Unnamed(u) => {
let tup = Struct::new(
var.ident.clone(),
Expand Down Expand Up @@ -584,7 +575,7 @@ impl StructBuilder {
});
}
}
return Ok(self);
Ok(self)
}
}

Expand Down Expand Up @@ -615,7 +606,7 @@ impl Struct {
res.extend(self.children.iter().map(|c| &c.field));
res.extend(self.var_children.iter().map(|c| &c.field));
res.extend(self.extra_fields.iter().map(|f| &f.field));
return res;
res
}
}

Expand Down Expand Up @@ -657,9 +648,6 @@ impl Parse for Definition {
{
Ok(Definition::NewType(NewType {
ident: item.ident,
trait_props,
generics: item.generics,
option: tup.extra_fields[0].option,
}))
} else {
Ok(Definition::TupleStruct(tup))
Expand Down Expand Up @@ -738,7 +726,7 @@ impl FieldAttrs {
if self.unwrap.is_some() {
emit_error!(span,
"decode modes are not supported on {}", element;
hint= span.clone() => "try putting decode mode \
hint= (*span) => "try putting decode mode \
into unwrap(.., {})", mode;
);
} else {
Expand Down Expand Up @@ -781,7 +769,7 @@ fn parse_attr_list(attrs: &[syn::Attribute]) -> Vec<(Attr, Span)> {
}
}
}
return all;
all
}

fn parse_attrs(input: ParseStream)
Expand Down Expand Up @@ -946,7 +934,7 @@ impl Field {
),
})
}
pub fn from_self(&self) -> TokenStream {
pub fn as_token_stream(&self) -> TokenStream {
match &self.attr {
AttrAccess::Indexed(idx) => quote!(self.#idx),
AttrAccess::Named(name) => quote!(self.#name),
Expand Down
14 changes: 7 additions & 7 deletions derive/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ fn decode_node(common: &Common, child_def: &Child, in_partial: bool,
let ctx = common.ctx;
let fld = &child_def.field.tmp_name;
let dest = if in_partial {
child_def.field.from_self()
child_def.field.as_token_stream()
} else {
quote!(#fld)
};
Expand Down Expand Up @@ -659,7 +659,7 @@ fn insert_child(s: &Common, node: &syn::Ident) -> syn::Result<TokenStream> {
let ctx = s.ctx;
let mut match_branches = Vec::with_capacity(s.object.children.len());
for child_def in &s.object.children {
let dest = &child_def.field.from_self();
let dest = &child_def.field.as_token_stream();
let child_name = &child_def.name;
if matches!(child_def.mode, ChildMode::Flatten) {
match_branches.push(quote! {
Expand All @@ -686,7 +686,7 @@ fn insert_child(s: &Common, node: &syn::Ident) -> syn::Result<TokenStream> {
} else {
let dup_err = format!("duplicate node `{}`, single node expected",
child_name.escape_default());
let decode = decode_node(s, &child_def, true, node)?;
let decode = decode_node(s, child_def, true, node)?;
match_branches.push(quote! {
#child_name => {
if #dest.is_some() {
Expand All @@ -713,7 +713,7 @@ fn insert_property(s: &Common, name: &syn::Ident, value: &syn::Ident)
let ctx = s.ctx;
let mut match_branches = Vec::with_capacity(s.object.children.len());
for prop in &s.object.properties {
let dest = &prop.field.from_self();
let dest = &prop.field.as_token_stream();
let prop_name = &prop.name;
if prop.flatten {
match_branches.push(quote! {
Expand All @@ -722,7 +722,7 @@ fn insert_property(s: &Common, name: &syn::Ident, value: &syn::Ident)
=> Ok(true),
});
} else {
let decode_value = decode_value(&value, ctx, &prop.decode,
let decode_value = decode_value(value, ctx, &prop.decode,
prop.option)?;
if prop.option {
match_branches.push(quote! {
Expand Down Expand Up @@ -784,7 +784,7 @@ fn decode_children(s: &Common, children: &syn::Ident,
declare_empty.push(quote! {
let mut #fld = Vec::new();
});
let decode = decode_node(s, &child_def, false, &child)?;
let decode = decode_node(s, child_def, false, &child)?;
match_branches.push(quote! {
#child_name => #decode,
});
Expand Down Expand Up @@ -832,7 +832,7 @@ fn decode_children(s: &Common, children: &syn::Ident,
let dup_err = format!(
"duplicate node `{}`, single node expected",
child_name.escape_default());
let decode = decode_node(s, &child_def, false, &child)?;
let decode = decode_node(s, child_def, false, &child)?;
match_branches.push(quote! {
#child_name => {
if #fld.is_some() {
Expand Down
2 changes: 1 addition & 1 deletion derive/src/variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn decode(e: &Common, node: &syn::Ident) -> syn::Result<TokenStream> {
#name => { #decode }
});
}
VariantKind::Named(_) => todo!(),
VariantKind::Named => unimplemented!(),
}
}
// TODO(tailhook) use strsim to find similar names
Expand Down
21 changes: 21 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
watch:
watchexec -e rs,toml just test

test:
cargo test --workspace

lint:
cargo fmt --check
cargo clippy --workspace --tests

cov:
cargo +nightly llvm-cov --workspace --branch --open

ci-cov:
cargo +nightly llvm-cov --workspace --branch --codecov --output-path codecov.json

check-wasm:
cargo check --workspace --target wasm32-unknown-unknown

min-versions:
cargo +nightly test --workspace -Z direct-minimal-versions
Empty file removed lib/Cargo.toml
Empty file.
9 changes: 4 additions & 5 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ pub enum Literal {
impl<S> Node<S> {
/// Returns node children
pub fn children(&self)
-> impl Iterator<Item=&Spanned<Node<S>, S>> +
ExactSizeIterator
-> impl ExactSizeIterator<Item=&Spanned<Node<S>, S>>
{
self.children.as_ref().map(|c| c.iter()).unwrap_or_else(|| [].iter())
}
Expand Down Expand Up @@ -295,9 +294,9 @@ impl fmt::Display for TypeName {
}
}

impl Into<TypeName> for BuiltinType {
fn into(self) -> TypeName {
self.as_type()
impl From<BuiltinType> for TypeName {
fn from(val: BuiltinType) -> Self {
val.as_type()
}
}

Expand Down
Loading