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"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
"monorepo",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
]
|
]
|
||||||
@@ -135,6 +136,10 @@ version = "2.8.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "monorepo"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell_polyfill"
|
name = "once_cell_polyfill"
|
||||||
version = "1.70.2"
|
version = "1.70.2"
|
||||||
|
|||||||
@@ -2,5 +2,12 @@
|
|||||||
members = [
|
members = [
|
||||||
"src/ariadne",
|
"src/ariadne",
|
||||||
"src/machoke",
|
"src/machoke",
|
||||||
|
"src/monorepo",
|
||||||
]
|
]
|
||||||
resolver = "3"
|
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",
|
name = "ariadne_lib",
|
||||||
crate_name = "ariadne",
|
crate_name = "ariadne",
|
||||||
srcs = [
|
srcs = [
|
||||||
|
"ariadne.rs",
|
||||||
"clew.rs",
|
"clew.rs",
|
||||||
"lib.rs",
|
|
||||||
"pipeline.rs",
|
"pipeline.rs",
|
||||||
],
|
],
|
||||||
deps = [
|
deps = [
|
||||||
@@ -17,9 +17,10 @@ rust_library(
|
|||||||
rust_binary(
|
rust_binary(
|
||||||
name = "ariadne",
|
name = "ariadne",
|
||||||
crate_name = "ariadne_cli",
|
crate_name = "ariadne_cli",
|
||||||
srcs = ["main.rs"],
|
srcs = ["ariadne_main.rs"],
|
||||||
deps = [
|
deps = [
|
||||||
":ariadne_lib",
|
":ariadne_lib",
|
||||||
|
"//src/monorepo:monorepo",
|
||||||
"//third_party/crates:clap",
|
"//third_party/crates:clap",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,13 +4,14 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
path = "lib.rs"
|
path = "ariadne.rs"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "ariadne"
|
name = "ariadne"
|
||||||
path = "main.rs"
|
path = "ariadne_main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4", features = ["derive"] }
|
clap = { workspace = true, features = ["derive"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
monorepo = { workspace = true }
|
||||||
serde_json = "1.0"
|
serde = { workspace = true, features = ["derive"] }
|
||||||
|
serde_json = { workspace = true }
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ struct Args {
|
|||||||
impl From<Args> for ariadne::Config {
|
impl From<Args> for ariadne::Config {
|
||||||
fn from(args: Args) -> Self {
|
fn from(args: Args) -> Self {
|
||||||
ariadne::Config {
|
ariadne::Config {
|
||||||
output_dir: resolve_workspace_path(args.output_dir),
|
output_dir: monorepo::workspace::resolve_workspace_path(args.output_dir),
|
||||||
force: args.force,
|
force: args.force,
|
||||||
target_stage: args.target_stage,
|
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}"))
|
.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<()> {
|
fn main() -> ariadne::Result<()> {
|
||||||
let config: ariadne::Config = Args::parse().into();
|
let config: ariadne::Config = Args::parse().into();
|
||||||
ariadne::run(&config)
|
ariadne::run(&config)
|
||||||
@@ -3,11 +3,11 @@ load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
|
|||||||
rust_library(
|
rust_library(
|
||||||
name = "machoke_lib",
|
name = "machoke_lib",
|
||||||
crate_name = "machoke",
|
crate_name = "machoke",
|
||||||
srcs = ["lib.rs"],
|
srcs = ["machoke.rs"],
|
||||||
)
|
)
|
||||||
|
|
||||||
rust_test(
|
rust_test(
|
||||||
name = "machoke_test",
|
name = "machoke_test",
|
||||||
srcs = ["lib_test.rs"],
|
srcs = ["machoke_test.rs"],
|
||||||
deps = [":machoke_lib"],
|
deps = [":machoke_lib"],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,10 +4,6 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
path = "lib.rs"
|
path = "machoke.rs"
|
||||||
|
|
||||||
[[test]]
|
|
||||||
name = "lib_test"
|
|
||||||
path = "lib_test.rs"
|
|
||||||
|
|
||||||
[dependencies]
|
[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