Skip to content

Commit

Permalink
add ci
Browse files Browse the repository at this point in the history
  • Loading branch information
mox692 committed Jan 3, 2024
1 parent 9387d11 commit fe66939
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
97 changes: 97 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions or-rs-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
1 change: 1 addition & 0 deletions tests/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
10 changes: 10 additions & 0 deletions tests/tests/macro_test.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -56,3 +58,11 @@ fn test_compile() {
_ => "hello".to_string(),
};
}

// fn foo() {
// my_first_proc_macro!(42);
// }

#[test]
#[add_print]
fn your_function() {}

0 comments on commit fe66939

Please sign in to comment.