Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .bazeliskrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Required for rules_rust configuration.
13 changes: 12 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ 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.69.0")

bazel_dep(name = "protobuf", version = "27.0", repo_name = "com_google_protobuf")

crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
crate.from_cargo(
name = "crates",
cargo_lockfile = "//rust:Cargo.lock",
manifests = ["//rust:Cargo.toml"],
)
use_repo(crate, "crates")
34 changes: 34 additions & 0 deletions rust/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
load("@crates//:defs.bzl", "all_crate_deps")

rust_library(
name = "open_location_code",
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(
name = "all_test",
size = "small",
srcs = [
"tests/all_test.rs",
"tests/csv_reader.rs",
],
data = ["//test_data"],
deps = [
":open_location_code",
] + all_crate_deps(
normal = True,
normal_dev = True,
),
visibility = ["//visibility:private"],
)
12 changes: 6 additions & 6 deletions rust/tests/all_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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::<usize>().unwrap();
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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::<f64>().unwrap();
Expand Down
37 changes: 30 additions & 7 deletions rust/tests/csv_reader.rs
Original file line number Diff line number Diff line change
@@ -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<BufReader<File>>,
}

impl CSVReader {
pub fn new(csv_name: &str) -> CSVReader {
pub fn new(csv_name: &str) -> Result<CSVReader, Error> {
// Assumes we're called from <open-location-code root>/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(),
})
}
}

Expand Down