Skip to content

Commit

Permalink
fix: Fix test, use indexmap
Browse files Browse the repository at this point in the history
  • Loading branch information
semtexzv committed Sep 14, 2023
1 parent 802485d commit 3df0d0d
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 140 deletions.
10 changes: 5 additions & 5 deletions protokit_binformat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ where
T: BinProto<'buf>,
{
#[inline(always)]
fn qualified_name(&self) -> &'static str{
fn qualified_name(&self) -> &'static str {
self.deref().qualified_name()
}

Expand All @@ -93,9 +93,10 @@ where

#[cfg(feature = "arena")]
impl<'buf, 'arena, T> BinProto<'buf> for bumpalo::boxed::Box<'arena, T>
where T: BinProto<'buf>
where
T: BinProto<'buf>,
{
fn qualified_name(&self) -> &'static str {
fn qualified_name(&self) -> &'static str {
self.deref().qualified_name()
}

Expand Down Expand Up @@ -771,10 +772,9 @@ pub fn encode<'buf, T: BinProto<'buf>>(b: &T) -> Result<Vec<u8>> {
Ok(out.buf)
}


pub fn encode_to<'buf, T: BinProto<'buf>>(b: &T, out: Vec<u8>) -> Result<Vec<u8>> {
let mut out = OutputStream::new(out);
b.size(&mut out.stack);
b.encode(&mut out);
Ok(out.buf)
}
}
26 changes: 17 additions & 9 deletions protokit_binformat/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::fmt::Debug;

use crate::{emit_raw, unknown_tag, unknown_wire, BinProto, BytesLike, Error, InputStream, OutputStream, SizeStack, MASK_WIRE, _size_varint};
use crate::{
emit_raw, unknown_tag, unknown_wire, BinProto, BytesLike, Error, InputStream, OutputStream, SizeStack,
_size_varint, MASK_WIRE,
};

/// Single protobuf value
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
Expand All @@ -21,13 +24,14 @@ pub struct Field<B> {

impl<'a, B: BytesLike<'a>> Field<B> {
fn size(&self, stack: &mut SizeStack) -> usize {
_size_varint(self.num) + match &self.val {
Value::Varint(v) => _size_varint(*v),
Value::Fixed32(_) => 4,
Value::Fixed64(_) => 8,
Value::Bytes(b) => _size_varint(b.len()) + b.len(),
Value::Group(g) => _size_varint(self.num) + g.iter().rev().map(|f| f.size(stack)).sum::<usize>()
}
_size_varint(self.num)
+ match &self.val {
Value::Varint(v) => _size_varint(*v),
Value::Fixed32(_) => 4,
Value::Fixed64(_) => 8,
Value::Bytes(b) => _size_varint(b.len()) + b.len(),
Value::Group(g) => _size_varint(self.num) + g.iter().rev().map(|f| f.size(stack)).sum::<usize>(),
}
}
}

Expand All @@ -53,7 +57,11 @@ impl<'buf, B: BytesLike<'buf>> BinProto<'buf> for UnknownFields<B> {
}

fn size(&self, _stack: &mut SizeStack) -> usize {
self.fields.iter().flat_map(|v| v.iter().rev()).map(|v| v.size(_stack)).sum()
self.fields
.iter()
.flat_map(|v| v.iter().rev())
.map(|v| v.size(_stack))
.sum()
}

fn encode(&self, stream: &mut OutputStream) {
Expand Down
4 changes: 0 additions & 4 deletions protokit_build/src/filegen/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use crate::filegen::{rustify_name, CodeGenerator};

impl CodeGenerator<'_> {
pub fn generate_server(&self, file: &FileDef, svc: &ServiceDef) -> TokenStream {
let root = self.options.import_root.clone();

let svc_qualified_raw_name = format!("{}.{}", file.package, svc.name);

let svc_name = format_ident!("{}", rustify_name(svc.name.as_str()));
Expand Down Expand Up @@ -187,8 +185,6 @@ impl CodeGenerator<'_> {
}

pub fn generate_client(&self, file: &FileDef, svc: &ServiceDef) -> TokenStream {
let root = self.options.import_root.clone();

let _svc_qualified_raw_name = format!("{}.{}", file.package, svc.name);

let _svc_name = format_ident!("{}", rustify_name(svc.name.as_str()));
Expand Down
45 changes: 18 additions & 27 deletions protokit_build/src/filegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl CodeGenerator<'_> {
BuiltinType::String_ => return self.options.string_type.clone(),
BuiltinType::Bytes_ => return self.options.bytes_type.clone(),
})
.unwrap()
.unwrap()
}

pub fn type_marker(typ: &DataType) -> TokenStream {
Expand All @@ -195,10 +195,10 @@ impl CodeGenerator<'_> {
builtin_type_marker(k.0),
Self::type_marker(&k.1)
))
.unwrap();
.unwrap();
}
})
.unwrap()
.unwrap()
}

pub fn base_type(&self, typ: &DataType) -> Result<TokenStream> {
Expand Down Expand Up @@ -284,22 +284,15 @@ impl CodeGenerator<'_> {
let generics = self.options.generics.struct_def_generics();
let name = msg.name.as_str();
let pkg = file.package.as_str();
let attrs = quote!{
let attrs = quote! {
#[proto(name = #name, package = #pkg)]
};

let extfields = extfields.into_iter()
.map(|(f, pkg)| self.field(file, f, Some(pkg)));
let extfields = extfields.into_iter().map(|(f, pkg)| self.field(file, f, Some(pkg)));

let fields = msg
.fields
.by_number
.values()
.map(|f| self.field(file, f, None));
let fields = msg.fields.by_number.values().map(|f| self.field(file, f, None));

let fields = fields
.chain(extfields)
.collect::<Result<Vec<_>>>()?;
let fields = fields.chain(extfields).collect::<Result<Vec<_>>>()?;

let oneofs = msg
.oneofs
Expand Down Expand Up @@ -448,7 +441,7 @@ impl CodeGenerator<'_> {
Frequency::Repeated => "repeated",
Frequency::Required => "required",
})
.unwrap();
.unwrap();

Ok(quote! {
#[field(#num, #name, #kind, #freq)]
Expand Down Expand Up @@ -478,21 +471,21 @@ pub fn generate_file(ctx: &FileSetDef, opts: &Options, name: PathBuf, file: &Fil
// }

let their_name = if other.name.contains('/') {
&other.name.as_str()[other.name.rfind('/').unwrap() + 1..]
&other.name.as_str()[other.name.rfind('/').unwrap() + 1 ..]
} else {
other.name.as_str()
};
let their_name = if their_name.contains('.') {
&their_name[..their_name.rfind('.').unwrap()]
&their_name[.. their_name.rfind('.').unwrap()]
} else {
their_name
};
let mut our_module = file.package.as_str();
let mut that_module = other.package.as_str();

while !our_module.is_empty() && !that_module.is_empty() && our_module[..1] == that_module[..1] {
our_module = &our_module[1..];
that_module = &that_module[1..];
while !our_module.is_empty() && !that_module.is_empty() && our_module[.. 1] == that_module[.. 1] {
our_module = &our_module[1 ..];
that_module = &that_module[1 ..];
}
let mut path = String::new();
path.push_str("super::");
Expand All @@ -519,9 +512,9 @@ pub fn generate_file(ctx: &FileSetDef, opts: &Options, name: PathBuf, file: &Fil
let output = generator.output;
let types = generator.types;
let maproot = if let Some(ref root) = root {
quote!{ use #root as protokit; }
quote! { use #root as protokit; }
} else {
quote!{}
quote! {}
};

let output = quote! {
Expand Down Expand Up @@ -569,7 +562,7 @@ pub fn generate_file(ctx: &FileSetDef, opts: &Options, name: PathBuf, file: &Fil
// f.flush().unwrap();
// }

pub fn generate_mod<'s>(path: impl AsRef<Path>, opts: &Options, files: impl Iterator<Item=&'s str>) -> Result<()> {
pub fn generate_mod<'s>(path: impl AsRef<Path>, opts: &Options, files: impl Iterator<Item = &'s str>) -> Result<()> {
let root = opts.import_root.clone();
let files: Vec<_> = files
.map(|v| {
Expand All @@ -578,14 +571,12 @@ pub fn generate_mod<'s>(path: impl AsRef<Path>, opts: &Options, files: impl Iter
})
.collect();


let maproot = if let Some(ref root) = root {
quote!{ use #root as protokit; }
quote! { use #root as protokit; }
} else {
quote!{}
quote! {}
};


let output = quote! {
#maproot

Expand Down
6 changes: 3 additions & 3 deletions protokit_build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl ProtocContext {

cmd.arg("--experimental_allow_proto3_optional");
cmd.arg("--include_imports");

for i in self.includes {
cmd.arg(format!("-I{}", i.display()));
}
Expand Down Expand Up @@ -120,9 +120,9 @@ fn generate(opts: &filegen::Options, set: &protokit_desc::FileSetDef, out_dir: P

// Generate a valid module file in every subdirectory
for path in &dirs {
for i in 0..path.len() {
for i in 0 .. path.len() {
subdirs
.entry(path[0..i].join("/"))
.entry(path[0 .. i].join("/"))
.or_insert_with(BTreeSet::new)
.insert(path[i]);
}
Expand Down
4 changes: 2 additions & 2 deletions protokit_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn binproto(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
panic!("Unions are not supported")
}
}
.into()
.into()
}

#[proc_macro_derive(Proto, attributes(proto, field, oneof, unknown))]
Expand All @@ -94,7 +94,7 @@ pub fn proto(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
panic!("Unions are not supported")
}
}
.into()
.into()
}

enum Item {
Expand Down
3 changes: 2 additions & 1 deletion protokit_desc/src/generated/google/protobuf/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#![allow(nonstandard_style)]
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use crate as protokit;
use protokit::*;

use crate as protokit;
pub fn register_types(registry: &mut protokit::textformat::reflect::Registry) {
registry.register(&Any::default());
}
Expand Down
11 changes: 4 additions & 7 deletions protokit_desc/src/generated/google/protobuf/compiler/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#![allow(nonstandard_style)]
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use crate as protokit;
use protokit::*;

use crate as protokit;
pub fn register_types(registry: &mut protokit::textformat::reflect::Registry) {
registry.register(&Version::default());
registry.register(&CodeGeneratorRequest::default());
Expand All @@ -16,13 +17,9 @@ pub struct CodeGeneratorResponseFeature(pub u32);
#[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)]
#[proto(name = "Version", package = "google.protobuf.compiler")]
Expand Down
27 changes: 8 additions & 19 deletions protokit_desc/src/generated/google/protobuf/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#![allow(nonstandard_style)]
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use crate as protokit;
use protokit::*;

use crate as protokit;
pub fn register_types(registry: &mut protokit::textformat::reflect::Registry) {
registry.register(&FileDescriptorSet::default());
registry.register(&FileDescriptorProto::default());
Expand Down Expand Up @@ -79,17 +80,11 @@ pub struct FieldDescriptorProtoLabel(pub u32);
#[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);
Expand Down Expand Up @@ -129,17 +124,11 @@ pub struct MethodOptionsIdempotencyLevel(pub u32);
#[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)]
#[proto(name = "FileDescriptorSet", package = "google.protobuf")]
Expand Down
3 changes: 2 additions & 1 deletion protokit_desc/src/generated/google/protobuf/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#![allow(nonstandard_style)]
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use crate as protokit;
use protokit::*;

use crate as protokit;
pub fn register_types(registry: &mut protokit::textformat::reflect::Registry) {
registry.register(&Empty::default());
}
Expand Down
3 changes: 2 additions & 1 deletion protokit_desc/src/generated/google/protobuf/field_mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#![allow(nonstandard_style)]
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use crate as protokit;
use protokit::*;

use crate as protokit;
pub fn register_types(registry: &mut protokit::textformat::reflect::Registry) {
registry.register(&FieldMask::default());
}
Expand Down
3 changes: 2 additions & 1 deletion protokit_desc/src/generated/google/protobuf/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#![allow(nonstandard_style)]
#![allow(unreachable_patterns)]
#![allow(clippy::module_inception)]
use crate as protokit;
use protokit::*;

use crate as protokit;
pub fn register_types(registry: &mut protokit::textformat::reflect::Registry) {
registry.register(&Timestamp::default());
}
Expand Down
Loading

0 comments on commit 3df0d0d

Please sign in to comment.