Commit Graph

46 Commits

Author SHA1 Message Date
Wolfgang Bumiller
183125b1cd macro: named struct: allow short form for fields
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 15:05:26 +02:00
Wolfgang Bumiller
c54afe1be4 macro: add spans to more errors
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 14:59:09 +02:00
Wolfgang Bumiller
f340968773 macro: ability to rename enum variants via an attribute
In PVE we have multiple different enum types: hyphenated,
all-caps, underscores.

By default our api-macro enums will convert CamelCase to
underscores, so we need a way to represent the rest:

enum AnEnum {
	CaseOne, // becomes "case_one",
	#[api(rename = "case-two")]
	CaseTwo, // "case-two",
	#[api(rename = "CASE_THREE")]
	CaseThree,
}

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 14:52:25 +02:00
Wolfgang Bumiller
4b3333aa5c macro: cleanup error handling, remove error module
syn::Error already covers this

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 14:41:27 +02:00
Wolfgang Bumiller
09eaef022f formatting cleanup
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 13:58:26 +02:00
Wolfgang Bumiller
43d69cb1aa macro: more error handling improvements
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 13:57:51 +02:00
Wolfgang Bumiller
9c9413ae56 macro: add types.rs, preserve Span for Expressions
Move the Name type into types.rs and make it hashable.
Expression::Object not contains an Object where the
hashmap's key is a Name and therefore preserves the Span of
all the keys for better error messages.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 13:54:52 +02:00
Wolfgang Bumiller
4803bfcb59 macro: add TypeInfo for enum types
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 11:26:57 +02:00
Wolfgang Bumiller
7510f3179c macro: more absolute paths
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 11:26:49 +02:00
Wolfgang Bumiller
a034fe3335 cleanup/remove old code
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 11:15:21 +02:00
Wolfgang Bumiller
7f704d12df macro: started basic enum support
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 11:14:58 +02:00
Wolfgang Bumiller
624afe28a4 prepare to parse enums
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 10:51:16 +02:00
Wolfgang Bumiller
da40267188 api-server, tools: formatting cleanup
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 10:47:56 +02:00
Wolfgang Bumiller
9654d1feeb macro: add spanned format_err equivalent
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 10:47:41 +02:00
Wolfgang Bumiller
145abf62a5 macro: helpers for error handling
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-18 10:30:40 +02:00
Wolfgang Bumiller
a1c8b00f17 example for better error messages
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-17 16:49:19 +02:00
Wolfgang Bumiller
bfbba4e605 tests: use an async block instead of an async closure
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-07-17 16:13:36 +02:00
Wolfgang Bumiller
205a9fc2aa api: split ApiHandler out of ApiMethodInfo
The method info part is not generic after all.
(Makes it easier to test different representations of
ApiHandler without having to adapt all the other methods as
well.)

Signed-off-by: Wolfgang Bumiller <wry.git@bumiller.com>
2019-07-01 09:48:33 +02:00
Wolfgang Bumiller
8a2d7a43e0 macro: cleanup: rename parse_object2 to parse_object
The old parse_object function is gone now.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-26 15:08:20 +02:00
Wolfgang Bumiller
c8e11115d2 api: replace handler() with call()
An `fn` type can be more annoying to produce in some generic
cases, and we haven't really needed it yet.

Signed-off-by: Wolfgang Bumiller <wry.git@bumiller.com>
2019-06-23 11:19:49 +02:00
Wolfgang Bumiller
b4378755f0 api-macro: remove old comment
Signed-off-by: Wolfgang Bumiller <wry.git@bumiller.com>
2019-06-23 11:10:32 +02:00
Wolfgang Bumiller
bd79dd8f02 api: make method body an associated type
This way we do not need to carry the body type into the CLI
router and can instead just require the body to be
Into<Bytes>.

This also makes more sense, because previously a method
could in theory implement multiple ApiMethodInfo types with
different bodies which seems pointless.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-19 15:16:40 +02:00
Wolfgang Bumiller
a5af6eedf7 formatting fixup
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-17 12:32:42 +02:00
Wolfgang Bumiller
c48e17fe26 macro: add cli property and remove specialization
Drop #!feature(specialization) in favor of having a `cli`
property for types to decide whether they are CLI
compatible.

The unconstrained_type! macro now has both ParseCli and
ParseCliFromStr in view, and requires one of the two to be
implemented for a type. This means that if a type implements
FromStr, it should "just work".

For types created without the help of the #[api] macro,
there's a shortcut to exclude a type from the CLI via
the no_cli_type!{typename} macro.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-17 12:31:29 +02:00
Wolfgang Bumiller
691af5cade add the ParseCli trait
In order to get parameters from the command line into the
API we need to get them into a json value, so that we can
pass it down the handler which deserializes them into their
real type and runs verifications.
For this we need to define how the type is going to be
converted into a json Value. We cannot simply use
Deserialize as that would for instance require quotes
around strings. So instead, we have a ParseCli trait, which
is a nop (direct serde_json::Value::String()) for string
types, and uses .parse() (the std::str::FromStr trait) for
everything else.

Currently this uses a `default fn` as an example of the
specialization feature, but I'll probably remove this and
use yet another mass-impl macro since there isn't much
activity on that feature's issue tracker. (The issue itself
seems to be outdated ...

Signed-off-by: Wolfgang Bumiller <wry.git@bumiller.com>
2019-06-17 12:31:29 +02:00
Wolfgang Bumiller
b243d9664d macro: implement minimum and maximum verification
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-13 14:29:09 +02:00
Wolfgang Bumiller
23d8e1d4e0 api: Parameter::type_info must be an fn
while filling the Parameter fields as &'static we cannot
really make use of the get_type_info() yet because it would
need to be a `const fn` (possible via #!feature(const_fn)),
so for now we store the method pointer until `const_fn` is
stable

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 16:43:24 +02:00
Wolfgang Bumiller
1a279f54a9 formatting fixup
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 16:33:27 +02:00
Wolfgang Bumiller
41884a622e macro: check function parameters
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 16:33:12 +02:00
Wolfgang Bumiller
11b09e578c router: parse an optional visibility
The 'pub' in `pub static ROUTER: ...`

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 15:31:19 +02:00
Wolfgang Bumiller
841799d882 fixup add missing question mark
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 09:48:30 +02:00
Wolfgang Bumiller
1c1c8530c5 formatting cleanup
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 09:48:30 +02:00
Wolfgang Bumiller
7db28deaff macro: body type support for router and api macros
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 09:48:30 +02:00
Wolfgang Bumiller
10451f65eb rewrite parse_object
This should support arbitrary expresion, so gobble up
elements up to the next comma and then run it through
syn::parse2.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 09:48:30 +02:00
Wolfgang Bumiller
1791c089b6 macro: need to allow specifying the body type now
TODO:
  - change parse_object to use Punctuated for the fields to
    support longer value types (so we can use actual types
    as fields)
  - allow adding a body type to methods
  - allow adding a body type to routers explicitly

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 09:48:30 +02:00
Wolfgang Bumiller
ff2767129f rename reload_timezone -> reload-timezone
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 09:48:30 +02:00
Wolfgang Bumiller
b82b14d947 macro: add wildcard matching to router macro
router!{
    /path/{parameter}*: {
        methods...
    }
}

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 09:48:30 +02:00
Wolfgang Bumiller
dcfa3ca9a2 allow async api methods to return Response<Bytes>
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 09:48:30 +02:00
Wolfgang Bumiller
3f7e4c0eda router: add remaining missing ::proxmox::api:: prefixes
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 09:48:30 +02:00
Wolfgang Bumiller
2b577c9c17 cleanup
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 09:48:30 +02:00
Wolfgang Bumiller
1993460d4a allow integer literals as subdirectories
eg: /api/2/ should be legal

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 09:48:30 +02:00
Wolfgang Bumiller
f3e2e672be use 'quote' in router macro
get rid of a lot of unreadable TokenTree entries

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 09:48:30 +02:00
Wolfgang Bumiller
7155689724 fix missing proxmox_api -> proxmox::api replacement
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-12 09:48:30 +02:00
Wolfgang Bumiller
cd23e0956a macro: switch to using proxmox::api
and fixup a `bail!` -> `::failure::bail!`

Signed-off-by: Wolfgang Bumiller <wry.git@bumiller.com>
2019-06-12 09:48:30 +02:00
Wolfgang Bumiller
3dd6cd3fe0 formatting fixup
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-07 12:03:17 +02:00
Wolfgang Bumiller
b5c05fc85c import proxmox-api-macro crate
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-06-06 15:25:47 +02:00