Skip to content

Commit

Permalink
build: Move macro implementations under internal
Browse files Browse the repository at this point in the history
Signed-off-by: Akira Moroo <[email protected]>
  • Loading branch information
retrage committed Mar 11, 2023
1 parent 2032900 commit 6aceb5a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 31 deletions.
6 changes: 6 additions & 0 deletions src/internal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
// Akira Moroo <[email protected]> 2023

mod chatgpt;

pub mod auto_test;
39 changes: 39 additions & 0 deletions src/internal/auto_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: MIT
// Akira Moroo <[email protected]> 2023

use proc_macro::TokenStream;
use std::collections::HashSet;
use syn::{
parse::{Parse, ParseStream, Result},
parse_macro_input, Ident, Token,
};

use crate::internal::chatgpt;

/// Parses a list of test function names separated by commas.
///
/// test_valid, test_div_by_zero
///
/// The function name is used to generate the test function name.
struct Args {
test_names: HashSet<Ident>,
}

impl Parse for Args {
fn parse(input: ParseStream) -> Result<Self> {
let test_names = input.parse_terminated::<Ident, Token![,]>(Ident::parse)?;
Ok(Args {
test_names: test_names.into_iter().collect(),
})
}
}

pub fn auto_test_impl(args: TokenStream, input: TokenStream) -> TokenStream {
// Parse the list of test function names that should be generated.
let args = parse_macro_input!(args as Args);

match chatgpt::generate_tests(input, args.test_names) {
Ok(output) => output,
Err(e) => panic!("{}", e),
}
}
File renamed without changes.
34 changes: 3 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
// SPDX-License-Identifier: MIT
// Akira Moroo <[email protected]> 2023

use internal::auto_test::auto_test_impl;
use proc_macro::TokenStream;
use std::collections::HashSet;
use syn::{
parse::{Parse, ParseStream, Result},
parse_macro_input, Ident, Token,
};

mod chatgpt;

/// Parses a list of test function names separated by commas.
///
/// test_valid, test_div_by_zero
///
/// The function name is used to generate the test function name.
struct Args {
test_names: HashSet<Ident>,
}

impl Parse for Args {
fn parse(input: ParseStream) -> Result<Self> {
let test_names = input.parse_terminated::<Ident, Token![,]>(Ident::parse)?;
Ok(Args {
test_names: test_names.into_iter().collect(),
})
}
}
mod internal;

/// Attribute macro for automatically generating tests for functions.
///
Expand All @@ -51,11 +29,5 @@ impl Parse for Args {
/// ```
#[proc_macro_attribute]
pub fn auto_test(args: TokenStream, input: TokenStream) -> TokenStream {
// Parse the list of test function names that should be generated.
let args = parse_macro_input!(args as Args);

match chatgpt::generate_tests(input, args.test_names) {
Ok(output) => output,
Err(e) => panic!("{}", e),
}
auto_test_impl(args, input)
}

0 comments on commit 6aceb5a

Please sign in to comment.