rustify_derive/lib.rs
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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
//! Provides a derive macro for easily implementing an `Endpoint` from the
//! [rustify][1] crate. See the documentation for `rustify` for details on how
//! to use this macro.
//!
//! [1]: https://docs.rs/rustify/
#[macro_use]
extern crate synstructure;
extern crate proc_macro;
mod error;
mod params;
mod parse;
use std::{collections::HashMap, convert::TryFrom};
use error::Error;
use params::Parameters;
use proc_macro2::Span;
use quote::quote;
use regex::Regex;
use syn::{self, spanned::Spanned, Field, Generics, Ident, Meta};
const MACRO_NAME: &str = "Endpoint";
const ATTR_NAME: &str = "endpoint";
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) enum EndpointAttribute {
Body,
Query,
Raw,
Skip,
Untagged,
}
impl TryFrom<&Meta> for EndpointAttribute {
type Error = Error;
fn try_from(m: &Meta) -> Result<Self, Self::Error> {
match m.path().get_ident() {
Some(i) => match i.to_string().to_lowercase().as_str() {
"body" => Ok(EndpointAttribute::Body),
"query" => Ok(EndpointAttribute::Query),
"raw" => Ok(EndpointAttribute::Raw),
"skip" => Ok(EndpointAttribute::Skip),
_ => Err(Error::new(
m.span(),
format!("Unknown attribute: {}", i).as_str(),
)),
},
None => Err(Error::new(m.span(), "Invalid attribute")),
}
}
}
/// Generates the path string for the endpoint.
///
/// The string supplied by the end-user supports basic interpolation using curly
/// braces. For example,
/// ```
/// endpoint(path = "user/{self.name}")
/// ```
/// Should produce:
/// ```
/// format!("user/{}", self.name);
/// ```
/// This is currently accomplished using a basic regular expression which
/// matches contents in the braces, extracts them out, leaving behind the empty
/// braces and placing the contents into the proper position in `format!`.
///
/// If no interpolation is needed the user provided string is fed into
/// `String::from` without modification.
fn gen_path(path: &syn::LitStr) -> Result<proc_macro2::TokenStream, Error> {
let re = Regex::new(r"\{(.*?)\}").unwrap();
let mut fmt_args: Vec<syn::Expr> = Vec::new();
for cap in re.captures_iter(path.value().as_str()) {
let expr = syn::parse_str(&cap[1]);
match expr {
Ok(ex) => fmt_args.push(ex),
Err(_) => {
return Err(Error::new(
path.span(),
format!("Failed parsing format argument as expression: {}", &cap[1]).as_str(),
));
}
}
}
let path = syn::LitStr::new(
re.replace_all(path.value().as_str(), "{}")
.to_string()
.as_str(),
Span::call_site(),
);
if !fmt_args.is_empty() {
Ok(quote! {
format!(#path, #(#fmt_args),*)
})
} else {
Ok(quote! {
String::from(#path)
})
}
}
/// Generates the query method for generating query parameters.
///
/// If any fields are found with the [EndpointAttribute::Query] attribute they
/// are combined into a new struct and then serialized into a query string. If
/// the attribute is not found on any of the fields the query method is not
/// generated.
fn gen_query(
fields: &HashMap<EndpointAttribute, Vec<Field>>,
serde_attrs: &[Meta],
) -> proc_macro2::TokenStream {
let query_fields = fields.get(&EndpointAttribute::Query);
if let Some(v) = query_fields {
// Construct query function
let temp = parse::fields_to_struct(v, serde_attrs);
quote! {
fn query(&self) -> Result<Option<String>, ClientError> {
#temp
Ok(Some(build_query(&__temp)?))
}
}
} else {
quote! {}
}
}
/// Generates the body method for generating the request body.
///
/// The final result is determined by which attributes are present and/or
/// missing on the struct fields. The following order is respected:
///
/// * If a field is found with the [EndpointAttribute::Raw] attribute that field
/// is returned directly as the request body. The assumption is this field
/// will always be a [Vec<u8>].
/// * If any fields are found with the [EndpointAttribute::Body] attribute they
/// are combined into a new struct and then serialized into the request body
/// depending on the request type of the Endpoint.
/// * If neither of the above two conditions are true, and there are fields
/// found that don't have any attribute, those fields are combined into a new
/// struct and then serialized into the request body depending on the request
/// type of the Endpoint.
/// * If none of the above is true, the body method is not generated.
fn gen_body(
fields: &HashMap<EndpointAttribute, Vec<Field>>,
serde_attrs: &[Meta],
) -> Result<proc_macro2::TokenStream, Error> {
// Check for a raw field first
if let Some(v) = fields.get(&EndpointAttribute::Raw) {
if v.len() > 1 {
return Err(Error::new(v[1].span(), "May only mark one field as raw"));
}
let id = v[0].ident.clone().unwrap();
Ok(quote! {
fn body(&self) -> Result<Option<Vec<u8>>, ClientError>{
Ok(Some(self.#id.clone()))
}
})
// Then for any body fields
} else if let Some(v) = fields.get(&EndpointAttribute::Body) {
let temp = parse::fields_to_struct(v, serde_attrs);
Ok(quote! {
fn body(&self) -> Result<Option<Vec<u8>>, ClientError> {
#temp
Ok(Some(build_body(&__temp, Self::REQUEST_BODY_TYPE)?))
}
})
// Then for any untagged fields
} else if let Some(v) = fields.get(&EndpointAttribute::Untagged) {
let temp = parse::fields_to_struct(v, serde_attrs);
Ok(quote! {
fn body(&self) -> Result<Option<Vec<u8>>, ClientError> {
#temp
Ok(Some(build_body(&__temp, Self::REQUEST_BODY_TYPE)?))
}
})
// Leave it undefined if no body fields found
} else {
Ok(quote! {})
}
}
/// Generates `builder()` and `exec_*` helper methods for use with
/// `derive_builder`.
///
/// Adds an implementation to the base struct which provides a `builder` method
/// for returning instances of the Builder variant of the struct. This removes
/// the need to explicitly import it.
fn gen_builder(id: &Ident, generics: &Generics) -> proc_macro2::TokenStream {
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let builder_id: syn::Type = syn::parse_str(format!("{}Builder", id).as_str()).unwrap();
let builder_func: syn::Expr =
syn::parse_str(format!("{}Builder::default()", id).as_str()).unwrap();
quote! {
impl #impl_generics #id #ty_generics #where_clause {
pub fn builder() -> #builder_id #ty_generics {
#builder_func
}
}
}
}
/// Parses parameters passed into the `endpoint` attribute attached to the
/// struct.
fn parse_params(attr: &Meta) -> Result<Parameters, Error> {
// Parse the attribute as a key/value pair list
let kv = parse::attr_kv(attr)?;
// Create map from key/value pair list
let map = parse::to_map(&kv)?;
// Convert map to Parameters
params::Parameters::new(map)
}
/// Implements `Endpoint` on the provided struct.
fn endpoint_derive(s: synstructure::Structure) -> proc_macro2::TokenStream {
// Parse `endpoint` attributes attached to input struct
let attrs = match parse::attributes(&s.ast().attrs, ATTR_NAME) {
Ok(v) => v,
Err(e) => return e.into_tokens(),
};
// Parse `endpoint` attributes attached to input struct fields
let field_attrs = match parse::field_attributes(&s.ast().data) {
Ok(v) => v,
Err(e) => return e.into_tokens(),
};
// Verify attribute is present
if attrs.is_empty() {
return Error::new(
Span::call_site(),
format!(
"Deriving `{}` requires attaching an `{}` attribute",
MACRO_NAME, ATTR_NAME
)
.as_str(),
)
.into_tokens();
}
// Verify there's only one instance of the attribute present
if attrs.len() > 1 {
return Error::new(
Span::call_site(),
format!("Cannot define the {} attribute more than once", ATTR_NAME).as_str(),
)
.into_tokens();
}
// Parse endpoint attribute parameters
let params = match parse_params(&attrs[0]) {
Ok(v) => v,
Err(e) => return e.into_tokens(),
};
let path = params.path;
let method = params.method;
let response = params.response;
let request_type = params.request_type;
let response_type = params.response_type;
let id = &s.ast().ident;
// Find serde attributes
let serde_attrs = parse::attributes(&s.ast().attrs, "serde");
let serde_attrs = if let Ok(v) = serde_attrs {
v
} else {
Vec::<Meta>::new()
};
// Generate path string
let path = match gen_path(&path) {
Ok(a) => a,
Err(e) => return e.into_tokens(),
};
// Generate query function
let query = gen_query(&field_attrs, &serde_attrs);
// Generate body function
let body = match gen_body(&field_attrs, &serde_attrs) {
Ok(d) => d,
Err(e) => return e.into_tokens(),
};
// Generate helper functions when deriving Builder
let builder = match params.builder {
true => gen_builder(&s.ast().ident, &s.ast().generics),
false => quote! {},
};
// Capture generic information
let (impl_generics, ty_generics, where_clause) = s.ast().generics.split_for_impl();
// Generate Endpoint implementation
let const_name = format!("_DERIVE_Endpoint_FOR_{}", id);
let const_ident = Ident::new(const_name.as_str(), Span::call_site());
quote! {
const #const_ident: () = {
use rustify::__private::serde::Serialize;
use rustify::http::{build_body, build_query};
use rustify::client::Client;
use rustify::endpoint::Endpoint;
use rustify::enums::{RequestMethod, RequestType, ResponseType};
use rustify::errors::ClientError;
impl #impl_generics Endpoint for #id #ty_generics #where_clause {
type Response = #response;
const REQUEST_BODY_TYPE: RequestType = RequestType::#request_type;
const RESPONSE_BODY_TYPE: ResponseType = ResponseType::#response_type;
fn path(&self) -> String {
#path
}
fn method(&self) -> RequestMethod {
RequestMethod::#method
}
#query
#body
}
#builder
};
}
}
synstructure::decl_derive!([Endpoint, attributes(endpoint)] => endpoint_derive);