Skip to content

Commit ad65779

Browse files
committed
Make protobuf-codegen separate crate
Fixes #234
1 parent 4a582fc commit ad65779

21 files changed

+88
-106
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ members = [
44
"protoc/test-protoc",
55
"protoc-rust",
66
"protobuf",
7+
"protobuf-codegen",
78
"protobuf-test",
89
"perftest/vs-cxx",
910
"perftest/bytes",

protobuf-codegen/Cargo.toml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "protobuf-codegen"
3+
version = "1.4.1"
4+
authors = ["Stepan Koltsov <stepan.koltsov@gmail.com>"]
5+
6+
[dependencies.protobuf]
7+
path = "../protobuf"
8+
9+
[[bin]]
10+
11+
name = "protoc-gen-rust"
12+
path = "src/bin/protoc-gen-rust.rs"
13+
test = false
14+
15+
[[bin]]
16+
17+
name = "protobuf-bin-gen-rust-do-not-use"
18+
path = "src/bin/protobuf-bin-gen-rust-do-not-use.rs"
19+
test = false
20+

protobuf/protobuf-bin-gen-rust-do-not-use.rs protobuf-codegen/src/bin/protobuf-bin-gen-rust-do-not-use.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
extern crate protobuf;
2+
extern crate protobuf_codegen;
23

34
use std::fs::*;
45
use std::io::Read;
@@ -7,7 +8,7 @@ use std::path::Path;
78

89
use protobuf::parse_from_reader;
910
use protobuf::descriptor::*;
10-
use protobuf::codegen::*;
11+
use protobuf_codegen::*;
1112

1213

1314
fn write_file(bin: &str) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate protobuf_codegen;
2+
3+
fn main() {
4+
protobuf_codegen::protoc_gen_rust_main();
5+
}

protobuf/src/code_writer.rs protobuf-codegen/src/code_writer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::io::Write;
55

66
// TODO: should not use wire_format here
7-
use wire_format;
7+
use protobuf::wire_format;
88

99
/// Field visibility.
1010
pub enum Visibility {

protobuf/src/codegen/enums.rs protobuf-codegen/src/enums.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::collections::HashSet;
22

3-
use descriptor::*;
4-
use descriptorx::*;
5-
use code_writer::*;
3+
use protobuf::descriptor::*;
4+
use protobuf::descriptorx::*;
5+
6+
use super::code_writer::*;
67

78

89
#[derive(Clone)]

protobuf/src/codegen/extensions.rs protobuf-codegen/src/extensions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use descriptor::*;
2-
use descriptorx::*;
3-
use code_writer::CodeWriter;
1+
use protobuf::descriptor::*;
2+
use protobuf::descriptorx::*;
3+
use super::code_writer::CodeWriter;
44
use super::rust_types_values::*;
55

66

protobuf/src/codegen/field.rs protobuf-codegen/src/field.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
use descriptor::*;
2-
use descriptorx::*;
3-
use rustproto;
4-
use wire_format;
5-
use rt;
6-
use rust;
7-
use text_format;
8-
use code_writer::CodeWriter;
9-
use types::ProtobufType;
10-
use reflect::ProtobufValue;
11-
12-
use ext::ExtFieldOptional;
1+
use protobuf::descriptor::*;
2+
use protobuf::descriptorx::*;
3+
use protobuf::rustproto; // TODO: should probably live here
4+
use protobuf::wire_format;
5+
use protobuf::rt;
6+
use protobuf::rust;
7+
use protobuf::text_format;
8+
use protobuf::types::ProtobufType;
9+
use protobuf::reflect::ProtobufValue;
10+
11+
use protobuf::ext::ExtFieldOptional;
1312

1413
use super::message::*;
1514
use super::rust_types_values::*;
1615
use super::enums::*;
16+
use super::code_writer::CodeWriter;
1717

1818

1919

@@ -26,7 +26,12 @@ fn type_is_copy(field_type: FieldDescriptorProto_Type) -> bool {
2626
}
2727
}
2828

29-
impl FieldDescriptorProto_Type {
29+
trait FieldDescriptorProtoTypeExt {
30+
fn read(&self, is: &str) -> String;
31+
fn is_s_varint(&self) -> bool;
32+
}
33+
34+
impl FieldDescriptorProtoTypeExt for FieldDescriptorProto_Type {
3035
fn read(&self, is: &str) -> String {
3136
format!("{}.read_{}()", is, protobuf_name(*self))
3237
}
@@ -42,7 +47,7 @@ impl FieldDescriptorProto_Type {
4247
}
4348

4449
fn field_type_wire_type(field_type: FieldDescriptorProto_Type) -> wire_format::WireType {
45-
use stream::wire_format::*;
50+
use protobuf::stream::wire_format::*;
4651
match field_type {
4752
FieldDescriptorProto_Type::TYPE_INT32 => WireTypeVarint,
4853
FieldDescriptorProto_Type::TYPE_INT64 => WireTypeVarint,

protobuf/src/codegen/mod.rs protobuf-codegen/src/lib.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
// TODO: move into separate crate
2-
#![doc(hidden)]
1+
extern crate protobuf;
32

43
use std::collections::hash_map::HashMap;
54
use std::fmt::Write;
65

7-
use descriptor::*;
8-
use core::Message;
9-
use compiler_plugin;
10-
use code_writer::CodeWriter;
11-
use descriptorx::*;
6+
use protobuf::descriptor::*;
7+
use protobuf::Message;
8+
use protobuf::compiler_plugin;
9+
use protobuf::descriptorx::*;
1210

1311
mod message;
1412
mod enums;
@@ -17,10 +15,12 @@ mod well_known_types;
1715
mod field;
1816
mod extensions;
1917

18+
pub mod code_writer;
19+
2020
use self::message::*;
2121
use self::enums::*;
2222
use self::extensions::*;
23-
23+
use self::code_writer::CodeWriter;
2424

2525
fn escape_byte(s: &mut String, b: u8) {
2626
if b == b'\n' {
@@ -33,7 +33,7 @@ fn escape_byte(s: &mut String, b: u8) {
3333
write!(s, "\\{}", b as char).unwrap();
3434
} else if b == b'\0' {
3535
write!(s, "\\0").unwrap();
36-
// ASCII printable except space
36+
// ASCII printable except space
3737
} else if b > 0x20 && b < 0x7f {
3838
write!(s, "{}", b as char).unwrap();
3939
} else {
@@ -99,10 +99,10 @@ fn gen_file(
9999

100100
if scope.get_messages().is_empty() && scope.get_enums().is_empty() &&
101101
file.get_extension().is_empty()
102-
{
103-
// protoc generates empty file descriptors for directories: skip them
104-
return None;
105-
}
102+
{
103+
// protoc generates empty file descriptors for directories: skip them
104+
return None;
105+
}
106106

107107
let mut v = Vec::new();
108108

protobuf/src/codegen/message.rs protobuf-codegen/src/message.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use descriptor::*;
2-
use descriptorx::*;
3-
use code_writer::*;
4-
use rustproto;
1+
use protobuf::descriptor::*;
2+
use protobuf::descriptorx::*;
3+
use protobuf::rustproto;
54

65
use super::enums::*;
76
use super::rust_types_values::*;
87
use super::field::*;
9-
8+
use super::code_writer::*;
109

1110

1211
/// Message info for codegen

protobuf/src/codegen/rust_types_values.rs protobuf-codegen/src/rust_types_values.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::fmt;
22
use std::cmp;
33

4-
use descriptor::*;
5-
use descriptorx::*;
4+
use protobuf::descriptor::*;
5+
use protobuf::descriptorx::*;
66
use super::well_known_types::is_well_known_type_full;
77

88

protobuf/Cargo.toml

-12
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,3 @@ with-bytes = ["bytes"]
1919

2020
[dependencies]
2121
bytes = { version = "0.*", optional = true }
22-
23-
[[bin]]
24-
25-
name = "protoc-gen-rust"
26-
path = "protoc-gen-rust.rs"
27-
test = false
28-
29-
[[bin]]
30-
31-
name = "protobuf-bin-gen-rust-do-not-use"
32-
path = "protobuf-bin-gen-rust-do-not-use.rs"
33-
test = false

protobuf/README.md

+3-25
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,6 @@
11
## How to develop rust-protobuf itself
22

3-
### ./build.sh
3+
`cargo test --all` to build everything.
44

5-
Compile the project.
6-
7-
### ./regenerate.sh
8-
9-
Generate .rs files from .proto files, that needed
10-
internally in rust-protobuf.
11-
12-
### rust test lib/protobuf.rs
13-
14-
Execute the test suite.
15-
16-
### ./full-rebuild.sh
17-
18-
* build project
19-
* regenerate .rs files
20-
* build project again to ensure that generated files are correct
21-
* run tests
22-
23-
This is primary script for development.
24-
25-
### ./checkout-generated.sh
26-
27-
Revert generated files to git version. Use if generated
28-
files are incorrect.
5+
If code generator is changed, code needs to be regenerated, see
6+
`regenerate.sh`.

protobuf/build.sh

-8
This file was deleted.

protobuf/full-rebuild.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ die() {
88
cd $(dirname $0)
99

1010
# Build protoc-gen-rust
11-
./build.sh
11+
cargo build --manifest-path ../protobuf-codegen/Cargo.toml
1212

1313
protoc_ver=$(protoc --version)
1414
case "$protoc_ver" in
1515
"libprotoc 3"*)
1616
# Generate from descriptor.proto
1717
./regenerate.sh
1818
# Build again with regenerated descriptor.proto
19-
./build.sh
19+
cargo build --all
2020
;;
2121
"libprotoc 2"*)
2222
echo "do not regenerate with proto 2"
@@ -26,6 +26,6 @@ case "$protoc_ver" in
2626
;;
2727
esac
2828

29-
./../protobuf-test/test.sh
29+
../protobuf-test/test.sh
3030

3131
# vim: set ts=4 sw=4 et:

protobuf/protoc-gen-rust.rs

-8
This file was deleted.

protobuf/src/lib.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ pub use chars::Chars;
3838
// generated
3939
pub mod descriptor;
4040
pub mod plugin;
41-
mod rustproto;
41+
pub mod rustproto;
4242

4343
mod core;
4444
pub mod rt;
4545
pub mod lazy;
46-
pub mod code_writer;
47-
pub mod codegen;
4846
pub mod compiler_plugin;
4947
mod repeated;
5048
mod singular;
@@ -67,7 +65,8 @@ mod zigzag;
6765
mod paginate;
6866
mod unknown;
6967
mod strx;
70-
mod rust;
68+
#[doc(hidden)] // used by codegen
69+
pub mod rust;
7170
mod cached_size;
7271
mod varint;
7372
#[cfg(feature = "bytes")]
@@ -82,7 +81,6 @@ mod buf_read_iter;
8281
mod protobuf {
8382
pub use descriptor;
8483
pub use descriptorx;
85-
pub use codegen;
8684
pub use reflect;
8785
pub use core::*;
8886
pub use error::*;

protoc-rust/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ doctest = false
1515
[dependencies]
1616
protoc = { path = "../protoc", version = "1.4.1" }
1717
protobuf = { path = "../protobuf", version = "1.4.1" }
18+
protobuf-codegen = { path = "../protobuf-codegen", version = "1.4.1" }
1819
tempdir = "0.3"

protoc-rust/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ extern crate tempdir;
22

33
extern crate protoc;
44
extern crate protobuf;
5+
extern crate protobuf_codegen;
56

67
use std::io;
78
use std::io::Read;
@@ -73,7 +74,7 @@ pub fn run(args: Args) -> Result<()> {
7374
));
7475
}
7576

76-
let gen_result = protobuf::codegen::gen(fds.get_file(), &files_to_generate);
77+
let gen_result = protobuf_codegen::gen(fds.get_file(), &files_to_generate);
7778

7879
for r in gen_result {
7980
let r: protobuf::compiler_plugin::GenResult = r;

protoc/test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ cd $(dirname $0)
44

55
(
66
echo "building protoc-gen-rust"
7-
cd ../protobuf
8-
cargo build --features=$RUST_PROTOBUF_FEATURES --bin=protoc-gen-rust
7+
cd ../protobuf-codegen
8+
cargo build --bin=protoc-gen-rust
99
)
1010

1111
echo "cargo check in test-protoc"

0 commit comments

Comments
 (0)