Skip to content

Latest commit

 

History

History
69 lines (53 loc) · 1.45 KB

README.MD

File metadata and controls

69 lines (53 loc) · 1.45 KB

Travis branch Latest Version Travis branch

I was going to write a simple HTTP client library.

There is an enum, which called HTTPMethod, for building a HTTP request message, I have to convert the enum to str.

Without macro, I have to write a lot of pattern match expressions, I believe there will be many typos.

I think it sucks! So I learned the ProcMacro and write this lib.

warning

Because of proc-macro, this lib is nightly only(1.22).

example

cargo.toml

[dependencies]
enum_to_str_derive = "0.1.0"

main.rs

#[macro_use]
extern crate enum_to_str_derive;

#[derive(EnumToStr)]
enum HTTPMethod {
    GET,
    HEAD,
    POST,
    PUT,
    DELETE,
    CONNECT,
    OPTIONS,
    TRACE
}

fn main() {
    assert_eq!("GET", HTTPMethod::GET.enum_to_str());
    assert_eq!("HEAD", HTTPMethod::HEAD.enum_to_str());
    assert_eq!("POST", HTTPMethod::POST.enum_to_str());
}

Other Way (which means I'm stupid)

#[derive(Debug)]
enum HTTPMethod {
    GET,
    HEAD,
    POST,
    PUT,
    DELETE,
    CONNECT,
    OPTIONS,
    TRACE
}

fn main() {
    assert_eq!("GET", format!("{:?}", HTTPMethod::GET));
}