summaryrefslogtreecommitdiffstats
path: root/lib/sellershut-core/build.rs
blob: 8ff48f14a364067ac89a59f2cddfb3ff7ac571ff (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#[cfg(feature = "auth")]
enum Entity {
    #[cfg(feature = "auth")]
    Auth,
}

#[cfg(feature = "auth")]
impl Entity {
    fn protos(&self) -> Vec<&'static str> {
        let mut res: Vec<&'static str> = vec![];

        match self {
            #[cfg(feature = "auth")]
            Entity::Auth => {
                res.extend(vec!["proto/auth/auth.proto"]);
            }
        }
        res
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("cargo:rerun-if-changed=proto");

    #[cfg(feature = "auth")]
    build_proto("auth", Entity::Auth);

    Ok(())
}

#[cfg(feature = "auth")]
fn build_proto(package: &str, entity: Entity) {
    let out_dir = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());

    let config = tonic_build::configure().compile_well_known_types(true)
         .server_mod_attribute(
                package,
                format!("#[cfg(feature = \"{package}\")] #[cfg_attr(docsrs, doc(cfg(feature = \"{package}\")))]"),
            )
            .client_mod_attribute(
                package,
                format!("#[cfg(feature = \"{package}\")] #[cfg_attr(docsrs, doc(cfg(feature = \"{package}\")))]"),
            );

    #[cfg(feature = "serde")]
    let config = add_serde(config);

    #[cfg(feature = "time")]
    let config = config
        .type_attribute(
            ".google.protobuf.Timestamp",
            "#[serde(try_from = \"crate::google::helpers::DateItem\")] #[serde(into = \"String\")]",
        )
        .type_attribute(
            ".google.type.Date",
            "#[serde(try_from = \"crate::google::helpers::DateItem\")] #[serde(into = \"String\")]",
        );

    let include_paths = &["proto"];

    config
        .file_descriptor_set_path(out_dir.join(format!("{package}_descriptor.bin")))
            .server_mod_attribute(
                package,
                format!("#[cfg(feature = \"{package}\")] #[cfg_attr(docsrs, doc(cfg(feature = \"{package}\")))]"),
            )
            .client_mod_attribute(
                package,
                format!("#[cfg(feature = \"{package}\")] #[cfg_attr(docsrs, doc(cfg(feature = \"{package}\")))]"),
            )
        .compile_well_known_types(true)
        .compile_protos(&entity.protos(), include_paths).unwrap();
}

#[cfg(all(feature = "serde", feature = "auth",))]
fn add_serde(config: tonic_build::Builder) -> tonic_build::Builder {
    config.type_attribute(
        ".",
        "#[derive(serde::Serialize, serde::Deserialize)] #[serde(rename_all = \"snake_case\")]",
    )
}