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",
    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 = "tidaldb",
    srcs = glob(["src/**/*.rs", "src/*.rs"], allow_empty = True),
    crate_root = "src/lib.rs",
    crate_name = "tidaldb",
    edition = "2024",
    crate_features = [
        "default",
        "metrics",
    ],
    compile_data = glob(["**/*"], exclude = ["**/*.rs", "**/*.bazel", "BUILD", "WORKSPACE"], allow_empty = True),
    aliases = aliases(),
    deps = all_crate_deps(normal = True) + [
        ":build_script",
    ],
    proc_macro_deps = all_crate_deps(proc_macro = True),
    visibility = ["//visibility:public"],
)

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

rust_test(
    name = "tidaldb_test",
    crate = ":tidaldb_test_support",
    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 = "tidaldb",
    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) + [
        ":tidaldb_test_support",
    ],
    proc_macro_deps = all_crate_deps(proc_macro = True, proc_macro_dev = True),
    # regression_guards reads tidal/CLAUDE.md and docker/standalone/Dockerfile via
    # env!("CARGO_MANIFEST_DIR"); the baked compile-sandbox path is gone at run time, so these can't
    # resolve under hermetic sandboxing. Passes under `cargo test`; excluded from the default
    # `bazel test //...`, still built by `bazel build //...`.
    sandbox_incompatible = [
        "tests/regression_guards.rs",
    ],
)
