From fe66939b940edc29fcf6a6ee9b9c82e583738820 Mon Sep 17 00:00:00 2001 From: mox692 Date: Thu, 4 Jan 2024 04:40:45 +0900 Subject: [PATCH 1/3] add ci --- .github/workflows/ci.yml | 97 +++++++++++++++++++++++++++++++++ or-rs-macros/src/lib.rs | 53 ++++++++++++++++++ tests/tests/integration_test.rs | 1 + tests/tests/macro_test.rs | 10 ++++ 4 files changed, 161 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..fec1e57 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,97 @@ +name: CI + +on: + push: + pull_request: + workflow_dispatch: + schedule: [cron: "40 1 * * *"] + +permissions: + contents: read + +env: + RUSTFLAGS: -Dwarnings + +jobs: + pre_ci: + uses: dtolnay/.github/.github/workflows/pre_ci.yml@master + + test: + name: Rust ${{matrix.rust}} + needs: pre_ci + if: needs.pre_ci.outputs.continue + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + rust: [nightly, beta, stable, 1.56.0] + timeout-minutes: 45 + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{matrix.rust}} + - name: Enable type layout randomization + run: echo RUSTFLAGS=${RUSTFLAGS}\ -Zrandomize-layout >> $GITHUB_ENV + if: matrix.rust == 'nightly' + - run: cargo test + + minimal: + name: Minimal versions + needs: pre_ci + if: needs.pre_ci.outputs.continue + runs-on: ubuntu-latest + timeout-minutes: 45 + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly + - run: cargo generate-lockfile -Z minimal-versions + - run: cargo check --locked + + doc: + name: Documentation + needs: pre_ci + if: needs.pre_ci.outputs.continue + runs-on: ubuntu-latest + timeout-minutes: 45 + env: + RUSTDOCFLAGS: -Dwarnings + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly + - uses: dtolnay/install@cargo-docs-rs + - run: cargo docs-rs + + clippy: + name: Clippy + runs-on: ubuntu-latest + if: github.event_name != 'pull_request' + timeout-minutes: 45 + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@clippy + - run: cargo clippy --tests -- -Dclippy::all -Dclippy::pedantic + + miri: + name: Miri + needs: pre_ci + if: needs.pre_ci.outputs.continue + runs-on: ubuntu-latest + timeout-minutes: 45 + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@miri + - run: cargo miri setup + - run: cargo miri test + env: + MIRIFLAGS: -Zmiri-strict-provenance + + outdated: + name: Outdated + runs-on: ubuntu-latest + if: github.event_name != 'pull_request' + timeout-minutes: 45 + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/install@cargo-outdated + - run: cargo outdated --workspace --exit-code 1 diff --git a/or-rs-macros/src/lib.rs b/or-rs-macros/src/lib.rs index 9c05bef..eb8c672 100644 --- a/or-rs-macros/src/lib.rs +++ b/or-rs-macros/src/lib.rs @@ -58,3 +58,56 @@ use proc_macro::TokenStream; pub fn or_gen(_attr: TokenStream, item: TokenStream) -> TokenStream { parser::MacroParser::parse(item) } + +#[proc_macro] +pub fn my_first_proc_macro(item: TokenStream) -> TokenStream { + item +} + +use proc_macro::TokenStream; +use quote::quote; +use syn::*; + +#[proc_macro_attribute] +pub fn add_print(_attr: TokenStream, item: TokenStream) -> TokenStream { + // 入力をItemFn(関数を表現する構文木データ型に変換) + let input: ItemFn = parse_macro_input!(item as ItemFn); + // 関数名を取得 + let name = &input.sig.ident; + // 関数のブロックを取得 + let block = &input.block; + + // quoteマクロでproc_macro2::TokenStreamを生成 + let expanded: proc_macro2::TokenStream = quote! { + fn #name() { + println!("Function {} is called", stringify!(#name)); + #block + } + }; + + // proc_macro2::TokenStreamからTokenStreamに変換 + TokenStream::from(expanded) +} + +use proc_macro::{TokenStream, TokenTree}; + +#[proc_macro_attribute] +pub fn log(attr: TokenStream, item: TokenStream) -> TokenStream { + let mut iter = item.into_iter(); + + if let Some(TokenTree::Ident(ident)) = iter.next() { + let func_name = ident.to_string(); + let rest_of_stream: TokenStream = iter.collect(); + + let new_stream = format!( + "fn {}() {{ println!(\"Function '{}' called\"); {} }}", + func_name, func_name, rest_of_stream + ); + new_stream.parse().unwrap() + } else { + // トークンストリームが関数宣言でない場合はエラー + "compile_error!(\"Expected function declaration\")" + .parse() + .unwrap() + } +} diff --git a/tests/tests/integration_test.rs b/tests/tests/integration_test.rs index 76ceeff..6155a32 100644 --- a/tests/tests/integration_test.rs +++ b/tests/tests/integration_test.rs @@ -1,6 +1,7 @@ #![feature(proc_macro_hygiene)] // for now, you have to add this unstable feature flag use or_rs::enums::*; +use or_rs_macros::my_first_proc_macro; use or_rs_macros::or_gen; fn main() { diff --git a/tests/tests/macro_test.rs b/tests/tests/macro_test.rs index b2c9cb8..a756ed1 100644 --- a/tests/tests/macro_test.rs +++ b/tests/tests/macro_test.rs @@ -1,6 +1,8 @@ #![feature(proc_macro_hygiene)] #![allow(unused_variables)] +use or_rs_macros::add_print; +use or_rs_macros::my_first_proc_macro; use or_rs_macros::or_gen; #[test] @@ -56,3 +58,11 @@ fn test_compile() { _ => "hello".to_string(), }; } + +// fn foo() { +// my_first_proc_macro!(42); +// } + +#[test] +#[add_print] +fn your_function() {} From e316a638816a47d7a94cbd3b644c898b296d4d6d Mon Sep 17 00:00:00 2001 From: mox692 Date: Thu, 4 Jan 2024 04:54:57 +0900 Subject: [PATCH 2/3] remove change --- or-rs-macros/src/lib.rs | 53 --------------------------------- tests/tests/integration_test.rs | 1 - tests/tests/macro_test.rs | 10 ------- 3 files changed, 64 deletions(-) diff --git a/or-rs-macros/src/lib.rs b/or-rs-macros/src/lib.rs index eb8c672..9c05bef 100644 --- a/or-rs-macros/src/lib.rs +++ b/or-rs-macros/src/lib.rs @@ -58,56 +58,3 @@ use proc_macro::TokenStream; pub fn or_gen(_attr: TokenStream, item: TokenStream) -> TokenStream { parser::MacroParser::parse(item) } - -#[proc_macro] -pub fn my_first_proc_macro(item: TokenStream) -> TokenStream { - item -} - -use proc_macro::TokenStream; -use quote::quote; -use syn::*; - -#[proc_macro_attribute] -pub fn add_print(_attr: TokenStream, item: TokenStream) -> TokenStream { - // 入力をItemFn(関数を表現する構文木データ型に変換) - let input: ItemFn = parse_macro_input!(item as ItemFn); - // 関数名を取得 - let name = &input.sig.ident; - // 関数のブロックを取得 - let block = &input.block; - - // quoteマクロでproc_macro2::TokenStreamを生成 - let expanded: proc_macro2::TokenStream = quote! { - fn #name() { - println!("Function {} is called", stringify!(#name)); - #block - } - }; - - // proc_macro2::TokenStreamからTokenStreamに変換 - TokenStream::from(expanded) -} - -use proc_macro::{TokenStream, TokenTree}; - -#[proc_macro_attribute] -pub fn log(attr: TokenStream, item: TokenStream) -> TokenStream { - let mut iter = item.into_iter(); - - if let Some(TokenTree::Ident(ident)) = iter.next() { - let func_name = ident.to_string(); - let rest_of_stream: TokenStream = iter.collect(); - - let new_stream = format!( - "fn {}() {{ println!(\"Function '{}' called\"); {} }}", - func_name, func_name, rest_of_stream - ); - new_stream.parse().unwrap() - } else { - // トークンストリームが関数宣言でない場合はエラー - "compile_error!(\"Expected function declaration\")" - .parse() - .unwrap() - } -} diff --git a/tests/tests/integration_test.rs b/tests/tests/integration_test.rs index 6155a32..76ceeff 100644 --- a/tests/tests/integration_test.rs +++ b/tests/tests/integration_test.rs @@ -1,7 +1,6 @@ #![feature(proc_macro_hygiene)] // for now, you have to add this unstable feature flag use or_rs::enums::*; -use or_rs_macros::my_first_proc_macro; use or_rs_macros::or_gen; fn main() { diff --git a/tests/tests/macro_test.rs b/tests/tests/macro_test.rs index a756ed1..b2c9cb8 100644 --- a/tests/tests/macro_test.rs +++ b/tests/tests/macro_test.rs @@ -1,8 +1,6 @@ #![feature(proc_macro_hygiene)] #![allow(unused_variables)] -use or_rs_macros::add_print; -use or_rs_macros::my_first_proc_macro; use or_rs_macros::or_gen; #[test] @@ -58,11 +56,3 @@ fn test_compile() { _ => "hello".to_string(), }; } - -// fn foo() { -// my_first_proc_macro!(42); -// } - -#[test] -#[add_print] -fn your_function() {} From e8498efdec3441a9f25e4a0fd69bd553ac2b993f Mon Sep 17 00:00:00 2001 From: mox692 Date: Thu, 4 Jan 2024 05:08:20 +0900 Subject: [PATCH 3/3] update ci --- .github/workflows/ci.yml | 26 +------------------------- or-rs-macros/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 26 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fec1e57..c03db44 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: strategy: fail-fast: false matrix: - rust: [nightly, beta, stable, 1.56.0] + rust: [nightly, 1.60.0] timeout-minutes: 45 steps: - uses: actions/checkout@v4 @@ -48,20 +48,6 @@ jobs: - run: cargo generate-lockfile -Z minimal-versions - run: cargo check --locked - doc: - name: Documentation - needs: pre_ci - if: needs.pre_ci.outputs.continue - runs-on: ubuntu-latest - timeout-minutes: 45 - env: - RUSTDOCFLAGS: -Dwarnings - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@nightly - - uses: dtolnay/install@cargo-docs-rs - - run: cargo docs-rs - clippy: name: Clippy runs-on: ubuntu-latest @@ -85,13 +71,3 @@ jobs: - run: cargo miri test env: MIRIFLAGS: -Zmiri-strict-provenance - - outdated: - name: Outdated - runs-on: ubuntu-latest - if: github.event_name != 'pull_request' - timeout-minutes: 45 - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/install@cargo-outdated - - run: cargo outdated --workspace --exit-code 1 diff --git a/or-rs-macros/Cargo.toml b/or-rs-macros/Cargo.toml index fc4a5f1..dd24881 100644 --- a/or-rs-macros/Cargo.toml +++ b/or-rs-macros/Cargo.toml @@ -15,7 +15,7 @@ keywords = ["either", "macro", "or"] [dependencies] quote = "1" -colored = { version = "1", optional = true } +colored = { version = "2", optional = true } proc-macro2 = { version = "1", optional = true } syn = { version = "2.0", features = ["full"] }