api-macro: formatting fixups

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-07-16 14:13:13 +02:00
parent debd9f9f4f
commit a4abaa8b17
6 changed files with 48 additions and 53 deletions

View File

@ -193,7 +193,7 @@ impl Schema {
fn find_schema_property(&self, key: &str) -> Option<&syn::Expr> { fn find_schema_property(&self, key: &str) -> Option<&syn::Expr> {
for prop in &self.properties { for prop in &self.properties {
if prop.0 == key { if prop.0 == key {
return Some(&prop.1) return Some(&prop.1);
} }
} }
None None
@ -201,7 +201,8 @@ impl Schema {
pub fn add_default_property(&mut self, key: &str, value: syn::Expr) { pub fn add_default_property(&mut self, key: &str, value: syn::Expr) {
if self.find_schema_property(key).is_none() { if self.find_schema_property(key).is_none() {
self.properties.push((Ident::new(key, Span::call_site()), value)); self.properties
.push((Ident::new(key, Span::call_site()), value));
} }
} }
} }

View File

@ -45,7 +45,7 @@ pub fn handle_method(mut attribs: JSONObject, mut func: syn::ItemFn) -> Result<T
let permission = access.permission; let permission = access.permission;
let description = match access.description { let description = match access.description {
Some(desc) => quote_spanned! { desc.span() => Some(#desc) }, Some(desc) => quote_spanned! { desc.span() => Some(#desc) },
None => quote_spanned! { access.span => None } None => quote_spanned! { access.span => None },
}; };
quote_spanned! { access.span => quote_spanned! { access.span =>
.access(#description, #permission) .access(#description, #permission)

View File

@ -34,7 +34,7 @@ pub fn handle_struct(attribs: JSONObject, stru: syn::ItemStruct) -> Result<Token
fields.paren_token.span, fields.paren_token.span,
"api macro does not support tuple structs" "api macro does not support tuple structs"
), ),
syn::Fields::Named(_) => handle_regular_struct(attribs, stru) syn::Fields::Named(_) => handle_regular_struct(attribs, stru),
} }
} }
@ -47,10 +47,7 @@ fn get_struct_description(schema: &mut Schema, stru: &syn::ItemStruct) -> Result
Ok(()) Ok(())
} }
fn handle_unit_struct( fn handle_unit_struct(attribs: JSONObject, stru: syn::ItemStruct) -> Result<TokenStream, Error> {
attribs: JSONObject,
stru: syn::ItemStruct,
) -> Result<TokenStream, Error> {
// unit structs, not sure about these? // unit structs, not sure about these?
let mut schema: Schema = if attribs.is_empty() { let mut schema: Schema = if attribs.is_empty() {
@ -83,10 +80,7 @@ fn finish_schema(
}) })
} }
fn handle_newtype_struct( fn handle_newtype_struct(attribs: JSONObject, stru: syn::ItemStruct) -> Result<TokenStream, Error> {
attribs: JSONObject,
stru: syn::ItemStruct,
) -> Result<TokenStream, Error> {
// Ideally we could clone the contained item's schema, but this is "hard", so for now we assume // Ideally we could clone the contained item's schema, but this is "hard", so for now we assume
// the contained type is a simple type. // the contained type is a simple type.
// //
@ -104,7 +98,11 @@ fn handle_newtype_struct(
_ => panic!("handle_unit_struct on non-unit struct"), _ => panic!("handle_unit_struct on non-unit struct"),
}; };
// this is also part of `handle_struct()`'s verification! // this is also part of `handle_struct()`'s verification!
assert_eq!(fields.len(), 1, "handle_unit_struct needs a struct with exactly 1 field"); assert_eq!(
fields.len(),
1,
"handle_unit_struct needs a struct with exactly 1 field"
);
// Now infer the type information: // Now infer the type information:
util::infer_type(&mut schema, &fields[0].ty)?; util::infer_type(&mut schema, &fields[0].ty)?;
@ -115,10 +113,7 @@ fn handle_newtype_struct(
finish_schema(schema, &stru, &stru.ident) finish_schema(schema, &stru, &stru.ident)
} }
fn handle_regular_struct( fn handle_regular_struct(attribs: JSONObject, stru: syn::ItemStruct) -> Result<TokenStream, Error> {
attribs: JSONObject,
stru: syn::ItemStruct,
) -> Result<TokenStream, Error> {
let mut schema: Schema = if attribs.is_empty() { let mut schema: Schema = if attribs.is_empty() {
Schema::empty_object(Span::call_site()) Schema::empty_object(Span::call_site())
} else { } else {

View File

@ -69,8 +69,7 @@ pub struct AnI64(i64);
#[test] #[test]
fn test_an_i64_schema() { fn test_an_i64_schema() {
const TEST_SCHEMA: ::proxmox::api::schema::Schema = const TEST_SCHEMA: ::proxmox::api::schema::Schema =
::proxmox::api::schema::IntegerSchema::new("An i64: this is left unlimited.") ::proxmox::api::schema::IntegerSchema::new("An i64: this is left unlimited.").schema();
.schema();
assert_eq!(TEST_SCHEMA, AnI64::API_SCHEMA); assert_eq!(TEST_SCHEMA, AnI64::API_SCHEMA);
} }

View File

@ -77,11 +77,13 @@ fn test_invocations() {
.expect("func with option should work"); .expect("func with option should work");
assert_eq!(value, true); assert_eq!(value, true);
let value = api_function_test_option(json!({"value": false}), &API_METHOD_TEST_OPTION, &mut env) let value =
api_function_test_option(json!({"value": false}), &API_METHOD_TEST_OPTION, &mut env)
.expect("func with option should work"); .expect("func with option should work");
assert_eq!(value, false); assert_eq!(value, false);
let value = api_function_test_default_macro(json!({}), &API_METHOD_TEST_DEFAULT_MACRO, &mut env) let value =
api_function_test_default_macro(json!({}), &API_METHOD_TEST_DEFAULT_MACRO, &mut env)
.expect("func with option should work"); .expect("func with option should work");
assert_eq!(value, 5); assert_eq!(value, 5);
} }

View File

@ -84,8 +84,7 @@ pub struct RenamedStruct {
#[test] #[test]
fn renamed_struct() { fn renamed_struct() {
const TEST_SCHEMA: ::proxmox::api::schema::Schema = const TEST_SCHEMA: ::proxmox::api::schema::Schema = ::proxmox::api::schema::ObjectSchema::new(
::proxmox::api::schema::ObjectSchema::new(
"An example of a struct with renamed fields.", "An example of a struct with renamed fields.",
&[ &[
( (
@ -124,8 +123,7 @@ pub enum Selection {
#[test] #[test]
fn selection_test() { fn selection_test() {
const TEST_SCHEMA: ::proxmox::api::schema::Schema = const TEST_SCHEMA: ::proxmox::api::schema::Schema = ::proxmox::api::schema::StringSchema::new(
::proxmox::api::schema::StringSchema::new(
"A selection of either \'onekind\', \'another-kind\' or \'selection-number-three\'.", "A selection of either \'onekind\', \'another-kind\' or \'selection-number-three\'.",
) )
.format(&::proxmox::api::schema::ApiStringFormat::Enum(&[ .format(&::proxmox::api::schema::ApiStringFormat::Enum(&[