refactor(rust): align workspace with style rules

This commit is contained in:
2026-09-17 09:57:11 -04:00
parent cf9a671fb8
commit b4b77faaa9
15 changed files with 124 additions and 28 deletions

17
src/monorepo/BUILD Normal file
View File

@@ -0,0 +1,17 @@
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
rust_library(
name = "monorepo",
crate_name = "monorepo",
srcs = [
"monorepo.rs",
"workspace.rs",
],
visibility = ["//visibility:public"],
)
rust_test(
name = "workspace_test",
srcs = ["workspace_test.rs"],
deps = [":monorepo"],
)

9
src/monorepo/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "monorepo"
version = "0.1.0"
edition = "2024"
[lib]
path = "monorepo.rs"
[dependencies]

1
src/monorepo/monorepo.rs Normal file
View File

@@ -0,0 +1 @@
pub mod workspace;

20
src/monorepo/workspace.rs Normal file
View File

@@ -0,0 +1,20 @@
use std::path::PathBuf;
pub fn resolve_workspace_path(path: PathBuf) -> PathBuf {
resolve_workspace_path_impl(
path,
std::env::var_os("BUILD_WORKSPACE_DIRECTORY").map(PathBuf::from),
)
}
fn resolve_workspace_path_impl(path: PathBuf, workspace: Option<PathBuf>) -> PathBuf {
if path.is_absolute() {
return path;
}
if let Some(workspace) = workspace {
return workspace.join(path);
}
path
}

View File

@@ -0,0 +1,52 @@
use std::path::PathBuf;
use monorepo::workspace::resolve_workspace_path;
const BUILD_WORKSPACE_DIRECTORY: &str = "BUILD_WORKSPACE_DIRECTORY";
#[derive(Debug)]
struct Case {
name: &'static str,
workspace: Option<&'static str>,
path: &'static str,
expected: &'static str,
}
#[test]
fn resolve_workspace_path_cases() {
let cases = [
Case {
name: "absolute path returns unchanged path",
workspace: Some("/workspace"),
path: "/tmp/output",
expected: "/tmp/output",
},
Case {
name: "relative path joins workspace path",
workspace: Some("/workspace"),
path: "output/graph",
expected: "/workspace/output/graph",
},
Case {
name: "relative path without workspace returns unchanged path",
workspace: None,
path: "output/graph",
expected: "output/graph",
},
];
for case in cases {
unsafe {
if let Some(workspace) = case.workspace {
std::env::set_var(BUILD_WORKSPACE_DIRECTORY, workspace);
} else {
std::env::remove_var(BUILD_WORKSPACE_DIRECTORY);
}
}
let resolved = resolve_workspace_path(PathBuf::from(case.path));
assert_eq!(resolved, PathBuf::from(case.expected), "{}", case.name);
}
}