-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ce6515b
Showing
13 changed files
with
587 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/target | ||
/rustify_derive/target | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "rustify" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
reqwest = { version = "0.11.4", features = ["blocking", "json"] } | ||
rustify_derive = { path = "rustify_derive" } | ||
serde = { version = "1.0.127", features = ["derive"] } | ||
serde_json = "1.0.66" | ||
thiserror = "1.0.26" | ||
url = "2.2.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Joshua Gilman | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
A Rust crate which provides an abstraction layer over HTTP REST endpoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "rustify_derive" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
syn = "1.0" | ||
quote = "1.0" | ||
synstructure = "0.12.5" | ||
proc-macro2 = "1.0.28" | ||
regex = "1.5.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,269 @@ | ||
#[macro_use] | ||
extern crate synstructure; | ||
|
||
extern crate proc_macro; | ||
|
||
use std::ops::Deref; | ||
|
||
use proc_macro2::Span; | ||
use quote::{quote, ToTokens}; | ||
use regex::Regex; | ||
use syn::{self, spanned::Spanned}; | ||
|
||
const MACRO_NAME: &str = "Endpoint"; | ||
const ATTR_NAME: &str = "endpoint"; | ||
const DATA_ATTR_NAME: &str = "data"; | ||
|
||
#[derive(Debug)] | ||
struct Error(proc_macro2::TokenStream); | ||
|
||
impl Error { | ||
fn new(span: Span, message: &str) -> Error { | ||
Error(quote_spanned! { span => | ||
compile_error!(#message); | ||
}) | ||
} | ||
|
||
fn into_tokens(self) -> proc_macro2::TokenStream { | ||
self.0 | ||
} | ||
} | ||
|
||
impl From<syn::Error> for Error { | ||
fn from(e: syn::Error) -> Error { | ||
Error(e.to_compile_error()) | ||
} | ||
} | ||
|
||
#[derive(Default, Debug)] | ||
struct Parameters { | ||
path: Option<syn::LitStr>, | ||
method: Option<syn::Expr>, | ||
result: Option<syn::Type>, | ||
} | ||
|
||
fn parse_attr(meta: &syn::Meta) -> Result<Parameters, Error> { | ||
let mut params = Parameters::default(); | ||
if let syn::Meta::List(l) = meta { | ||
// Verify the attribute list isn't empty | ||
if l.nested.is_empty() { | ||
return Err(Error::new( | ||
meta.span(), | ||
format!( | ||
"The `{}` attribute must be a list of name/value pairs", | ||
ATTR_NAME | ||
) | ||
.as_str(), | ||
)); | ||
} | ||
|
||
// Collect name/value arguments | ||
let mut args: Vec<&syn::MetaNameValue> = Vec::new(); | ||
for nm in l.nested.iter() { | ||
if let syn::NestedMeta::Meta(m) = nm { | ||
if let syn::Meta::NameValue(nv) = m { | ||
args.push(nv); | ||
} else { | ||
return Err(Error::new( | ||
m.span(), | ||
format!( | ||
"The `{}` attribute must only contain name/value pairs", | ||
ATTR_NAME | ||
) | ||
.as_str(), | ||
)); | ||
} | ||
} else { | ||
return Err(Error::new( | ||
nm.span(), | ||
"The `action` attribute must not contain any literals", | ||
)); | ||
} | ||
} | ||
|
||
// Extract arguments | ||
for arg in args { | ||
if let syn::Lit::Str(val) = &arg.lit { | ||
match arg.path.get_ident().unwrap().to_string().as_str() { | ||
"path" => { | ||
params.path = Some(val.deref().clone()); | ||
} | ||
"method" => { | ||
params.method = Some(val.deref().clone().parse().map_err(|_| { | ||
Error::new(arg.lit.span(), "Unable to parse value into expression") | ||
})?); | ||
} | ||
"result" => { | ||
params.result = Some(val.deref().clone().parse().map_err(|_| { | ||
Error::new(arg.lit.span(), "Unable to parse value into expression") | ||
})?); | ||
} | ||
_ => { | ||
return Err(Error::new(arg.span(), "Unsupported argument")); | ||
} | ||
} | ||
} else { | ||
return Err(Error::new(arg.span(), "Invalid value for argument")); | ||
} | ||
} | ||
} else { | ||
return Err(Error::new( | ||
meta.span(), | ||
format!( | ||
"The `{}` attribute must be a list of key/value pairs", | ||
ATTR_NAME | ||
) | ||
.as_str(), | ||
)); | ||
} | ||
Ok(params) | ||
} | ||
|
||
fn gen_action(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) | ||
}) | ||
} | ||
} | ||
|
||
fn endpoint_derive(s: synstructure::Structure) -> proc_macro2::TokenStream { | ||
let mut found_attr = false; | ||
let mut params = Parameters::default(); | ||
for attr in &s.ast().attrs { | ||
match attr.parse_meta() { | ||
Ok(meta) => { | ||
if meta.path().is_ident(ATTR_NAME) { | ||
found_attr = true; | ||
match parse_attr(&meta) { | ||
Ok(p) => { | ||
params = p; | ||
} | ||
Err(e) => return e.into_tokens(), | ||
} | ||
} | ||
} | ||
Err(e) => return e.to_compile_error(), | ||
} | ||
} | ||
|
||
if !found_attr { | ||
return Error::new( | ||
Span::call_site(), | ||
format!( | ||
"Must supply the `{}` attribute when deriving `{}`", | ||
ATTR_NAME, MACRO_NAME | ||
) | ||
.as_str(), | ||
) | ||
.into_tokens(); | ||
} | ||
|
||
// Find data attribute | ||
let mut field_name: Option<proc_macro2::TokenStream> = None; | ||
let mut ty: Option<proc_macro2::TokenStream> = None; | ||
if let syn::Data::Struct(data) = &s.ast().data { | ||
for field in data.fields.iter() { | ||
if &field.ident.clone().unwrap().to_string() == DATA_ATTR_NAME { | ||
field_name = Some(field.ident.to_token_stream()); | ||
ty = Some(field.ty.to_token_stream()); | ||
} else { | ||
for attr in field.attrs.iter() { | ||
if attr.path.is_ident(DATA_ATTR_NAME) { | ||
field_name = Some(field.ident.to_token_stream()); | ||
ty = Some(field.ty.to_token_stream()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
let mut data_empty = false; | ||
let data_type = match ty { | ||
Some(t) => t, | ||
None => { | ||
data_empty = true; | ||
quote! {EmptyEndpointData} | ||
} | ||
}; | ||
|
||
let data_fn = match data_empty { | ||
true => quote! {None}, | ||
false => quote! {Some(&self.#field_name)}, | ||
}; | ||
|
||
// Parse arguments | ||
let path = match params.path { | ||
Some(p) => p, | ||
None => { | ||
return Error::new(Span::call_site(), "Missing required `path` argument").into_tokens() | ||
} | ||
}; | ||
let method = match params.method { | ||
Some(m) => m, | ||
None => match data_empty { | ||
true => syn::parse_str("RequestType::GET").unwrap(), | ||
false => syn::parse_str("RequestType::POST").unwrap(), | ||
}, | ||
}; | ||
let result = match params.result { | ||
Some(r) => r, | ||
None => syn::parse_str("EmptyEndpointResult").unwrap(), | ||
}; | ||
|
||
// Hacky variable substitution | ||
let action = match gen_action(&path) { | ||
Ok(a) => a, | ||
Err(e) => return e.into_tokens(), | ||
}; | ||
|
||
// Generate Endpoint implementation | ||
s.gen_impl(quote! { | ||
use ::rustify::endpoint::{Endpoint, EmptyEndpointData, EmptyEndpointResult}; | ||
use ::rustify::enums::RequestType; | ||
|
||
gen impl Endpoint for @Self { | ||
type RequestData = #data_type; | ||
type Response = #result; | ||
|
||
fn action(&self) -> String { | ||
#action | ||
} | ||
|
||
fn method(&self) -> RequestType { | ||
#method | ||
} | ||
|
||
fn data(&self) -> Option<&Self::RequestData> { | ||
#data_fn | ||
} | ||
} | ||
}) | ||
} | ||
|
||
synstructure::decl_derive!([Endpoint, attributes(endpoint, data)] => endpoint_derive); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use serde::{de::DeserializeOwned, Serialize}; | ||
use url::Url; | ||
|
||
use crate::{endpoint::Endpoint, enums::RequestType, errors::ClientError}; | ||
|
||
const HTTP_SUCCESS_CODES: [u16; 2] = [200, 204]; | ||
pub trait Client { | ||
fn send<S: Serialize>( | ||
&self, | ||
req: crate::client::Request<S>, | ||
) -> Result<crate::client::Response, ClientError>; | ||
|
||
fn base(&self) -> &str; | ||
|
||
fn execute<E: Endpoint, D: DeserializeOwned>( | ||
&self, | ||
endpoint: &E, | ||
) -> Result<Option<D>, ClientError> { | ||
let url = endpoint.build_url(self.base())?; | ||
let method = endpoint.method(); | ||
let data = endpoint.data(); | ||
let response = self.send(crate::client::Request { url, method, data })?; | ||
|
||
// Check response | ||
if !HTTP_SUCCESS_CODES.contains(&response.code) { | ||
return Err(ClientError::ServerResponseError { | ||
url: response.url.to_string(), | ||
code: response.code, | ||
content: response.content.clone(), | ||
}); | ||
} | ||
|
||
// Check for response content | ||
if response.content.is_empty() { | ||
return Ok(None); | ||
} | ||
|
||
// Parse response content | ||
serde_json::from_str(response.content.as_str()).map_err(|e| { | ||
ClientError::ResponseParseError { | ||
source: Box::new(e), | ||
content: response.content.clone(), | ||
} | ||
}) | ||
} | ||
} | ||
|
||
pub struct Request<'a, S: Serialize> { | ||
pub url: Url, | ||
pub method: RequestType, | ||
pub data: Option<&'a S>, | ||
} | ||
|
||
pub struct Response { | ||
pub url: Url, | ||
pub code: u16, | ||
pub content: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod reqwest; |
Oops, something went wrong.