generated from jmgilman/rust-template-lib
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds base structure for handling endpoints and implements support for…
… KV store
- Loading branch information
Showing
17 changed files
with
780 additions
and
19 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/consulrs_derive/target | ||
/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
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,19 @@ | ||
[package] | ||
name = "consulrs_derive" | ||
version = "0.1.0" | ||
authors = ["Joshua Gilman <joshuagilman@gmail.com>"] | ||
description = "A derive macro for implementing query options for Consul endpoints" | ||
license = "MIT" | ||
repository = "https://github.com/jmgilman/consulrs" | ||
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" |
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,31 @@ | ||
use proc_macro2::Span; | ||
|
||
/// The general error object returned by functions in this crate. | ||
/// | ||
/// The error object can be directly converted from a [syn::Error] as well as | ||
/// be converted directly into a [proc_macro2::TokenStream] to be returned to | ||
/// the compiler. | ||
#[derive(Debug)] | ||
pub struct Error(proc_macro2::TokenStream); | ||
|
||
impl Error { | ||
/// Returns a new instance of [Error] using the given [Span] and message. | ||
/// | ||
/// This uses [quote_spanned!] in order to provide more accurate information | ||
/// to the compiler about the exact location of the error. | ||
pub fn new(span: Span, message: &str) -> Error { | ||
Error(quote_spanned! { span => | ||
compile_error!(#message); | ||
}) | ||
} | ||
|
||
pub 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()) | ||
} | ||
} |
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,43 @@ | ||
#[macro_use] | ||
extern crate synstructure; | ||
extern crate proc_macro; | ||
|
||
mod error; | ||
|
||
use error::Error; | ||
use proc_macro2::Span; | ||
|
||
const FIELD_NAME: &str = "features"; | ||
|
||
/// Returns field names of the given struct. | ||
fn fields(data: &syn::DataStruct) -> Vec<String> { | ||
data.fields | ||
.iter() | ||
.map(|f| f.ident.clone().unwrap().to_string()) | ||
.collect() | ||
} | ||
|
||
fn endpoint_derive(s: synstructure::Structure) -> proc_macro2::TokenStream { | ||
// Validate the required field exists | ||
if let syn::Data::Struct(data) = &s.ast().data { | ||
let fields = fields(data); | ||
if !fields.contains(&FIELD_NAME.into()) { | ||
return Error::new(data.struct_token.span, "Missing required `features` field") | ||
.into_tokens(); | ||
} | ||
} else { | ||
return Error::new(Span::call_site(), "May only be used on with structs").into_tokens(); | ||
} | ||
|
||
s.gen_impl(quote! { | ||
use crate::api::features::{FeaturedEndpoint, Features}; | ||
|
||
gen impl FeaturedEndpoint for @Self { | ||
fn features(&self) -> Option<Features> { | ||
self.features.clone() | ||
} | ||
} | ||
}) | ||
} | ||
|
||
synstructure::decl_derive!([QueryEndpoint] => 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
Oops, something went wrong.