convex-macros
Macros to help make Convex in Rust nice
Installation
[]
= "0.0.4"
# Required by code this macro generates.
= "1.0.80"
= "0.6.0"
= "1.0.185"
= "1.0"
Usage
Create models using the same Convex validator syntax as your schema definition.
convex_model!;
This generates pub struct User {}
with various methods to convert from convex::Value
and to serde_json::Value
.
let user = from_convex_value
.expect;
assert_eq!;
assert_eq!;
assert_eq!;
Features
let user = User::from_convex_value(value)?;
to parse a value from Convex client.json!(user)
to serialize as json.- Discriminated unions are automatically handled.
- Helper functions for each union branch:
user.platform.as_2()?.username
.
Validator List
Validator Name | Rust Type | Notes |
---|---|---|
v.string() |
String |
|
v.id("tableName") |
String |
Ids are not validated against your tables |
v.null() |
() |
|
v.int64() |
i64 |
|
v.number() |
f64 |
|
v.boolean() |
bool |
|
v.optional(...) |
Option<T> |
|
v.union(...) |
Generated enum |
|
v.object({field: ...}) |
Generated struct |
Field names can't be rust keywords (like type ) |
v.bytes() |
not implemented | |
v.array(values) |
not implemented | |
v.any() |
not implemented |
Limitations
- This is experimental and may not be "production quality", use with caution.
v.bytes()
,v.array()
,v.any()
are not yet supported.- Field names must be valid Rust identifiers, so keywords like
type
cannot be a field name. Map it to_type
,kind
,t
, etc. - Union variant names are always named like:
Variant1
,Variant2
, etc. - The first acceptable union branch will be used if there are multiples that could validly parse data.
- This package generates code that expects
anyhow
,convex
,serde
, andserde_json
to be available. - Ints and Floats may be coerced into each other. Please test out your use cases and open an issue if you believe the behavior should change.
License
MIT