Skip to content

Commit

Permalink
lib: replace protobuf crate with prost
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralith committed Dec 22, 2022
1 parent 6063fe8 commit 1d3dcd4
Show file tree
Hide file tree
Showing 16 changed files with 627 additions and 204 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ jobs:
env:
RUST_BACKTRACE: 1

check-protos:
name: Check protos
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b
- uses: dtolnay/rust-toolchain@e645b0cf01249a964ec099494d38d2da0f0b349f
with:
toolchain: stable
- run: sudo apt update && sudo apt-get -y install protobuf-compiler
- name: Generate Rust code from .proto files
run: cargo run -p gen-protos
- name: Check for uncommitted changes
run: git diff-files -p --exit-code

rustfmt:
name: Check formatting
runs-on: ubuntu-latest
Expand Down
101 changes: 71 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ name = "diff_bench"
harness = false

[workspace]
members = ["lib", "lib/testutils"]
members = ["lib", "lib/testutils", "lib/gen-protos"]

[dependencies]
chrono = { version = "0.4.23", default-features = false, features = ["std", "clock"] }
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ cargo install --git https://github.com/martinvonz/jj.git --bin jj


### Windows
Due to [a protobuf-src
limitation](https://github.com/MaterializeInc/rust-protobuf-native/issues/4),
`protoc` must be provided manually. See ([prost-build's
instructions](https://github.com/tokio-rs/prost/tree/master/prost-build#protoc))
for details.

Run:
```shell script
Expand Down
3 changes: 1 addition & 2 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ readme = "../README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
protobuf-codegen = "3.2.0"
version_check = "0.9.4"

[dependencies]
Expand All @@ -32,7 +31,6 @@ maplit = "1.0.2"
once_cell = "1.16.0"
pest = "2.5.1"
pest_derive = "2.5.1"
protobuf = { version = "3.0.1", features = ["with-bytes"] }
regex = "1.7.0"
serde_json = "1.0.91"
tempfile = "3.3.0"
Expand All @@ -42,6 +40,7 @@ uuid = { version = "1.2.2", features = ["v4"] }
whoami = "1.2.3"
zstd = "0.12.1"
tracing = "0.1.37"
prost = "0.11.5"

[dev-dependencies]
assert_matches = "1.5.0"
Expand Down
18 changes: 3 additions & 15 deletions lib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

fn main() {
let input = &[
"src/protos/op_store.proto",
"src/protos/store.proto",
"src/protos/working_copy.proto",
];
protobuf_codegen::Codegen::new()
.pure()
.inputs(input)
.include("src/protos")
.cargo_out_dir("protos")
.run_from_script();
fn main() -> std::io::Result<()> {
println!("cargo:rerun-if-changed=build.rs");
for file in input {
println!("cargo:rerun-if-changed={file}");
}

if let Some(true) = version_check::supports_feature("map_first_last") {
println!("cargo:rustc-cfg=feature=\"map_first_last\"");
}

Ok(())
}
8 changes: 8 additions & 0 deletions lib/gen-protos/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "gen-protos"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
prost-build = "0.11.5"
20 changes: 20 additions & 0 deletions lib/gen-protos/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::io::Result;
use std::path::Path;

fn main() -> Result<()> {
let input = ["op_store.proto", "store.proto", "working_copy.proto"];

let root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
let protos_dir = root.join("src").join("protos");

prost_build::Config::new()
.out_dir(&protos_dir)
.include_file("mod.rs")
.compile_protos(
&input
.into_iter()
.map(|x| protos_dir.join(x))
.collect::<Vec<_>>(),
&[protos_dir],
)
}
13 changes: 7 additions & 6 deletions lib/src/git_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::sync::{Arc, Mutex};

use git2::Oid;
use itertools::Itertools;
use protobuf::Message;
use prost::Message;
use uuid::Uuid;

use crate::backend::{
Expand Down Expand Up @@ -126,17 +126,18 @@ fn signature_to_git(signature: &Signature) -> git2::Signature {
}

fn serialize_extras(commit: &Commit) -> Vec<u8> {
let mut proto = crate::protos::store::Commit::new();
proto.change_id = commit.change_id.to_bytes();
let mut proto = crate::protos::store::Commit {
change_id: commit.change_id.to_bytes(),
..Default::default()
};
for predecessor in &commit.predecessors {
proto.predecessors.push(predecessor.to_bytes());
}
proto.write_to_bytes().unwrap()
proto.encode_to_vec()
}

fn deserialize_extras(commit: &mut Commit, bytes: &[u8]) {
let mut cursor = Cursor::new(bytes);
let proto: crate::protos::store::Commit = Message::parse_from_reader(&mut cursor).unwrap();
let proto = crate::protos::store::Commit::decode(bytes).unwrap();
commit.change_id = ChangeId::new(proto.change_id);
for predecessor in &proto.predecessors {
commit.predecessors.push(CommitId::from_bytes(predecessor));
Expand Down
Loading

0 comments on commit 1d3dcd4

Please sign in to comment.