api-macro: formatting fixups
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
debd9f9f4f
commit
a4abaa8b17
@ -193,7 +193,7 @@ impl Schema {
|
||||
fn find_schema_property(&self, key: &str) -> Option<&syn::Expr> {
|
||||
for prop in &self.properties {
|
||||
if prop.0 == key {
|
||||
return Some(&prop.1)
|
||||
return Some(&prop.1);
|
||||
}
|
||||
}
|
||||
None
|
||||
@ -201,7 +201,8 @@ impl Schema {
|
||||
|
||||
pub fn add_default_property(&mut self, key: &str, value: syn::Expr) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ pub fn handle_method(mut attribs: JSONObject, mut func: syn::ItemFn) -> Result<T
|
||||
let permission = access.permission;
|
||||
let description = match access.description {
|
||||
Some(desc) => quote_spanned! { desc.span() => Some(#desc) },
|
||||
None => quote_spanned! { access.span => None }
|
||||
None => quote_spanned! { access.span => None },
|
||||
};
|
||||
quote_spanned! { access.span =>
|
||||
.access(#description, #permission)
|
||||
|
@ -34,7 +34,7 @@ pub fn handle_struct(attribs: JSONObject, stru: syn::ItemStruct) -> Result<Token
|
||||
fields.paren_token.span,
|
||||
"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(())
|
||||
}
|
||||
|
||||
fn handle_unit_struct(
|
||||
attribs: JSONObject,
|
||||
stru: syn::ItemStruct,
|
||||
) -> Result<TokenStream, Error> {
|
||||
fn handle_unit_struct(attribs: JSONObject, stru: syn::ItemStruct) -> Result<TokenStream, Error> {
|
||||
// unit structs, not sure about these?
|
||||
|
||||
let mut schema: Schema = if attribs.is_empty() {
|
||||
@ -83,10 +80,7 @@ fn finish_schema(
|
||||
})
|
||||
}
|
||||
|
||||
fn handle_newtype_struct(
|
||||
attribs: JSONObject,
|
||||
stru: syn::ItemStruct,
|
||||
) -> Result<TokenStream, Error> {
|
||||
fn handle_newtype_struct(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
|
||||
// the contained type is a simple type.
|
||||
//
|
||||
@ -104,7 +98,11 @@ fn handle_newtype_struct(
|
||||
_ => panic!("handle_unit_struct on non-unit struct"),
|
||||
};
|
||||
// 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:
|
||||
util::infer_type(&mut schema, &fields[0].ty)?;
|
||||
@ -115,10 +113,7 @@ fn handle_newtype_struct(
|
||||
finish_schema(schema, &stru, &stru.ident)
|
||||
}
|
||||
|
||||
fn handle_regular_struct(
|
||||
attribs: JSONObject,
|
||||
stru: syn::ItemStruct,
|
||||
) -> Result<TokenStream, Error> {
|
||||
fn handle_regular_struct(attribs: JSONObject, stru: syn::ItemStruct) -> Result<TokenStream, Error> {
|
||||
let mut schema: Schema = if attribs.is_empty() {
|
||||
Schema::empty_object(Span::call_site())
|
||||
} else {
|
||||
|
@ -69,8 +69,7 @@ pub struct AnI64(i64);
|
||||
#[test]
|
||||
fn test_an_i64_schema() {
|
||||
const TEST_SCHEMA: ::proxmox::api::schema::Schema =
|
||||
::proxmox::api::schema::IntegerSchema::new("An i64: this is left unlimited.")
|
||||
.schema();
|
||||
::proxmox::api::schema::IntegerSchema::new("An i64: this is left unlimited.").schema();
|
||||
|
||||
assert_eq!(TEST_SCHEMA, AnI64::API_SCHEMA);
|
||||
}
|
||||
|
@ -77,11 +77,13 @@ fn test_invocations() {
|
||||
.expect("func with option should work");
|
||||
assert_eq!(value, true);
|
||||
|
||||
let value = api_function_test_option(json!({"value": false}), &API_METHOD_TEST_OPTION, &mut env)
|
||||
.expect("func with option should work");
|
||||
let value =
|
||||
api_function_test_option(json!({"value": false}), &API_METHOD_TEST_OPTION, &mut env)
|
||||
.expect("func with option should work");
|
||||
assert_eq!(value, false);
|
||||
|
||||
let value = api_function_test_default_macro(json!({}), &API_METHOD_TEST_DEFAULT_MACRO, &mut env)
|
||||
.expect("func with option should work");
|
||||
let value =
|
||||
api_function_test_default_macro(json!({}), &API_METHOD_TEST_DEFAULT_MACRO, &mut env)
|
||||
.expect("func with option should work");
|
||||
assert_eq!(value, 5);
|
||||
}
|
||||
|
@ -84,26 +84,25 @@ pub struct RenamedStruct {
|
||||
|
||||
#[test]
|
||||
fn renamed_struct() {
|
||||
const TEST_SCHEMA: ::proxmox::api::schema::Schema =
|
||||
::proxmox::api::schema::ObjectSchema::new(
|
||||
"An example of a struct with renamed fields.",
|
||||
&[
|
||||
(
|
||||
"SomeOther",
|
||||
true,
|
||||
&::proxmox::api::schema::StringSchema::new(
|
||||
"An optional auto-derived value for testing:",
|
||||
)
|
||||
.schema(),
|
||||
),
|
||||
(
|
||||
"test-string",
|
||||
false,
|
||||
&::proxmox::api::schema::StringSchema::new("A test string.").schema(),
|
||||
),
|
||||
],
|
||||
)
|
||||
.schema();
|
||||
const TEST_SCHEMA: ::proxmox::api::schema::Schema = ::proxmox::api::schema::ObjectSchema::new(
|
||||
"An example of a struct with renamed fields.",
|
||||
&[
|
||||
(
|
||||
"SomeOther",
|
||||
true,
|
||||
&::proxmox::api::schema::StringSchema::new(
|
||||
"An optional auto-derived value for testing:",
|
||||
)
|
||||
.schema(),
|
||||
),
|
||||
(
|
||||
"test-string",
|
||||
false,
|
||||
&::proxmox::api::schema::StringSchema::new("A test string.").schema(),
|
||||
),
|
||||
],
|
||||
)
|
||||
.schema();
|
||||
|
||||
assert_eq!(TEST_SCHEMA, RenamedStruct::API_SCHEMA);
|
||||
}
|
||||
@ -124,16 +123,15 @@ pub enum Selection {
|
||||
|
||||
#[test]
|
||||
fn selection_test() {
|
||||
const TEST_SCHEMA: ::proxmox::api::schema::Schema =
|
||||
::proxmox::api::schema::StringSchema::new(
|
||||
"A selection of either \'onekind\', \'another-kind\' or \'selection-number-three\'.",
|
||||
)
|
||||
.format(&::proxmox::api::schema::ApiStringFormat::Enum(&[
|
||||
EnumEntry::new("onekind", "The first kind."),
|
||||
EnumEntry::new("another-kind", "Some other kind."),
|
||||
EnumEntry::new("selection-number-three", "And yet another."),
|
||||
]))
|
||||
.schema();
|
||||
const TEST_SCHEMA: ::proxmox::api::schema::Schema = ::proxmox::api::schema::StringSchema::new(
|
||||
"A selection of either \'onekind\', \'another-kind\' or \'selection-number-three\'.",
|
||||
)
|
||||
.format(&::proxmox::api::schema::ApiStringFormat::Enum(&[
|
||||
EnumEntry::new("onekind", "The first kind."),
|
||||
EnumEntry::new("another-kind", "Some other kind."),
|
||||
EnumEntry::new("selection-number-three", "And yet another."),
|
||||
]))
|
||||
.schema();
|
||||
|
||||
assert_eq!(TEST_SCHEMA, Selection::API_SCHEMA);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user