mecha10_cli_core/
paths.rs1#![allow(dead_code)]
15
16use std::path::{Path, PathBuf};
17
18pub const PROJECT_CONFIG: &str = "mecha10.json";
24
25pub mod project {
27 pub const NODES_DIR: &str = "nodes";
29
30 pub const DRIVERS_DIR: &str = "drivers";
32
33 pub const TYPES_DIR: &str = "types";
35
36 pub const BEHAVIORS_DIR: &str = "behaviors";
38
39 pub const ASSETS_DIR: &str = "assets";
41
42 pub const ASSETS_IMAGES_DIR: &str = "assets/images";
44
45 pub const MODELS_DIR: &str = "models";
47
48 pub const LOGS_DIR: &str = "logs";
50
51 pub const SIMULATION_DIR: &str = "simulation";
53
54 pub const SIMULATION_MODELS_DIR: &str = "simulation/models";
56
57 pub const SIMULATION_ENVIRONMENTS_DIR: &str = "simulation/environments";
59
60 pub fn model_config(model_name: &str) -> String {
62 format!("simulation/models/{}/model.json", model_name)
63 }
64
65 pub fn environment_config(env_name: &str) -> String {
67 format!("simulation/environments/{}/environment.json", env_name)
68 }
69
70 pub const SIMULATION_OUTPUT_DIR: &str = "simulation/output";
72
73 pub const TARGET_DIR: &str = "target";
75
76 pub const SRC_DIR: &str = "src";
78}
79
80pub mod config {
82 pub const DIR: &str = "configs";
84
85 pub const NODES_DIR: &str = "configs/nodes";
87
88 pub const NODES_MECHA10_DIR: &str = "configs/nodes/@mecha10";
90
91 pub const NODES_LOCAL_DIR: &str = "configs/nodes/@local";
93
94 pub const SIMULATION_DIR: &str = "configs/simulation";
96
97 pub const SIMULATION_CONFIG: &str = "configs/simulation/config.json";
99
100 pub fn framework_node(node_name: &str) -> String {
102 format!("configs/nodes/@mecha10/{}/config.json", node_name)
103 }
104
105 pub fn local_node(node_name: &str) -> String {
107 format!("configs/nodes/@local/{}/config.json", node_name)
108 }
109
110 pub fn project_node(node_name: &str) -> String {
112 format!("configs/nodes/{}/config.json", node_name)
113 }
114}
115
116pub mod docker {
118 pub const DIR: &str = "docker";
120
121 pub const COMPOSE_FILE: &str = "docker/docker-compose.yml";
123
124 pub const COMPOSE_REMOTE_FILE: &str = "docker/docker-compose.remote.yml";
126
127 pub const DOCKERFILE_REMOTE: &str = "docker/Dockerfile.remote";
129
130 pub const DOCKERFILE_ROBOT_BUILDER: &str = "docker/robot-builder.Dockerfile";
132}
133
134pub mod env {
136 pub const EXAMPLE: &str = ".env.example";
138
139 pub const FILE: &str = ".env";
141}
142
143pub mod meta {
145 pub const README: &str = "README.md";
147
148 pub const GITIGNORE: &str = ".gitignore";
150
151 pub const PACKAGE_JSON: &str = "package.json";
153
154 pub const REQUIREMENTS_TXT: &str = "requirements.txt";
156
157 pub const LS_LINT_CONFIG: &str = ".ls-lint.yml";
159}
160
161pub mod rust {
163 pub const CARGO_TOML: &str = "Cargo.toml";
165
166 pub const CARGO_LOCK: &str = "Cargo.lock";
168
169 pub const MAIN_RS: &str = "src/main.rs";
171
172 pub const LIB_RS: &str = "src/lib.rs";
174
175 pub const BUILD_RS: &str = "build.rs";
177
178 pub const RUSTFMT_TOML: &str = "rustfmt.toml";
180
181 pub const CARGO_CONFIG_DIR: &str = ".cargo";
183
184 pub const CARGO_CONFIG: &str = ".cargo/config.toml";
186}
187
188pub mod model {
190 pub const ONNX_FILE: &str = "model.onnx";
192
193 pub const LABELS_FILE: &str = "labels.txt";
195
196 pub const CONFIG_FILE: &str = "config.json";
198}
199
200pub mod user {
206 use std::path::PathBuf;
207
208 pub const MECHA10_DIR: &str = ".mecha10";
210
211 pub const CREDENTIALS_FILE: &str = ".mecha10/credentials.json";
213
214 pub const TEMPLATES_DIR: &str = ".mecha10/templates";
216
217 pub const SIMULATION_DIR: &str = ".mecha10/simulation";
219
220 pub const SIMULATION_CURRENT: &str = ".mecha10/simulation/current";
222
223 pub const SIMULATION_VERSION: &str = ".mecha10/simulation/version";
225
226 pub const BIN_DIR: &str = ".mecha10/bin";
228
229 pub fn mecha10_dir() -> PathBuf {
231 dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")).join(MECHA10_DIR)
232 }
233
234 pub fn templates_dir() -> PathBuf {
236 mecha10_dir().join("templates")
237 }
238
239 pub fn simulation_dir() -> PathBuf {
241 mecha10_dir().join("simulation")
242 }
243
244 pub fn simulation_current() -> PathBuf {
246 mecha10_dir().join("simulation/current")
247 }
248
249 pub fn bin_dir() -> PathBuf {
251 mecha10_dir().join("bin")
252 }
253
254 pub fn bin(name: &str) -> PathBuf {
256 bin_dir().join(name)
257 }
258
259 pub fn cargo_bin(name: &str) -> PathBuf {
261 mecha10_core::fs_utils::home_subpath(&[".cargo", "bin", name])
262 }
263
264 pub fn local_bin(name: &str) -> PathBuf {
266 mecha10_core::fs_utils::home_subpath(&[".local", "bin", name])
267 }
268}
269
270pub mod container {
276 pub const PROJECT_ROOT: &str = "/project";
278
279 pub const FRAMEWORK_ROOT: &str = "/mecha10";
281
282 pub const APP_ROOT: &str = "/app";
284
285 pub const REMOTE_CONFIG: &str = "/app/mecha10.json";
287
288 pub fn project_path(relative: &str) -> String {
290 format!("{}/{}", PROJECT_ROOT, relative)
291 }
292
293 pub fn framework_path(relative: &str) -> String {
295 format!("{}/{}", FRAMEWORK_ROOT, relative)
296 }
297}
298
299pub mod framework {
305 pub const PACKAGES_DIR: &str = "packages";
307
308 pub const NODES_DIR: &str = "packages/nodes";
310
311 pub const DRIVERS_DIR: &str = "packages/drivers";
313
314 pub const SERVICES_DIR: &str = "packages/services";
316
317 pub const SIMULATION_DIR: &str = "packages/simulation";
319
320 pub const SIMULATION_MODELS_DIR: &str = "packages/simulation/models";
322
323 pub const SIMULATION_ENVIRONMENTS_DIR: &str = "packages/simulation/environments";
325
326 pub const ROBOT_TASKS_DIR: &str = "packages/simulation/environments/robot-tasks";
328
329 pub const ROBOT_TASKS_CATALOG: &str = "packages/simulation/environments/robot-tasks/catalog.json";
331
332 pub const TASKRUNNER_DIR: &str = "packages/taskrunner";
334
335 pub const RL_TRAINER_DIR: &str = "packages/rl-trainer";
337
338 pub const ROOT_MARKER_DIR: &str = "packages/core";
340
341 pub const RELEASE_BINARY: &str = "target/release/mecha10";
343
344 pub const DEBUG_BINARY: &str = "target/debug/mecha10";
346
347 pub const NODE_RUNNER_RELEASE: &str = "target/release/mecha10-node-runner";
349
350 pub const NODE_RUNNER_DEBUG: &str = "target/debug/mecha10-node-runner";
352
353 pub fn node_config(node_name: &str) -> String {
355 format!("packages/nodes/{}/configs/config.json", node_name)
356 }
357}
358
359pub mod urls {
365 pub const MECHA10_API_URL: &str = "https://mecha.industries/api";
373
374 pub fn mecha10_api_base_url() -> String {
379 std::env::var("MECHA10_API_URL")
380 .unwrap_or_else(|_| MECHA10_API_URL.to_string())
381 .trim_end_matches('/')
382 .to_string()
383 }
384
385 pub const AUTH_URL: &str = "https://mecha.industries/api/auth";
387}
388
389pub fn target_path(profile: &str, binary: &str) -> String {
395 format!("target/{}/{}", profile, binary)
396}
397
398pub fn target_path_with_triple(target: Option<&str>, profile: &str) -> String {
400 match target {
401 Some(t) => format!("target/{}/{}", t, profile),
402 None => format!("target/{}", profile),
403 }
404}
405
406pub fn find_project_root(start: &Path) -> Option<PathBuf> {
408 let mut current = start.to_path_buf();
409 loop {
410 if current.join(PROJECT_CONFIG).exists() {
411 return Some(current);
412 }
413 if !current.pop() {
414 return None;
415 }
416 }
417}
418
419pub fn is_project_dir(dir: &Path) -> bool {
421 dir.join(PROJECT_CONFIG).exists()
422}
423
424#[cfg(test)]
425mod tests {
426 use super::*;
427
428 #[test]
429 fn test_config_paths() {
430 assert_eq!(
431 config::framework_node("object-detector"),
432 "configs/nodes/@mecha10/object-detector/config.json"
433 );
434 assert_eq!(
435 config::local_node("my-node"),
436 "configs/nodes/@local/my-node/config.json"
437 );
438 assert_eq!(config::project_node("camera"), "configs/nodes/camera/config.json");
439 }
440
441 #[test]
442 fn test_target_path() {
443 assert_eq!(target_path("release", "my-node"), "target/release/my-node");
444 assert_eq!(
445 target_path_with_triple(Some("aarch64-unknown-linux-gnu"), "release"),
446 "target/aarch64-unknown-linux-gnu/release"
447 );
448 assert_eq!(target_path_with_triple(None, "debug"), "target/debug");
449 }
450
451 #[test]
452 fn test_container_paths() {
453 assert_eq!(
454 container::project_path("configs/test.json"),
455 "/project/configs/test.json"
456 );
457 assert_eq!(container::framework_path("packages/core"), "/mecha10/packages/core");
458 }
459
460 #[test]
461 fn test_mecha10_api_base_url_default() {
462 let _guard = ENV_MUTEX.lock().unwrap();
464 std::env::remove_var("MECHA10_API_URL");
465 assert_eq!(urls::mecha10_api_base_url(), "https://mecha.industries/api");
466 }
467
468 #[test]
469 fn test_mecha10_api_base_url_env_override() {
470 let _guard = ENV_MUTEX.lock().unwrap();
471 std::env::set_var("MECHA10_API_URL", "http://localhost:4000/api/");
472 assert_eq!(urls::mecha10_api_base_url(), "http://localhost:4000/api");
473 std::env::remove_var("MECHA10_API_URL");
474 }
475
476 static ENV_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
479}