Skip to content

Commit a7ce0c8

Browse files
committed
revert vbare-gen
1 parent e08ca55 commit a7ce0c8

File tree

2 files changed

+62
-77
lines changed

2 files changed

+62
-77
lines changed

rust/vbare-gen/src/lib.rs

Lines changed: 46 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ Then, within a Rust source file:
5757
5858
```ignore
5959
60-
// Generate using the default config
61-
let config = bare_gen::Config { use_hashable_map: true };
62-
bare_gen::bare_schema(std::path::Path::new("schema.bare"), config); // TokenStream
60+
bare_gen::bare_schema("schema.bare"); // TokenStream
6361
6462
```
6563
@@ -73,11 +71,8 @@ as cleanly or require additional explanation.
7371
7472
## Maps
7573
76-
BARE maps are interpreted as either `HashMap<K, V>` or `HashableMap<K, V>` in Rust.
77-
This is controlled via the `Config` passed to `bare_schema`.
78-
79-
- When `Config { use_hashable_map: true }`, maps use `rivet_util::serde::HashableMap`.
80-
- When `Config { use_hashable_map: false }`, maps use `std::collections::HashMap`.
74+
BARE maps are interpreted as `HashMap<K, V>` in Rust. As of now, this is not configurable, but
75+
may be in the future.
8176
8277
## Variable Length Integers
8378
@@ -109,12 +104,11 @@ fn ident_from_string(s: &String) -> Ident {
109104
/// path is treated as relative to the file location of the macro's use.
110105
/// For details on how the BARE data model maps to the Rust data model, see the [`Serialize`
111106
/// derive macro's documentation.](https://docs.rs/serde_bare/latest/serde_bare/)
112-
pub fn bare_schema(schema_path: &Path, config: Config) -> proc_macro2::TokenStream {
107+
pub fn bare_schema(schema_path: &Path) -> proc_macro2::TokenStream {
113108
let file = read_to_string(schema_path).unwrap();
114109
let mut schema_generator = SchemaGenerator {
115110
global_output: Default::default(),
116111
user_type_registry: parse_string(&file),
117-
config,
118112
};
119113

120114
for (name, user_type) in &schema_generator.user_type_registry.clone() {
@@ -124,38 +118,27 @@ pub fn bare_schema(schema_path: &Path, config: Config) -> proc_macro2::TokenStre
124118
schema_generator.complete()
125119
}
126120

127-
pub struct Config {
128-
pub use_hashable_map: bool,
129-
}
130-
131121
struct SchemaGenerator {
132122
global_output: Vec<TokenStream>,
133123
user_type_registry: BTreeMap<String, AnyType>,
134-
config: Config,
135124
}
136125

137126
impl SchemaGenerator {
138127
/// Completes a generation cycle by consuming the `SchemaGenerator` and yielding a
139128
/// `TokenStream`.
140-
fn complete(self) -> TokenStream {
141-
let user_type_syntax = self.global_output;
142-
let import_maps = if self.config.use_hashable_map {
143-
quote! { #[allow(unused_imports)] use rivet_util::serde::HashableMap; }
144-
} else {
145-
TokenStream::new()
146-
};
147-
quote! {
148-
#import_maps
149-
#[allow(unused_imports)]
150-
use std::collections::HashMap;
151-
#[allow(unused_imports)]
152-
use serde::{Serialize, Deserialize};
153-
#[allow(unused_imports)]
154-
use serde_bare::{Uint, Int};
155-
156-
#(#user_type_syntax)*
157-
}
158-
}
129+
fn complete(self) -> TokenStream {
130+
let user_type_syntax = self.global_output;
131+
quote! {
132+
#[allow(unused_imports)]
133+
use rivet_util::serde::HashableMap;
134+
#[allow(unused_imports)]
135+
use serde::{Serialize, Deserialize};
136+
#[allow(unused_imports)]
137+
use serde_bare::{Uint, Int};
138+
139+
#(#user_type_syntax)*
140+
}
141+
}
159142

160143
/// `gen_user_type` is responsible for generating the token streams of a single user type at a top
161144
/// level. Rust does not support anonymous structs/enums/etc., so we must recursively parse any
@@ -234,10 +217,8 @@ impl SchemaGenerator {
234217
fn gen_map(&mut self, name: &String, key: &AnyType, value: &AnyType) -> TokenStream {
235218
let key_def = self.dispatch_type(name, key);
236219
let val_def = self.dispatch_type(name, value);
237-
if self.config.use_hashable_map {
238-
quote! { HashableMap<#key_def, #val_def> }
239-
} else {
240-
quote! { HashMap<#key_def, #val_def> }
220+
quote! {
221+
HashableMap<#key_def, #val_def>
241222
}
242223
}
243224

@@ -263,14 +244,14 @@ impl SchemaGenerator {
263244
let fields_clone = fields.clone();
264245
let fields_gen = self.gen_struct_field(name, fields_clone);
265246
self.gen_anonymous(name, |ident| {
266-
quote! {
267-
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Hash)]
268-
pub struct #ident {
269-
#(#fields_gen),*
270-
}
271-
}
272-
})
273-
}
247+
quote! {
248+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Hash)]
249+
pub struct #ident {
250+
#(#fields_gen),*
251+
}
252+
}
253+
})
254+
}
274255

275256
fn gen_union(&mut self, name: &String, members: &Vec<AnyType>) -> TokenStream {
276257
let mut members_def: Vec<TokenStream> = Vec::with_capacity(members.len());
@@ -316,14 +297,14 @@ impl SchemaGenerator {
316297
members_def.push(member_def);
317298
}
318299
self.gen_anonymous(name, |ident| {
319-
quote! {
320-
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Hash)]
321-
pub enum #ident {
322-
#(#members_def),*
323-
}
324-
}
325-
})
326-
}
300+
quote! {
301+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Hash)]
302+
pub enum #ident {
303+
#(#members_def),*
304+
}
305+
}
306+
})
307+
}
327308

328309
fn gen_option(&mut self, name: &String, inner: &AnyType) -> TokenStream {
329310
let inner_def = self.dispatch_type(name, inner);
@@ -363,16 +344,16 @@ impl SchemaGenerator {
363344
}
364345
}
365346
});
366-
self.gen_anonymous(name, |ident| {
367-
quote! {
368-
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, PartialOrd, Ord, Hash, Clone)]
369-
#[repr(usize)]
370-
pub enum #ident {
371-
#(#member_defs),*
372-
}
373-
}
374-
})
375-
}
347+
self.gen_anonymous(name, |ident| {
348+
quote! {
349+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, PartialOrd, Ord, Hash, Clone)]
350+
#[repr(usize)]
351+
pub enum #ident {
352+
#(#member_defs),*
353+
}
354+
}
355+
})
356+
}
376357

377358
/// `gen_anonymous` generates an identifier from the provided `name`, passed it to `inner`, pushes
378359
/// the result of `inner` to the `registry`, and yields a quoted version of the generated
@@ -415,3 +396,4 @@ fn gen_primative_type_def(p: &PrimativeType) -> TokenStream {
415396
Bool => quote! { bool },
416397
}
417398
}
399+

rust/vbare-gen/src/parser.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,20 @@ fn parse_float(pair: Pair<'_, Rule>) -> AnyType {
210210
})
211211
}
212212

213-
#[cfg(test)]
214-
mod test {
213+
// #[cfg(test)]
214+
// mod test {
215+
// use std::fs::read_to_string;
216+
//
217+
// use super::*;
218+
//
219+
// #[test]
220+
// fn basic() {
221+
// let file = read_to_string("./src/example.bare").unwrap();
222+
// let user_type_registry: BTreeMap<String, AnyType> = parse_string(&file);
223+
//
224+
// for (key, value) in user_type_registry.iter() {
225+
// println!("{} = {:?}", key, value);
226+
// }
227+
// }
228+
// }
215229

216-
// Test disabled: example.bare file not included in copy
217-
// #[test]
218-
// fn basic() {
219-
// let file = read_to_string("./src/example.bare").unwrap();
220-
// let user_type_registry: BTreeMap<String, AnyType> = parse_string(&file);
221-
//
222-
// for (key, value) in user_type_registry.iter() {
223-
// println!("{} = {:?}", key, value);
224-
// }
225-
// }
226-
}

0 commit comments

Comments
 (0)