load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_proc_macro", "rust_test")
load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
load("@crates//:defs.bzl", "aliases", "all_crate_deps")
load("//bazel:rust_cargo_tests.bzl", "rust_cargo_integration_tests")

cargo_build_script(
    name = "build_script",
    srcs = [
        "build.rs",
    ],
    crate_root = "build.rs",
    data = [
        "proto/wal_shipping.proto",
    ],
    edition = "2024",
    aliases = aliases(build = True),
    deps = all_crate_deps(build = True),
    proc_macro_deps = all_crate_deps(build_proc_macro = True),
    visibility = ["//visibility:private"],
)

rust_library(
    name = "tidal_net",
    srcs = glob(["src/**/*.rs", "src/*.rs"], allow_empty = True),
    crate_root = "src/lib.rs",
    crate_name = "tidal_net",
    edition = "2024",
    compile_data = glob(["**/*"], exclude = ["**/*.rs", "**/*.bazel", "BUILD", "WORKSPACE"], allow_empty = True),
    aliases = aliases(),
    deps = all_crate_deps(normal = True) + [
        "//tidal:tidaldb",
        ":build_script",
    ],
    proc_macro_deps = all_crate_deps(proc_macro = True),
    visibility = ["//visibility:public"],
)

rust_test(
    name = "tidal_net_test",
    crate = ":tidal_net",
    compile_data = glob(["**/*"], exclude = ["**/*.rs", "**/*.bazel", "BUILD", "WORKSPACE"], allow_empty = True),
    data = glob(["**/*"], exclude = ["**/*.rs", "**/*.bazel", "BUILD", "WORKSPACE"], allow_empty = True),
    aliases = aliases(normal_dev = True, proc_macro_dev = True),
    deps = all_crate_deps(normal = True, normal_dev = True),
    proc_macro_deps = all_crate_deps(proc_macro = True, proc_macro_dev = True),
)

rust_cargo_integration_tests(
    name_prefix = "tidal_net",
    tests = glob(["tests/*.rs"]),
    srcs = glob(["tests/**/*.rs"]),
    compile_data = glob(["**/*"], exclude = ["**/*.rs", "**/*.bazel", "BUILD", "WORKSPACE"], allow_empty = True),
    data = glob(["**/*"], exclude = ["**/*.rs", "**/*.bazel", "BUILD", "WORKSPACE"], allow_empty = True),
    aliases = aliases(normal_dev = True, proc_macro_dev = True),
    deps = all_crate_deps(normal = True, normal_dev = True) + [
        "//tidal:tidaldb",
        ":tidal_net",
    ],
    proc_macro_deps = all_crate_deps(proc_macro = True, proc_macro_dev = True),
    # mtls.rs does a real mTLS gRPC send/receive over a socket -> requires-network (excluded from the
    # default `bazel test //...`, like other network-bound integration tests).
    # KNOWN DEFECT it surfaced: tidal-net enables NO rustls crypto provider in its own dep closure
    # (cargo tree shows rustls gets only "log" via tonic). Under `cargo test` it works ONLY because the
    # whole-workspace shared rustls inherits aws-lc-rs from another workspace crate's reqwest;
    # crate_universe resolves the tidal closure narrowly, so the provider is absent and rustls panics
    # ("Could not automatically determine the process-level CryptoProvider"). This is a latent fragility
    # even in cargo: a standalone tidal-server build would break mTLS at runtime. PROPER FIX (out of
    # this build-only review's scope — touches the production crypto path + needs a lock repin): make
    # the provider explicit in tidal-net (direct `rustls = { features = ["aws_lc_rs"] }` dep OR
    # CryptoProvider::install_default() at startup), then CARGO_BAZEL_REPIN.
    requires_network = [
        "tests/mtls.rs",
    ],
)
