Skip to content

Remove proc macro error #5

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

Merged
merged 5 commits into from
Apr 17, 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
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ proc-macro = true
doctest = false

[dependencies]
quote = "1.0.33"
proc-macro2 = "1.0.69"
syn = { version = "2.0.39", features = ["full"] }
proc-macro-error = "1.0.4"
quote = "1.0.36"
proc-macro2 = "1.0.81"
syn = { version = "2.0.59", features = ["full"] }
unicode-ident = "1.0.12"
53 changes: 34 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![doc = include_str!("../README.md")]
use proc_macro2::{Ident, TokenStream};
use proc_macro_error::{abort, abort_call_site, proc_macro_error};
use quote::{format_ident, quote};
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
Expand All @@ -16,6 +15,18 @@ struct TestEachArgs {
extensions: Vec<String>,
}

macro_rules! abort {
($span:expr, $message:expr) => {
return Err(syn::Error::new($span, $message))
};
}

macro_rules! abort_token_stream {
($span:expr, $message:expr) => {
return syn::Error::new($span, $message).into_compile_error().into()
};
}

impl Parse for TestEachArgs {
fn parse(input: ParseStream) -> syn::Result<Self> {
// Optionally parse extensions if the keyword `for` is used. Aborts if none are given.
Expand Down Expand Up @@ -80,7 +91,7 @@ struct Tree {
}

impl Tree {
fn new(base: &Path, extensions: &[String]) -> Self {
fn new(base: &Path, extensions: &[String]) -> Result<Self, String> {
let mut tree = Self::default();
for entry in base.read_dir().unwrap() {
let mut entry = entry.unwrap().path();
Expand All @@ -104,13 +115,13 @@ impl Tree {
} else if entry.is_dir() {
tree.children.insert(
entry.as_path().to_path_buf(),
Self::new(entry.as_path(), extensions),
Self::new(entry.as_path(), extensions)?,
);
} else {
abort_call_site!(format!("Unsupported path: {:#?}.", entry))
return Err(format!("Unsupported path: {:#?}.", entry));
}
}
tree
Ok(tree)
}
}

Expand Down Expand Up @@ -155,15 +166,15 @@ fn generate_from_tree(
parsed: &TestEachArgs,
stream: &mut TokenStream,
invocation_type: &Type,
) {
) -> Result<(), String> {
let mut taken_names_folders = HashSet::new();
for (name, directory) in tree.children.iter() {
let file_name = name.file_name().unwrap().to_str().unwrap();
let file_name = sanitize_ident(file_name);
let file_name = generate_name(file_name, &mut taken_names_folders);

let mut sub_stream = TokenStream::new();
generate_from_tree(directory, parsed, &mut sub_stream, invocation_type);
generate_from_tree(directory, parsed, &mut sub_stream, invocation_type)?;
stream.extend(quote! {
mod #file_name {
use super::*;
Expand Down Expand Up @@ -192,13 +203,10 @@ fn generate_from_tree(
let mut arguments = TokenStream::new();

for extension in &parsed.extensions {
let input = file.with_extension(extension).canonicalize().unwrap();
if !input.exists() {
abort_call_site!(format!(
"Expected file {:?} with extension {}, but it does not exist.",
file, extension
))
}
let input = match file.with_extension(extension).canonicalize() {
Ok(path) => path,
Err(e) => return Err(format!("Failed to read expected file {}.{extension}: {e}", file.display())),
};
let input = input.to_str().unwrap();

arguments.extend(match invocation_type {
Expand All @@ -217,18 +225,27 @@ fn generate_from_tree(
}
});
}

Ok(())
}

fn test_each(input: proc_macro::TokenStream, invocation_type: &Type) -> proc_macro::TokenStream {
let parsed = parse_macro_input!(input as TestEachArgs);

if !Path::new(&parsed.path.value()).is_dir() {
abort!(parsed.path.span(), "Given directory does not exist");
abort_token_stream!(parsed.path.span(), "Given directory does not exist");
}

let mut tokens = TokenStream::new();
let files = Tree::new(parsed.path.value().as_ref(), &parsed.extensions);
generate_from_tree(&files, &parsed, &mut tokens, invocation_type);

let files = match Tree::new(parsed.path.value().as_ref(), &parsed.extensions) {
Ok(files) => files,
Err(e) => abort_token_stream!(parsed.path.span(), e),
};

if let Err(e) = generate_from_tree(&files, &parsed, &mut tokens, invocation_type) {
abort_token_stream!(parsed.path.span(), e)
}

if let Some(module) = parsed.module {
tokens = quote! {
Expand All @@ -247,7 +264,6 @@ fn test_each(input: proc_macro::TokenStream, invocation_type: &Type) -> proc_mac
///
/// See crate level documentation for details.
#[proc_macro]
#[proc_macro_error]
pub fn test_each_file(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
test_each(input, &Type::File)
}
Expand All @@ -256,7 +272,6 @@ pub fn test_each_file(input: proc_macro::TokenStream) -> proc_macro::TokenStream
///
/// See crate level documentation for details.
#[proc_macro]
#[proc_macro_error]
pub fn test_each_path(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
test_each(input, &Type::Path)
}