3 releases
| 0.0.3 | Oct 25, 2025 |
|---|---|
| 0.0.2 | Sep 24, 2025 |
| 0.0.1 | Sep 23, 2025 |
#324 in Procedural macros
2,750 downloads per month
Used in vbare-compiler
26KB
549 lines
bare_gen provides a simple function that generates Rust types from BARE schema files.
Generated types implicitly implement serde::Serialize and serde::Deserialize, as serde_bare
is used to handle encoding and decoding. Please see
serde_bare's documentation for information on how
the Rust data model maps to the BARE data model.
To use this macro, define a BARE schema file and populate it with type declarations.
For example:
// schema.bare
type PublicKey data[128]
type Time str # ISO 8601
type Department enum {
ACCOUNTING
ADMINISTRATION
CUSTOMER_SERVICE
DEVELOPMENT
JSMITH = 99
}
type Address list<str>[4] # street, city, state, country
type Customer struct {
name: str
email: str
address: Address
orders: list<struct {
orderId: i64
quantity: i32
}>
metadata: map<str><data>
}
type Employee struct {
name: str
email: str
address: Address
department: Department
hireDate: Time
publicKey: optional<PublicKey>
metadata: map<str><data>
}
type TerminatedEmployee void
type Person union {Customer | Employee | TerminatedEmployee}
Then, within a Rust source file:
bare_gen::bare_schema("schema.bare", bare_gen::Config::default()); // TokenStream
BARE => Rust Data Mapping
In most areas, the BARE data model maps cleanly to a Rust representation. Unless otherwise
specified, the most obvious Rust data type is generated from a given BARE type. For example,
a BARE option<type> is mapped to Rust's Option<type>, BARE unions and enums are mapped to
Rust enums. See below for opinions that this crate has around data types that do not map
as cleanly or require additional explanation.
Maps
BARE maps are interpreted as std::collections::HashMap<K, V> in Rust by default. Set
Config::use_hashable_map to true to emit rivet_util::serde::HashableMap<K, V> instead.
Variable Length Integers
The variable uint and int types are mapped to serde_bare::UInt and serde_bare::Int
respectively. These types wrap u64 and i64 (the largest possible sized values stored in BARE
variable length integers).
Arrays that have 32 or less elements are mapped directly as Rust arrays, while BARE arrays with
more than 32 elements are converted into Vec<T>.
TODO: This package is a fork, need to find the package & provide correct attribution
Dependencies
~2.1–3MB
~63K SLoC