Skip to content

Commit

Permalink
fix: Generate registration functions
Browse files Browse the repository at this point in the history
  • Loading branch information
semtexzv committed Sep 13, 2023
1 parent 67f72ac commit 9b67c7e
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 133 deletions.
62 changes: 8 additions & 54 deletions protokit_build/src/filegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ pub fn builtin_type_marker(typ: BuiltinType) -> &'static str {
pub struct CodeGenerator<'a> {
context: &'a FileSetDef,
options: &'a Options,
types: Vec<TokenStream>,
output: Vec<TokenStream>,
}

Expand Down Expand Up @@ -294,6 +295,7 @@ impl CodeGenerator<'_> {
None
};

self.types.push(quote!{#ident});
self.output.push(quote! {
#[derive(Debug, Default, Clone, PartialEq, Proto)]
#attrs
Expand Down Expand Up @@ -435,62 +437,11 @@ pub fn generate_file(ctx: &FileSetDef, opts: &Options, name: PathBuf, file: &Fil
let mut generator = CodeGenerator {
context: ctx,
options: opts,
types: vec![],
output: vec![],
};

generator.file(file)?;
//
// let root = opts.import_root.clone();
//
// let mut push_imports = HashSet::new();
// let mut msg_types = vec![];
// let mut messages = vec![];
// let mut enums = vec![];
// let mut services: Vec<TokenStream> = vec![];
//
// let generator = CodeGenerator {
// context: ctx,
// options: opts,
// };
//
// for (idx, (_sym, msg)) in unit.messages.iter().enumerate() {
// let defid = (unit_id as u64) << 32 | LOCAL_DEFID_MSG as u64 | idx as u64;
// let (msg_contents, types, imports) = generator.generate_msg(&ctx.def, unit, defid, msg);
// push_imports.extend(imports);
// msg_types.extend(types.into_iter());
// messages.push(msg_contents);
// }
//
// for (idx, (_name, en)) in unit.enums.iter().enumerate() {
// let _defid = (unit_id as u64) << 32 | LOCAL_DEFID_ENUM as u64 | idx as u64;
// enums.push(generator.generate_enum(en));
// }
//
// for (_name, svc) in unit.services.iter() {
// services.push(generator.generate_server(unit, svc));
// services.push(generator.generate_client(unit, svc));
// }
//

//
// let output = quote! {
// #![allow(nonstandard_style)]
// #![allow(unused)]
// #![deny(unused_must_use)]
// #![allow(clippy::derive_partial_eq_without_eq)]
//
// use core::fmt::Write;
//
// use #root::*;
// use #root as root;
//
// #(#imports)*
//
//
// #(#messages)*
// #(#enums)*
// #(#services)*
// };

let root = opts.import_root.clone();
let imports = file.imports.iter().map(|imp| imp.file_idx).map(|file_idx| {
Expand Down Expand Up @@ -540,16 +491,19 @@ pub fn generate_file(ctx: &FileSetDef, opts: &Options, name: PathBuf, file: &Fil
use #import::*;
}
});

let output = generator.output;
let types = generator.types;

let output = quote! {
#![allow(unused_imports)]
#![allow(nonstandard_style)]
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use #root::*;

pub fn register_types(_registry: &mut #root::textformat::reflect::Registry) {
// #(#files::register_types(registry);)*
pub fn register_types(registry: &mut #root::textformat::reflect::Registry) {
#(registry.register(&#types::default());)*
}

#(#imports)*
Expand Down
10 changes: 7 additions & 3 deletions protokit_desc/src/generated/google/protobuf/compiler/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ pub fn register_types(_registry: &mut crate::textformat::reflect::Registry) {}
use super::super::descriptor::*;
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct CodeGeneratorResponseFeature(pub u32);
#[protoenum(closed)]
#[protoenum]
impl CodeGeneratorResponseFeature {
#[var(0u32, "FEATURE_NONE")]
pub const FEATURE_NONE: CodeGeneratorResponseFeature = CodeGeneratorResponseFeature(0u32);
pub const FEATURE_NONE: CodeGeneratorResponseFeature = CodeGeneratorResponseFeature(
0u32,
);
#[var(1u32, "FEATURE_PROTO3_OPTIONAL")]
pub const FEATURE_PROTO3_OPTIONAL: CodeGeneratorResponseFeature = CodeGeneratorResponseFeature(1u32);
pub const FEATURE_PROTO3_OPTIONAL: CodeGeneratorResponseFeature = CodeGeneratorResponseFeature(
1u32,
);
}
#[derive(Debug, Default, Clone, PartialEq, Proto)]
pub struct Version {
Expand Down
36 changes: 24 additions & 12 deletions protokit_desc/src/generated/google/protobuf/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::*;
pub fn register_types(_registry: &mut crate::textformat::reflect::Registry) {}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FieldDescriptorProtoType(pub u32);
#[protoenum(closed)]
#[protoenum]
impl FieldDescriptorProtoType {
#[var(1u32, "TYPE_DOUBLE")]
pub const TYPE_DOUBLE: FieldDescriptorProtoType = FieldDescriptorProtoType(1u32);
Expand Down Expand Up @@ -47,18 +47,24 @@ impl FieldDescriptorProtoType {
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FieldDescriptorProtoLabel(pub u32);
#[protoenum(closed)]
#[protoenum]
impl FieldDescriptorProtoLabel {
#[var(1u32, "LABEL_OPTIONAL")]
pub const LABEL_OPTIONAL: FieldDescriptorProtoLabel = FieldDescriptorProtoLabel(1u32);
pub const LABEL_OPTIONAL: FieldDescriptorProtoLabel = FieldDescriptorProtoLabel(
1u32,
);
#[var(2u32, "LABEL_REQUIRED")]
pub const LABEL_REQUIRED: FieldDescriptorProtoLabel = FieldDescriptorProtoLabel(2u32);
pub const LABEL_REQUIRED: FieldDescriptorProtoLabel = FieldDescriptorProtoLabel(
2u32,
);
#[var(3u32, "LABEL_REPEATED")]
pub const LABEL_REPEATED: FieldDescriptorProtoLabel = FieldDescriptorProtoLabel(3u32);
pub const LABEL_REPEATED: FieldDescriptorProtoLabel = FieldDescriptorProtoLabel(
3u32,
);
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FileOptionsOptimizeMode(pub u32);
#[protoenum(closed)]
#[protoenum]
impl FileOptionsOptimizeMode {
#[var(1u32, "SPEED")]
pub const SPEED: FileOptionsOptimizeMode = FileOptionsOptimizeMode(1u32);
Expand All @@ -69,7 +75,7 @@ impl FileOptionsOptimizeMode {
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FieldOptionsCType(pub u32);
#[protoenum(closed)]
#[protoenum]
impl FieldOptionsCType {
#[var(0u32, "STRING")]
pub const STRING: FieldOptionsCType = FieldOptionsCType(0u32);
Expand All @@ -80,7 +86,7 @@ impl FieldOptionsCType {
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FieldOptionsJSType(pub u32);
#[protoenum(closed)]
#[protoenum]
impl FieldOptionsJSType {
#[var(0u32, "JS_NORMAL")]
pub const JS_NORMAL: FieldOptionsJSType = FieldOptionsJSType(0u32);
Expand All @@ -91,14 +97,20 @@ impl FieldOptionsJSType {
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct MethodOptionsIdempotencyLevel(pub u32);
#[protoenum(closed)]
#[protoenum]
impl MethodOptionsIdempotencyLevel {
#[var(0u32, "IDEMPOTENCY_UNKNOWN")]
pub const IDEMPOTENCY_UNKNOWN: MethodOptionsIdempotencyLevel = MethodOptionsIdempotencyLevel(0u32);
pub const IDEMPOTENCY_UNKNOWN: MethodOptionsIdempotencyLevel = MethodOptionsIdempotencyLevel(
0u32,
);
#[var(1u32, "NO_SIDE_EFFECTS")]
pub const NO_SIDE_EFFECTS: MethodOptionsIdempotencyLevel = MethodOptionsIdempotencyLevel(1u32);
pub const NO_SIDE_EFFECTS: MethodOptionsIdempotencyLevel = MethodOptionsIdempotencyLevel(
1u32,
);
#[var(2u32, "IDEMPOTENT")]
pub const IDEMPOTENT: MethodOptionsIdempotencyLevel = MethodOptionsIdempotencyLevel(2u32);
pub const IDEMPOTENT: MethodOptionsIdempotencyLevel = MethodOptionsIdempotencyLevel(
2u32,
);
}
#[derive(Debug, Default, Clone, PartialEq, Proto)]
pub struct FileDescriptorSet {
Expand Down
10 changes: 4 additions & 6 deletions protokit_desc/src/generated/google/protobuf/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
pub mod any;
pub mod compiler;
pub mod descriptor;
pub mod duration;
pub mod empty;
pub mod field_mask;
pub mod timestamp;
pub fn register_types(registry: &mut crate::textformat::reflect::Registry) {
empty::register_types(registry);
descriptor::register_types(registry);
compiler::register_types(registry);
timestamp::register_types(registry);
any::register_types(registry);
duration::register_types(registry);
compiler::register_types(registry);
descriptor::register_types(registry);
empty::register_types(registry);
field_mask::register_types(registry);
timestamp::register_types(registry);
}
2 changes: 0 additions & 2 deletions protokit_desc/src/generated/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
pub mod google;
pub mod validate;
pub fn register_types(registry: &mut crate::textformat::reflect::Registry) {
google::register_types(registry);
validate::register_types(registry);
}
2 changes: 1 addition & 1 deletion protokit_examples/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn main() -> protokit_build::Result<()> {
protokit_build::Build::new()
.track_unknowns(true)
.borrow_bufs()
// .borrow_bufs()
.include("../proto")
.compile("google/protobuf/descriptor.proto")?
.compile("google/protobuf/api.proto")?
Expand Down
7 changes: 6 additions & 1 deletion tools/conformance/src/gen/conformance/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use ::protokit::*;
pub fn register_types(_registry: &mut ::protokit::textformat::reflect::Registry) {}
pub fn register_types(registry: &mut ::protokit::textformat::reflect::Registry) {
registry.register(&FailureSet::default());
registry.register(&ConformanceRequest::default());
registry.register(&ConformanceResponse::default());
registry.register(&JspbEncodingConfig::default());
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct WireFormat(pub u32);
#[protoenum]
Expand Down
4 changes: 3 additions & 1 deletion tools/conformance/src/gen/google/protobuf/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use ::protokit::*;
pub fn register_types(_registry: &mut ::protokit::textformat::reflect::Registry) {}
pub fn register_types(registry: &mut ::protokit::textformat::reflect::Registry) {
registry.register(&Any::default());
}
#[derive(Debug, Default, Clone, PartialEq, Proto)]
pub struct Any {
#[field(1u32, "type_url", string, singular)]
Expand Down
4 changes: 3 additions & 1 deletion tools/conformance/src/gen/google/protobuf/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use ::protokit::*;
pub fn register_types(_registry: &mut ::protokit::textformat::reflect::Registry) {}
pub fn register_types(registry: &mut ::protokit::textformat::reflect::Registry) {
registry.register(&Duration::default());
}
#[derive(Debug, Default, Clone, PartialEq, Proto)]
pub struct Duration {
#[field(1u32, "seconds", varint, singular)]
Expand Down
4 changes: 3 additions & 1 deletion tools/conformance/src/gen/google/protobuf/field_mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use ::protokit::*;
pub fn register_types(_registry: &mut ::protokit::textformat::reflect::Registry) {}
pub fn register_types(registry: &mut ::protokit::textformat::reflect::Registry) {
registry.register(&FieldMask::default());
}
#[derive(Debug, Default, Clone, PartialEq, Proto)]
pub struct FieldMask {
#[field(1u32, "paths", string, repeated)]
Expand Down
6 changes: 5 additions & 1 deletion tools/conformance/src/gen/google/protobuf/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use ::protokit::*;
pub fn register_types(_registry: &mut ::protokit::textformat::reflect::Registry) {}
pub fn register_types(registry: &mut ::protokit::textformat::reflect::Registry) {
registry.register(&Struct::default());
registry.register(&Value::default());
registry.register(&ListValue::default());
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct NullValue(pub u32);
#[protoenum]
Expand Down
4 changes: 3 additions & 1 deletion tools/conformance/src/gen/google/protobuf/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use ::protokit::*;
pub fn register_types(_registry: &mut ::protokit::textformat::reflect::Registry) {}
pub fn register_types(registry: &mut ::protokit::textformat::reflect::Registry) {
registry.register(&Timestamp::default());
}
#[derive(Debug, Default, Clone, PartialEq, Proto)]
pub struct Timestamp {
#[field(1u32, "seconds", varint, singular)]
Expand Down
12 changes: 11 additions & 1 deletion tools/conformance/src/gen/google/protobuf/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use ::protokit::*;
pub fn register_types(_registry: &mut ::protokit::textformat::reflect::Registry) {}
pub fn register_types(registry: &mut ::protokit::textformat::reflect::Registry) {
registry.register(&DoubleValue::default());
registry.register(&FloatValue::default());
registry.register(&Int64Value::default());
registry.register(&UInt64Value::default());
registry.register(&Int32Value::default());
registry.register(&UInt32Value::default());
registry.register(&BoolValue::default());
registry.register(&StringValue::default());
registry.register(&BytesValue::default());
}
#[derive(Debug, Default, Clone, PartialEq, Proto)]
pub struct DoubleValue {
#[field(1u32, "value", fixed64, singular)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,27 @@
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use ::protokit::*;
pub fn register_types(_registry: &mut ::protokit::textformat::reflect::Registry) {}
pub fn register_types(registry: &mut ::protokit::textformat::reflect::Registry) {
registry.register(&TestAllTypesProto2NestedMessage::default());
registry.register(&TestAllTypesProto2Data::default());
registry.register(&TestAllTypesProto2MessageSetCorrect::default());
registry.register(&TestAllTypesProto2MessageSetCorrectExtension1::default());
registry.register(&TestAllTypesProto2MessageSetCorrectExtension2::default());
registry.register(&TestAllTypesProto2::default());
registry.register(&ForeignMessageProto2::default());
registry.register(&UnknownToTestAllTypesOptionalGroup::default());
registry.register(&UnknownToTestAllTypes::default());
registry.register(&NullHypothesisProto2::default());
registry.register(&EnumOnlyProto2::default());
registry.register(&OneStringProto2::default());
registry.register(&ProtoWithKeywords::default());
registry.register(&TestAllRequiredTypesProto2NestedMessage::default());
registry.register(&TestAllRequiredTypesProto2Data::default());
registry.register(&TestAllRequiredTypesProto2MessageSetCorrect::default());
registry.register(&TestAllRequiredTypesProto2MessageSetCorrectExtension1::default());
registry.register(&TestAllRequiredTypesProto2MessageSetCorrectExtension2::default());
registry.register(&TestAllRequiredTypesProto2::default());
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct ForeignEnumProto2(pub u32);
#[protoenum]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use ::protokit::*;
pub fn register_types(_registry: &mut ::protokit::textformat::reflect::Registry) {}
pub fn register_types(registry: &mut ::protokit::textformat::reflect::Registry) {
registry.register(&TestAllTypesProto3NestedMessage::default());
registry.register(&TestAllTypesProto3::default());
registry.register(&ForeignMessage::default());
registry.register(&NullHypothesisProto3::default());
registry.register(&EnumOnlyProto3::default());
}
use super::super::super::google::protobuf::any::*;
use super::super::super::google::protobuf::duration::*;
use super::super::super::google::protobuf::field_mask::*;
Expand Down
Loading

0 comments on commit 9b67c7e

Please sign in to comment.