It provides functional macros to reuse fields from Struct and Enum definition.
[dependencies]
publish = "0.0.0"
use publish::{
nested_macro,
public_struct,
};
public_struct!(
// pub is required before 'struct' when you use public_struct!
pub struct MessageBase {
pub text: String
// text: String // pub is optional in fields.
}
); // It is lazy. Nothing is made yet.
MessageBase!(); // You have to call it to use the struct.
fn main() {
let message = MessageBase {
text: "First Message".into(),
};
println!("{}", &message.text);
}
use publish::{
nested_macro,
public_enum,
};
public_enum!(
// pub is required before 'enum' when you use public_enum!
pub enum WebEventBase {
PageLoad,
PageUnload, // , here is required if you want to extend the fields later.
}
); // It is lazy. Nothing is made yet.
WebEventBase!(); // You have to call it to use the enum.
fn inspect(event: WebEventBase) {
match event {
WebEventBase ::PageLoad => println!("page loaded"),
WebEventBase ::PageUnload => println!("page unloaded"),
}
}
fn main() {
let load = WebEventBase::PageLoad;
let unload = WebEventBase::PageUnload;
inspect(load);
inspect(unload);
}
-
Each struct and enum created from the macros are completely unrelevant except they have the same fields you define.
-
When you use
private_struct!
andprivate_enum!
, you can't use pub keyword in it and others use them. It wouldn't be logical if a private struct or private enum can have public fields. -
nested_macro!
is required to use the other macros from this crate. It is used to make a macro that creates macros.
macro_rules! nested_macro {
($($body:tt)*) => {
macro_rules! __nested_macro { $($body)+ }
__nested_macro!($);
}
}
-
You can reuse the fields with attribute macros also. But, you need to install more dependencies.
-
If you want more, please read the official documenation about procedural macros.
$git clone git@github.com:steadylearner/publish.git && cargo test pass
$cargo test pass
to run passing tests.$cargo test fail
to run failing tests. You need to install trybuild first.
If you want to see how macros from this package expand, use $cargo test macros
. You need to install rustfmt and cargo-expand to use it.
$rustup component add rustfmt && cargo install cargo-expand
macrotest is based on trybuild. They are not that compatible to test with a single command. It make the test to redownload the dependendencies and recompile everytime.
For that reason, there are commands to test them separately.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
-
Exclude unnecessary file for the crate such as Python files, clippy.toml etc. Test it with a real crate name and code instead.