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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
//! # Mongo Derive
//!
//! This crate provides two derives `Bson` & `Mongo` for the [`mongo`][mongo] crate.
//!
//! ## Examples
//!
//! Deriving BSON
//!
//! ```
//! # use mongod_derive::Bson;
//! #[derive(Bson)]
//! pub struct User {
//! name: String,
//! age: u32,
//! }
//! ```
//!
//! Deriving Mongo
//!
//! ```
//! # mod wrap {
//! # use mongod_derive::Mongo;
//! # #[derive(mongod_derive::Bson)]
//! #[derive(Mongo)]
//! #[mongo(collection = "users", field, filter, update)]
//! pub struct User {
//! name: String,
//! age: u32,
//! }
//! # }
//! ```
//!
//! [mongo]: https://docs.rs/mongo
#[macro_use]
extern crate quote;
#[macro_use]
extern crate syn;
use proc_macro::TokenStream;
use syn::DeriveInput;
mod ast;
mod bson;
mod mongo;
/// Derives implementations for `TryFrom` so that the decorated type can be converted `to` & `from`
/// BSON.
///
/// ## Container Attributes
///
/// - #[bson(from)]: derives `TryFrom` on `Bson` for `type`
/// - #[bson(into)]: derives `TryFrom` on `type` for `Bson`
///
/// ### `#[bson(from)]`
///
/// Tells the derive to only implement the `from` parts of the derive, i.e. deserialising only.
///
/// ```
/// # use mongod_derive::Bson;
/// use std::convert::TryFrom;
///
/// #[derive(Debug, Bson)]
/// #[bson(from)]
/// struct User {
/// name: String,
/// }
/// let mut doc = mongod::bson::Document::new();
/// doc.insert("name", "foo".to_owned());
/// let bson = mongod::bson::Bson::Document(doc);
///
/// let user = User::try_from(bson).unwrap();
///
/// println!("{:?}", user);
/// ```
///
/// ### `#[bson(into)]`
///
/// Tells the derive to only implement the `into` parts of the derive, i.e. serialising only.
///
/// ```
/// # use mongod_derive::Bson;
/// use std::convert::TryFrom;
///
/// #[derive(Bson)]
/// #[bson(into)]
/// struct User {
/// name: String,
/// }
///
/// let user = User { name: "foo".to_owned() };
///
/// let bson = mongod::bson::Bson::try_from(user).unwrap();
///
/// println!("{:?}", bson);
/// ```
///
/// ## Field Attributes
///
/// - #[bson(serde)]
///
/// ### `#[bson(serde)]`
///
/// Tells the derive to use `serde` for the decorated field.
/// ```
/// # use mongod_derive::Bson;
/// use std::convert::TryFrom;
///
/// #[derive(Bson)]
/// struct User {
/// name: String,
/// #[bson(serde)]
/// age: u32,
/// }
///
/// let user = User { name: "foo".to_owned(), age: 0 };
///
/// let bson = mongod::bson::Bson::try_from(user).unwrap();
///
/// println!("{:?}", bson);
/// ```
#[proc_macro_derive(Bson, attributes(bson))]
pub fn derive_bson(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
bson::expand_derive_bson(&input)
.unwrap_or_else(to_compile_errors)
.into()
}
/// Derives mongo traits on the decorated type.
///
/// ## Container Attributes
///
/// - `#[mongo(collection = "...")]`: derives the `Collection` trait
/// - `#[mongo(field)]`: derives the `AsField` & `Field` traits
/// - `#[mongo(filter)]`: derives the `AsFilter` & `Filter` traits
/// - `#[mongo(update)]`: derives the `AsUpdate` & `Update` traits
///
/// ### `#[mongo(collection = "...")]`
///
/// Tells the derive to implement the `Collection` trait where the `"..."` is the name of the
/// collection.
///
/// ```
/// # mod wrap {
/// # use mongod_derive::Mongo;
/// # #[derive(mongod_derive::Bson)]
/// #[derive(Mongo)]
/// #[mongo(collection = "users")]
/// pub struct User {
/// name: String,
/// age: u32,
/// }
/// # }
/// ```
///
/// ### `#[mongo(field)]`
///
/// Tells the derive to implement the `AsField` & `Field` traits.
///
/// ```
/// # mod wrap {
/// # use mongod_derive::Mongo;
/// # #[derive(mongod_derive::Bson)]
/// #[derive(Mongo)]
/// #[mongo(field)]
/// pub struct User {
/// name: String,
/// age: u32,
/// }
///
/// // The derived field enum can be exposed from the derived module which uses the type's name in
/// // snake_case
/// use self::user::Field;
/// # }
/// ```
///
/// ### `#[mongo(filter)]`
///
/// Tells the derive to implement the `AsFilter` & `Filter` traits.
///
/// ```
/// # mod wrap {
/// # use mongod_derive::Mongo;
/// # #[derive(mongod_derive::Bson)]
/// #[derive(Mongo)]
/// #[mongo(filter)]
/// pub struct User {
/// name: String,
/// age: u32,
/// }
///
/// // The derived filter struct can be exposed from the derived module which uses the type's name in
/// // snake_case
/// use self::user::Filter;
/// # }
/// ```
///
/// ### `#[mongo(update)]`
///
/// Tells the derive to implement the `AsUpdate` & `Update` traits.
///
/// ```
/// # mod wrap {
/// # use mongod_derive::Mongo;
/// # #[derive(mongod_derive::Bson)]
/// #[derive(Mongo)]
/// #[mongo(update)]
/// pub struct User {
/// name: String,
/// age: u32,
/// }
///
/// // The derived update struct can be exposed from the derived module which uses the type's name in
/// // snake_case
/// use self::user::Update;
/// # }
/// ```
///
/// ## Field Attributes
///
/// - `#[mongo(serde)]`: tells the derive that the field should be handled using serde
/// - `#[mongo(skip)]`: tells the derive to skip the field for `field`, `filter` & `update`
///
/// ### `#[mongo(serde)]`
///
/// Tells the derive that the field should be handled using serde
///
/// ```
/// # mod wrap {
/// # use mongod_derive::Mongo;
/// # #[derive(mongod_derive::Bson)]
/// #[derive(Mongo)]
/// #[mongo(collection = "users")]
/// pub struct User {
/// name: String,
/// #[mongo(serde)]
/// age: u32,
/// }
/// # }
/// ```
///
/// ### `#[mongo(skip)]`
///
/// Tells the derive to skip the field for `field`, `filter` & `update`
///
/// ```
/// # mod wrap {
/// # use mongod_derive::Mongo;
/// # #[derive(mongod_derive::Bson)]
/// #[derive(Mongo)]
/// #[mongo(collection = "users")]
/// pub struct User {
/// name: String,
/// #[mongo(skip)]
/// age: u32,
/// }
/// # }
/// ```
#[proc_macro_derive(Mongo, attributes(mongo))]
pub fn derive_collection(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
mongo::expand_derive_mongo(&input)
.unwrap_or_else(to_compile_errors)
.into()
}
fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
let compile_errors = errors.iter().map(syn::Error::to_compile_error);
quote!(#(#compile_errors)*)
}