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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! Custom derives for working with the [`slog`] crate.
//!
//! # The `KV` Derive
//!
//! Say you've got a struct like this,
//!
//! ```rust
//! # use std::path::PathBuf;
//! pub struct Config {
//!   width: f64,
//!   height: f64,
//!   output_file: PathBuf,
//! }
//! ```
//!
//! Sometimes you'll want to log the struct's contents in your application, for
//! example when you've just started and want to record the configuration
//! details for debugging purposes. Usually you'd need to do something like
//! this:
//!
//! ```rust
//! # #[macro_use]
//! # extern crate slog;
//! # use std::path::PathBuf;
//! # fn main() {
//! # let logger = slog::Logger::root(slog::Discard, o!());
//! # struct Config { width: f64, height: f64, output_file: PathBuf }
//! # let cfg = Config { width: 1.0, height: 0.0, output_file: "Foo".into() };
//! debug!(logger, "Loaded Config";
//!     "width" => cfg.width,
//!     "height" => cfg.height,
//!     "output-file" => cfg.output_file.display());
//! # }
//! ```
//!
//! This is where the [`KV`] trait comes in. Implementing it lets you translate
//! the previous log statement into something like this:
//!
//! ```rust
//! # #[macro_use]
//! # extern crate slog;
//! # fn main() {
//! # let logger = slog::Logger::root(slog::Discard, o!());
//! # let cfg = o!();
//! debug!(logger, "Loaded Config"; cfg);
//! # }
//! ```
//!
//! This crate provides a custom derive which will implement [`KV`] for you.
//! It'll just iterate over each field in your `struct` and invoke
//! [`Value::serialize()`] on each.
//!
//! ```rust
//! # #[macro_use]
//! # extern crate slog_derive;
//! # extern crate slog;
//! #[derive(KV)]
//! pub struct Config {
//!   width: f64,
//!   height: f64,
//!   output_file: String,
//! }
//! # fn main() {}
//! ```
//!
//! You can also skip fields using the `#[skip]` attribute, this is useful when
//! you don't want to log complex data structures or the particular field
//! doesn't implement `Value`.
//!
//! ```rust
//! # #[macro_use]
//! # extern crate slog_derive;
//! # extern crate slog;
//! # use std::path::PathBuf;
//! #[derive(KV)]
//! pub struct Config {
//!   width: f64,
//!   height: f64,
//!   #[slog(skip)]
//!   output_file: PathBuf,
//! }
//! # fn main() {}
//! ```
//!
//! # The `SerdeValue` Derive
//!
//! Implementing the [`SerdeValue`] is usually trivial and tedious so it also
//! has a custom derive.
//!
//! ```rust
//! extern crate slog;
//! #[macro_use]
//! extern crate slog_derive;
//! extern crate serde;
//! #[macro_use]
//! extern crate serde_derive;
//! extern crate erased_serde;
//!
//! use std::path::PathBuf;
//!
//! #[derive(Clone, SerdeValue, Serialize, Deserialize)]
//! pub struct Config {
//!   width: f64,
//!   height: f64,
//!   output_file: PathBuf,
//! }
//! # fn main() {}
//! ```
//!
//! This will require enabling `slog`'s `nested-values` feature flag, as well
//! as implementing (or deriving) `serde::Serialize` for your type. You will
//! also need to pull in the [`erased_serde`] crate because it's part of the
//! `SerdeValue` signature.
//!
//! For convenience this will also generate a `Value` impl for your type (to
//! implement `SerdeValue` you must also implement `Value`). This impl simply
//! calls `Serializer::emit_serde()`, but if you want to write your own `Value`
//! implementation you can add the `#[slog(no_value_impl)]` attribute.
//!
//! ```rust
//! extern crate slog;
//! #[macro_use]
//! extern crate slog_derive;
//! extern crate serde;
//! #[macro_use]
//! extern crate serde_derive;
//! extern crate erased_serde;
//!
//! use std::path::PathBuf;
//! use slog::{Key, Record, Serializer, Value};
//!
//! #[derive(Clone, SerdeValue, Serialize)]
//! #[slog(no_value_impl)]
//! pub struct Config {
//!   width: f64,
//!   height: f64,
//!   output_file: PathBuf,
//! }
//!
//! impl Value for Config {
//!     fn serialize(&self, _record: &Record, key: Key, ser: &mut Serializer) -> slog::Result {
//!         unimplemented!()
//!     }
//! }
//! # fn main() {}
//! ```
//!
//! [`slog`]: https://crates.io/crates/slog
//! [`KV`]: https://docs.rs/slog/2.1.1/slog/trait.KV.html
//! [`Value::serialize()`]: https://docs.rs/slog/2.1.1/slog/trait.Value.html#tymethod.serialize
//! [`SerdeValue`]: https://docs.rs/slog/2.1.1/slog/trait.SerdeValue.html
//! [`erased_serde`]: https://docs.rs/erased_serde

extern crate proc_macro;
#[macro_use]
extern crate quote;
extern crate syn;

mod derive_kv;
mod derive_serde_value;
mod utils;

use proc_macro::TokenStream;
use syn::DeriveInput;

#[proc_macro_derive(KV, attributes(slog))]
pub fn derive_kv(input: TokenStream) -> TokenStream {
    let ast: DeriveInput = syn::parse(input).unwrap();

    let gen = derive_kv::impl_kv(ast);
    gen.to_string().parse().unwrap()
}

#[proc_macro_derive(SerdeValue, attributes(slog))]
pub fn derive_serde_value(input: TokenStream) -> TokenStream {
    let ast: DeriveInput = syn::parse(input).unwrap();

    let gen = derive_serde_value::impl_serde_value(ast);
    gen.to_string().parse().unwrap()
}