refactor(rust): align workspace with style rules
This commit is contained in:
5
Cargo.lock
generated
5
Cargo.lock
generated
@@ -57,6 +57,7 @@ name = "ariadne"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"monorepo",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
@@ -135,6 +136,10 @@ version = "2.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||
|
||||
[[package]]
|
||||
name = "monorepo"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell_polyfill"
|
||||
version = "1.70.2"
|
||||
|
||||
@@ -2,5 +2,12 @@
|
||||
members = [
|
||||
"src/ariadne",
|
||||
"src/machoke",
|
||||
"src/monorepo",
|
||||
]
|
||||
resolver = "3"
|
||||
|
||||
[workspace.dependencies]
|
||||
clap = "4"
|
||||
monorepo = { path = "src/monorepo" }
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
|
||||
@@ -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",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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)
|
||||
@@ -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"],
|
||||
)
|
||||
|
||||
@@ -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
17
src/monorepo/BUILD
Normal 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
9
src/monorepo/Cargo.toml
Normal 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
1
src/monorepo/monorepo.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod workspace;
|
||||
20
src/monorepo/workspace.rs
Normal file
20
src/monorepo/workspace.rs
Normal 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
|
||||
}
|
||||
52
src/monorepo/workspace_test.rs
Normal file
52
src/monorepo/workspace_test.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user