api-macro: started with some more documentation

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-03-23 15:55:18 +01:00
parent 176e208caf
commit ab8a6120ca
5 changed files with 37 additions and 1 deletions

View File

@ -1,3 +1,12 @@
//! `#[api]` macro and API schema core module.
//!
//! This contains the `Schema` type, which represents the schema items we have in our api library,
//! but as seen within the `#[api]` macro context.
//!
//! The main job here is to parse our `util::JSONObject` into a `Schema`.
//!
//! The handling of methods vs type definitions happens in their corresponding submodules.
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};
use failure::Error; use failure::Error;

View File

@ -1,3 +1,10 @@
//! Method handling.
//!
//! This has to perform quite a few things: infer types from parameters, deal with optional types
//! and defaults, expose parameter and return value schema to the public, and finally create the
//! wrapper function converting from a json Value hash to the parameters listed in the function
//! signature, while recognizing specially handling `RPCEnvironment` and `ApiMethod` parameters.
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};
use std::mem; use std::mem;

View File

@ -1,3 +1,15 @@
//! `#[api]` macro for `struct` types.
//!
//! This module implements struct handling.
//!
//! We distinguish between 3 types at the moment:
//! 1) Unit structs (`struct Foo;`),which don't do much really and aren't very useful for the API
//! currently)
//! 2) Newtypes (`struct Foo(T)`), a 1-tuple, which is supposed to be a wrapper for a type `T` and
//! therefore should implicitly deserialize/serialize to `T`. Currently we only support simple
//! types for which we "know" the schema type used in the API.
//! 3) Object structs (`struct Foo { ... }`), which declare an `ObjectSchema`.
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};

View File

@ -8,11 +8,17 @@ use failure::Error;
use proc_macro::TokenStream as TokenStream_1; use proc_macro::TokenStream as TokenStream_1;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
/// Our `format_err` macro replacement to enforce the inclusion of a `Span`.
/// The arrow variant takes a spanned syntax element, the comma variant expects an actual `Span` as
/// first parameter.
macro_rules! format_err { macro_rules! format_err {
($span:expr => $($msg:tt)*) => { syn::Error::new_spanned($span, format!($($msg)*)) }; ($span:expr => $($msg:tt)*) => { syn::Error::new_spanned($span, format!($($msg)*)) };
($span:expr, $($msg:tt)*) => { syn::Error::new($span, format!($($msg)*)) }; ($span:expr, $($msg:tt)*) => { syn::Error::new($span, format!($($msg)*)) };
} }
/// Our `bail` macro replacement to enforce the inclusion of a `Span`.
/// The arrow variant takes a spanned syntax element, the comma variant expects an actual `Span` as
/// first parameter.
macro_rules! bail { macro_rules! bail {
($span:expr => $($msg:tt)*) => { return Err(format_err!($span => $($msg)*).into()) }; ($span:expr => $($msg:tt)*) => { return Err(format_err!($span => $($msg)*).into()) };
($span:expr, $($msg:tt)*) => { return Err(format_err!($span, $($msg)*).into()) }; ($span:expr, $($msg:tt)*) => { return Err(format_err!($span, $($msg)*).into()) };
@ -22,6 +28,7 @@ mod api;
mod serde; mod serde;
mod util; mod util;
/// Handle errors by appending a `compile_error!()` macro invocation to the original token stream.
fn handle_error(mut item: TokenStream, data: Result<TokenStream, Error>) -> TokenStream { fn handle_error(mut item: TokenStream, data: Result<TokenStream, Error>) -> TokenStream {
match data { match data {
Ok(output) => output, Ok(output) => output,

View File

@ -526,7 +526,8 @@ pub fn is_option_type(ty: &syn::Type) -> Option<&syn::Type> {
None None
} }
/// `parse_macro_input!` expects a TokenStream_1 /// Helper to do basically what `parse_macro_input!` does, except the macro expects a
/// `TokenStream_1`, and we always have a `TokenStream` from `proc_macro2`.
pub struct AttrArgs { pub struct AttrArgs {
_paren_token: syn::token::Paren, _paren_token: syn::token::Paren,
pub args: Punctuated<syn::NestedMeta, Token![,]>, pub args: Punctuated<syn::NestedMeta, Token![,]>,