From 38bf077ba2c8aca30a36edd723b8f24dc254d261 Mon Sep 17 00:00:00 2001 From: Will Beason Date: Sun, 8 Mar 2026 13:34:42 -0500 Subject: [PATCH 1/6] Add Rust tests to bazelisk --- BUILD | 0 MODULE.bazel | 23 +++++++++++++++++++++++ rust/BUILD | 32 ++++++++++++++++++++++++++++++++ rust/tests/all_test.rs | 12 ++++++------ rust/tests/csv_reader.rs | 37 ++++++++++++++++++++++++++++++------- 5 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 BUILD create mode 100644 rust/BUILD diff --git a/BUILD b/BUILD new file mode 100644 index 00000000..e69de29b diff --git a/MODULE.bazel b/MODULE.bazel index a639d0b0..2529235f 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -11,3 +11,26 @@ bazel_dep(name = "bazel_skylib", version = "1.7.1") # https://github.com/google/googletest bazel_dep(name = "googletest", version = "1.15.2") +bazel_dep(name = "rules_rust", version = "0.58.0") + +rust = use_extension("@rules_rust//rust:extensions.bzl", "rust") +rust.toolchain( + edition = "2024", + versions = ["1.85.0"], +) +use_repo(rust, "rust_toolchains") + +register_toolchains("@rust_toolchains//:all") + +crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate") +crate.from_cargo( + name = "crates", + cargo_lockfile = "//rust:Cargo.lock", + manifests = ["//rust:Cargo.toml"], +) +crate.annotation( + crate = "open-location-code", + rustc_flags = ["-Zunstable-options"], +) +use_repo(crate, "crates") + diff --git a/rust/BUILD b/rust/BUILD new file mode 100644 index 00000000..ec30fbff --- /dev/null +++ b/rust/BUILD @@ -0,0 +1,32 @@ +load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test", "rust_test_suite") +load("@crates//:defs.bzl", "all_crate_deps") + +rust_library( + name = "open_location_code", + srcs = glob(["src/**/*.rs"]), + edition = "2024", + visibility = ["//visibility:public"], + deps = all_crate_deps( + normal = True, + ), +) + +rust_test( + name = "all_test", + size = "small", + srcs = [ + "tests/all_test.rs", + "tests/csv_reader.rs", + ], + data = ["//test_data:test_data"], + edition = "2024", + proc_macro_deps = all_crate_deps( + proc_macro_dev = True, + ), + deps = [ + ":open_location_code", + ] + all_crate_deps( + normal = True, + normal_dev = True, + ), +) diff --git a/rust/tests/all_test.rs b/rust/tests/all_test.rs index 6e35a251..939cad80 100644 --- a/rust/tests/all_test.rs +++ b/rust/tests/all_test.rs @@ -19,7 +19,7 @@ use rand::random_range; #[test] fn is_valid_test() { let mut tested = 0; - for line in CSVReader::new("validityTests.csv") { + for line in CSVReader::new("validityTests.csv").unwrap() { let cols: Vec<&str> = line.split(',').collect(); let code = cols[0]; let _valid = cols[1] == "true"; @@ -38,7 +38,7 @@ fn is_valid_test() { #[test] fn decode_test() { let mut tested = 0; - for line in CSVReader::new("decoding.csv") { + for line in CSVReader::new("decoding.csv").unwrap() { let cols: Vec<&str> = line.split(',').collect(); let code = cols[0]; let len = cols[1].parse::().unwrap(); @@ -65,7 +65,7 @@ fn encode_test() { let mut errors = 0; // Allow a small proportion of errors due to floating point. let allowed_error_rate = 0.05; - for line in CSVReader::new("encoding.csv") { + for line in CSVReader::new("encoding.csv").unwrap() { if line.chars().count() == 0 { continue; } @@ -97,7 +97,7 @@ fn encode_test() { #[test] fn point_to_integers_test() { let mut tested = 0; - for line in CSVReader::new("encoding.csv") { + for line in CSVReader::new("encoding.csv").unwrap() { if line.chars().count() == 0 { continue; } @@ -131,7 +131,7 @@ fn point_to_integers_test() { #[test] fn encode_integers_test() { let mut tested = 0; - for line in CSVReader::new("encoding.csv") { + for line in CSVReader::new("encoding.csv").unwrap() { if line.chars().count() == 0 { continue; } @@ -158,7 +158,7 @@ fn encode_integers_test() { #[test] fn shorten_recovery_test() { let mut tested = 0; - for line in CSVReader::new("shortCodeTests.csv") { + for line in CSVReader::new("shortCodeTests.csv").unwrap() { let cols: Vec<&str> = line.split(',').collect(); let full_code = cols[0]; let lat = cols[1].parse::().unwrap(); diff --git a/rust/tests/csv_reader.rs b/rust/tests/csv_reader.rs index e1871654..e5180db3 100644 --- a/rust/tests/csv_reader.rs +++ b/rust/tests/csv_reader.rs @@ -1,20 +1,43 @@ use std::env::current_dir; use std::fs::File; -use std::io::{BufRead, BufReader, Lines}; +use std::io::{BufRead, BufReader, Lines, Error}; +use std::io::ErrorKind::InvalidInput; +use std::path::PathBuf; pub struct CSVReader { iter: Lines>, } impl CSVReader { - pub fn new(csv_name: &str) -> CSVReader { + pub fn new(csv_name: &str) -> Result { // Assumes we're called from /rust - let project_root = current_dir().unwrap(); - let olc_root = project_root.parent().unwrap(); + let project_root = current_dir()?; + + let olc_root: PathBuf = match project_root.file_name().and_then(|n| n.to_str()) { + Some("rust") => project_root + .parent() + .map(|p| p.to_path_buf()) + .ok_or_else(|| Error::new(InvalidInput, "Could not find project root parent")), + Some("_main") => Ok(project_root.clone()), + _ => { + return Err(Error::new(InvalidInput, format!( + "Expected current dir to end with 'rust' or '_main', got {:?}", + project_root + ))); + } + }?; let csv_path = olc_root.join("test_data").join(csv_name); - CSVReader { - iter: BufReader::new(File::open(csv_path).unwrap()).lines(), - } + + let file = File::open(&csv_path).map_err(|e| { + Error::new(e.kind(), format!( + "Failed to open CSV file at {:?}: {} (Current dir: {:?})", + csv_path, e, project_root + )) + })?; + + Ok(CSVReader { + iter: BufReader::new(file).lines(), + }) } } From 69d9a2ba594cd7649803c6582239b29e489d2570 Mon Sep 17 00:00:00 2001 From: Will Beason Date: Sun, 8 Mar 2026 14:04:57 -0500 Subject: [PATCH 2/6] Pin protobuf library --- MODULE.bazel | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MODULE.bazel b/MODULE.bazel index 2529235f..3d95529c 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -13,6 +13,8 @@ bazel_dep(name = "googletest", version = "1.15.2") bazel_dep(name = "rules_rust", version = "0.58.0") +bazel_dep(name = "protobuf", version = "27.0", repo_name = "com_google_protobuf") + rust = use_extension("@rules_rust//rust:extensions.bzl", "rust") rust.toolchain( edition = "2024", @@ -33,4 +35,3 @@ crate.annotation( rustc_flags = ["-Zunstable-options"], ) use_repo(crate, "crates") - From 66cd0130601f36fd2789356a98e5f4eaf7d50ed9 Mon Sep 17 00:00:00 2001 From: Will Beason Date: Sun, 8 Mar 2026 15:41:02 -0500 Subject: [PATCH 3/6] Remove unnecessary rust declarations from MODULE.bazel --- MODULE.bazel | 9 --------- 1 file changed, 9 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index 3d95529c..da70371e 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -15,15 +15,6 @@ bazel_dep(name = "rules_rust", version = "0.58.0") bazel_dep(name = "protobuf", version = "27.0", repo_name = "com_google_protobuf") -rust = use_extension("@rules_rust//rust:extensions.bzl", "rust") -rust.toolchain( - edition = "2024", - versions = ["1.85.0"], -) -use_repo(rust, "rust_toolchains") - -register_toolchains("@rust_toolchains//:all") - crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate") crate.from_cargo( name = "crates", From 6b29ad3b8ae8d14bec715acd1827af485cee349a Mon Sep 17 00:00:00 2001 From: Will Beason Date: Sun, 8 Mar 2026 15:54:19 -0500 Subject: [PATCH 4/6] Make BUILD more minimal Remove stray items from BUILD file that aren't actually required. --- rust/BUILD | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/rust/BUILD b/rust/BUILD index ec30fbff..983007b4 100644 --- a/rust/BUILD +++ b/rust/BUILD @@ -1,14 +1,19 @@ -load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test", "rust_test_suite") +load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") load("@crates//:defs.bzl", "all_crate_deps") rust_library( name = "open_location_code", - srcs = glob(["src/**/*.rs"]), - edition = "2024", - visibility = ["//visibility:public"], + srcs = [ + "src/lib.rs", + "src/codearea.rs", + "src/consts.rs", + "src/interface.rs", + "src/private.rs", + ], deps = all_crate_deps( normal = True, ), + visibility = ["//visibility:private"], ) rust_test( @@ -18,15 +23,12 @@ rust_test( "tests/all_test.rs", "tests/csv_reader.rs", ], - data = ["//test_data:test_data"], - edition = "2024", - proc_macro_deps = all_crate_deps( - proc_macro_dev = True, - ), + data = ["//test_data"], deps = [ ":open_location_code", ] + all_crate_deps( normal = True, normal_dev = True, ), + visibility = ["//visibility:private"], ) From af2eeaa06d9ba98ac64818aba0b27ffc149c5915 Mon Sep 17 00:00:00 2001 From: Will Beason Date: Sun, 8 Mar 2026 16:16:46 -0500 Subject: [PATCH 5/6] Increase rules_rust version and use non-buggy linker Add build --linkopt in .bazeliskrc to fix warning about deprecated/buggy Rust linker. --- .bazeliskrc | 2 ++ MODULE.bazel | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.bazeliskrc b/.bazeliskrc index 5af90737..bc4a176c 100644 --- a/.bazeliskrc +++ b/.bazeliskrc @@ -3,3 +3,5 @@ # Set the version of Bazel to use. # TODO: #642 -- Update once the JS bazelbuild/rules_closure supports bazel modules. USE_BAZEL_VERSION=7.4.1 +# Use lld linker for rust since gold is deprecated and has known bugs. +build --linkopt=-fuse-ld=lld diff --git a/MODULE.bazel b/MODULE.bazel index da70371e..6f5d3462 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -5,13 +5,13 @@ module( # bazel-skylib required by #@io_bazel_rules_closure. # https://github.com/bazelbuild/bazel-skylib -bazel_dep(name = "bazel_skylib", version = "1.7.1") +bazel_dep(name = "bazel_skylib", version = "1.8.2") # googletest required by c/BUILD. # https://github.com/google/googletest bazel_dep(name = "googletest", version = "1.15.2") -bazel_dep(name = "rules_rust", version = "0.58.0") +bazel_dep(name = "rules_rust", version = "0.69.0") bazel_dep(name = "protobuf", version = "27.0", repo_name = "com_google_protobuf") From e2f5b206a757ed9c6d9299cd0fa1d88952f9514b Mon Sep 17 00:00:00 2001 From: Will Beason Date: Sun, 8 Mar 2026 16:55:24 -0500 Subject: [PATCH 6/6] Remove more unnecessary MODULE.bazel configuration --- BUILD | 1 + MODULE.bazel | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/BUILD b/BUILD index e69de29b..bec4223f 100644 --- a/BUILD +++ b/BUILD @@ -0,0 +1 @@ +# Required for rules_rust configuration. diff --git a/MODULE.bazel b/MODULE.bazel index 6f5d3462..59911897 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -21,8 +21,4 @@ crate.from_cargo( cargo_lockfile = "//rust:Cargo.lock", manifests = ["//rust:Cargo.toml"], ) -crate.annotation( - crate = "open-location-code", - rustc_flags = ["-Zunstable-options"], -) use_repo(crate, "crates")