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

View File

@@ -4,8 +4,8 @@ rust_library(
name = "ariadne_lib",
crate_name = "ariadne",
srcs = [
"ariadne.rs",
"clew.rs",
"lib.rs",
"pipeline.rs",
],
deps = [
@@ -17,9 +17,10 @@ rust_library(
rust_binary(
name = "ariadne",
crate_name = "ariadne_cli",
srcs = ["main.rs"],
srcs = ["ariadne_main.rs"],
deps = [
":ariadne_lib",
"//src/monorepo:monorepo",
"//third_party/crates:clap",
],
)

View File

@@ -4,13 +4,14 @@ version = "0.1.0"
edition = "2024"
[lib]
path = "lib.rs"
path = "ariadne.rs"
[[bin]]
name = "ariadne"
path = "main.rs"
path = "ariadne_main.rs"
[dependencies]
clap = { version = "4", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
clap = { workspace = true, features = ["derive"] }
monorepo = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }

View File

@@ -17,7 +17,7 @@ struct Args {
impl From<Args> for ariadne::Config {
fn from(args: Args) -> Self {
ariadne::Config {
output_dir: resolve_workspace_path(args.output_dir),
output_dir: monorepo::workspace::resolve_workspace_path(args.output_dir),
force: args.force,
target_stage: args.target_stage,
}
@@ -32,19 +32,6 @@ fn parse_stage(value: &str) -> Result<ariadne::pipeline::Stage, String> {
.ok_or_else(|| format!("unknown stage: {value}"))
}
// TODO: Move this into a shared Rust utility crate.
fn resolve_workspace_path(path: PathBuf) -> PathBuf {
if path.is_absolute() {
return path;
}
if let Some(workspace) = std::env::var_os("BUILD_WORKSPACE_DIRECTORY") {
return PathBuf::from(workspace).join(path);
}
path
}
fn main() -> ariadne::Result<()> {
let config: ariadne::Config = Args::parse().into();
ariadne::run(&config)

View File

@@ -3,11 +3,11 @@ load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
rust_library(
name = "machoke_lib",
crate_name = "machoke",
srcs = ["lib.rs"],
srcs = ["machoke.rs"],
)
rust_test(
name = "machoke_test",
srcs = ["lib_test.rs"],
srcs = ["machoke_test.rs"],
deps = [":machoke_lib"],
)

View File

@@ -4,10 +4,6 @@ version = "0.1.0"
edition = "2024"
[lib]
path = "lib.rs"
[[test]]
name = "lib_test"
path = "lib_test.rs"
path = "machoke.rs"
[dependencies]

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);
}
}