Skip to content

Test with this repo before you publish a real Rust package.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

steadylearner/cargo-publish-example

Repository files navigation

Publish(Struct, Enum)

Build Status Image Crate Image Doc

It provides functional macros to reuse fields from Struct and Enum definition.

[dependencies]
publish = "0.0.0"

Example

Struct

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);
}

Enum

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);
}

Details

  • Each struct and enum created from the macros are completely unrelevant except they have the same fields you define.

  • When you use private_struct! and private_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!($);
    }
}

Comparison with attribute macros


How to test it

$git clone git@github.com:steadylearner/publish.git && cargo test pass
  1. $cargo test pass to run passing tests.
  2. $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.

License

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.

What left

About

Test with this repo before you publish a real Rust package.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published