1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) 2020, KTH Royal Institute of Technology.
// SPDX-License-Identifier: AGPL-3.0-only

//! `arcon_build` is a wrapper around `prost-build` that adds the required attributes for it to work in arcon.
//!
//! ```toml
//! [dependencies]
//! arcon = <arcon-version>
//! serde = <arcon-serde-version>
//! prost = <arcon-prost-version>
//! abomonation = <arcon-abomonation-version>
//! abomonation_derive = <arcon-abomonation-derive-version>
//!
//! [build-dependencies]
//! arcon_build = { version = <arcon-version> }
//! ```
//!
//! ## Example .proto file
//!
//! ```proto
//! syntax = "proto3";
//!
//! package arcon_data;
//!
//! // unsafe_ser_id = 100
//! // reliable_ser_id = 101
//! // version = 1
//! message Hello {
//! string id = 1
//! }
//! ```
//!
//! Generate the Rust code by creating a `build.rs` build script and use the
//! `compile_protos` function:
//!
//! ```rust,no_run
//! # use std::io::Result;
//! fn main() -> Result<()> {
//!   arcon_build::compile_protos(&["src/path_to_file.proto"], &["src/"])?;
//!   Ok(())
//! }
//! ```

use std::{io::Result, path::Path};

/// Compile Protobuf files with Arcon configured attributes
pub fn compile_protos<P>(protos: &[P], includes: &[P]) -> Result<()>
where
    P: AsRef<Path>,
{
    let mut config = prost_build::Config::new();
    config.type_attribute(
        ".",
        "#[cfg_attr(feature = \"arcon_serde\", derive(serde::Serialize, serde::Deserialize))]",
    );

    config.type_attribute(
        ".",
        "#[cfg_attr(feature = \"unsafe_flight\", derive(abomonation_derive::Abomonation))]",
    );

    config.type_attribute(".", "#[derive(arcon::Arcon)]");

    config.compile_protos(protos, includes)
}