api-macro: more documentation

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-01-08 11:23:48 +01:00
parent 5b652dbad8
commit 0f630a4cb8
2 changed files with 85 additions and 0 deletions

View File

@ -132,6 +132,77 @@ fn router_do(item: TokenStream) -> Result<TokenStream, Error> {
...
}
```
The `#[api]` macro can also be used on type declarations to create schemas for structs to be
used instead of accessing json values via string indexing.
For a simple struct, the schema can be left empty and will be completely derived from the
information available in rust:
```no_run
# use proxmox_api_macro::api;
# use serde::{Deserialize, Serialize};
#[api]
#[derive(Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
/// An example of a struct with renamed fields.
pub struct RenamedStruct {
/// A test string.
test_string: String,
/// An optional auto-derived value for testing:
#[serde(rename = "SomeOther")]
another: Option<String>,
}
```
This will produce the following schema:
```no_run
# struct RenamedStruct;
impl RenamedStruct {
pub const API_SCHEMA: &'static ::proxmox::api::schema::Schema =
&::proxmox::api::schema::ObjectSchema::new(
"An example of a struct with renamed fields.",
&[
(
"test-string",
false,
&::proxmox::api::schema::StringSchema::new("A test string.").schema(),
),
(
"SomeOther",
true,
&::proxmox::api::schema::StringSchema::new(
"An optional auto-derived value for testing:",
)
.schema(),
),
],
)
.schema();
}
```
Note that the schema itself contains the already renamed fields!
```
# use proxmox_api_macro::api;
# use serde::{Deserialize, Serialize};
#[api(
properties: {
"A-RENAMED-FIELD": { description: "Some description.", },
"another": { description: "Some description.", },
},
)]
#[derive(Deserialize, Serialize)]
#[serde(rename_all = "SCREAMING-KEBAB-CASE")]
/// Some Description.
pub struct SomeStruct {
a_renamed_field: String,
#[serde(rename = "another")]
AND_MORE: String,
}
```
*/
#[proc_macro_attribute]
pub fn api(attr: TokenStream_1, item: TokenStream_1) -> TokenStream_1 {

View File

@ -163,3 +163,17 @@ fn string_check_schema_test() {
assert_eq!(TEST_METHOD, API_METHOD_STRING_CHECK);
}
#[api(
properties: {
"a-field": {
description: "Some description.",
},
},
)]
#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
/// Some Description.
pub struct RenamedAndDescribed {
a_field: String,
}