Crates.io | procout |
lib.rs | procout |
version | 0.1.13 |
source | src |
created_at | 2021-05-11 18:54:59.484258 |
updated_at | 2021-11-09 14:45:09.990279 |
description | Output a proc macro's TokenStream to a file. |
homepage | |
repository | https://github.com/plasticartsshow/procout |
max_upload_size | |
id | 396206 |
size | 13,080 |
This depends on the procedural macro compiling to code. If it's not at the stage where it compiles, it has to get there before this will produce useful output.
Given a procedural macro's constructed as so,
use proc_macro::{TokenStream};
use proc_macro2::{Span};
use quote::{quote};
use syn::{Ident};
#[proc_macro]
pub fn ast(input: TokenStream) -> TokenStream {
let module_ident = Ident::new("this_module", Span::mixed_site());
let code_block: proc_macro2::TokenStream = quote!{
pub mod #module_ident {
/* ... some truly fantastic code, well done ... */
}
};
// Convert and return the code
TokenStream::from(code_block)
}
Just insert a call to procout before the conversion and return step.
use proc_macro::{TokenStream};
use proc_macro2::{Span};
use procout::{procout}; // Look!
use quote::{quote};
use syn::{Ident};
#[proc_macro]
pub fn ast(input: TokenStream) -> TokenStream {
let module_ident = Ident::new("this_module", Span::mixed_site());
let code_block: proc_macro2::TokenStream = quote!{
pub mod #module_ident {
/* ... some truly fantastic code, well done ... */
}
};
// Look!
procout(&code_block, Some(module_ident), Some("a/valid/path/string"));
// Convert and return the code
TokenStream::from(code_block)
}
By calling cargo test --features procout
, the code will print
to the a/valid/path/string
specified as a file corresponding to module_ident
.
By default, the path string is the local tests
directory, so after the first run using the procout
feature, it's possible to run something like cargo test --test module_ident
and get better errors
from the compiler.
module_ident
.module_ident
should be the name of a generated module.tests
subfolder,module_ident
is specified, the default will be a generic timestamp.This will overwrite whatever's at the specified path, so be careful when prototyping.
procout
Outputs the macro to a file. Calling procout
with this feature disabled is an intentional no-op.formatted
Calls rustfmt
on the created file. This is enabled by default and is recommended.notification
Prints a notification to stdout on success. This is enabled by default.License: MIT