1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//! Structopt adapter for the [Paw crate].
//!
//! [Paw crate]: https://docs.rs/paw
//!
//! ## Examples
//! ```
//! use std::io::prelude::*;
//! use std::net::TcpListener;
//!
//! #[derive(paw_structopt::StructOpt, structopt::StructOpt)]
//! struct Args {
//!     /// Port to listen on.
//!     #[structopt(short = "p", long = "port", env = "PORT", default_value = "8080")]
//!     port: u16,
//!     /// Address to listen on.
//!     #[structopt(short = "a", long = "address", default_value = "127.0.0.1")]
//!     address: String,
//! }
//!
//! #[paw::main]
//! fn main(args: Args) -> Result<(), std::io::Error> {
//!     let listener = TcpListener::bind((args.address.as_str(), args.port))?;
//!     println!("listening on {}", listener.local_addr()?);
//!     for stream in listener.incoming() {
//!         stream?.write(b"hello world!")?;
//!     }
//!     Ok(())
//! }
//! ```

#![recursion_limit = "512"]
#![warn(missing_debug_implementations, nonstandard_style, rust_2018_idioms)]

extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(StructOpt)]
pub fn structopt(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;

    let result = quote! {
        impl paw::ParseArgs for #name {
            /// Error type.
            type Error = std::io::Error;

            /// Try to parse an input to a type.
            fn parse_args() -> Result<Self, Self::Error> {
                Ok(<#name as structopt::StructOpt>::from_args())
            }
        }
    };

    result.into()
}