cicero_path_core/
lib.rs

1use std::{path::PathBuf, sync::LazyLock};
2
3
4/// Root of your Cargo workspace. This is the directory where
5/// your `Cargo.lock` file and `target/` folder is placed.
6///
7/// This is equivalent to `repo_path!()` in most projects.
8/// The two differ, if your Rust code lives in a subfolder of a larger repository.
9pub fn workspace_dir() -> PathBuf {
10    metadata::CARGO
11        .workspace_root
12        .clone()
13        .into_std_path_buf()
14}
15
16/// The `target/` directory of your workspace.
17///
18/// Compile artifacts can be found in here.
19pub fn target_dir() -> PathBuf {
20    metadata::CARGO
21        .target_directory
22        .clone()
23        .into_std_path_buf()
24}
25
26pub static USER_WORKSPACE_CARGO_TOML: LazyLock<PathBuf> = LazyLock::new(|| {
27    let output = std::process::Command::new(env!("CARGO"))
28        .arg("locate-project")
29        .arg("--workspace")
30        .arg("--message-format=plain")
31        .output()
32        .unwrap()
33        .stdout;
34
35    let output = std::str::from_utf8(&output).unwrap()
36        .trim();
37
38    PathBuf::from(output)
39});
40
41
42pub mod metadata {
43    use std::sync::LazyLock;
44
45    pub static CARGO: LazyLock<cargo_metadata::Metadata> = LazyLock::new(|| {
46        cargo_metadata::MetadataCommand::new()
47            .manifest_path(super::USER_WORKSPACE_CARGO_TOML.as_path())
48            .exec()
49            .expect("Failed to gather Cargo metadata.")
50    });
51}
52
53
54/// Paths used by Cicero.
55pub mod cicero {
56    use std::path::PathBuf;
57
58    /// The directory where Cicero places its artifacts.
59    /// This is a sub-path of the `target/` folder.
60    pub fn target() -> PathBuf {
61        super::target_dir().join("cicero")
62    }
63
64    /// Directory used for internal logic of Cicero.
65    pub fn internal() -> PathBuf {
66        target().join(".internal")
67    }
68}