Skip to main content

lemma/
deps.rs

1//! Dependency cache layout under `<workdir>/.deps/`, shared by CLI fetch and LSP.
2
3use std::path::{Path, PathBuf};
4
5#[must_use]
6pub fn lemma_deps_dir(workdir: &Path) -> PathBuf {
7    workdir.join(".deps")
8}
9
10/// Relative path under `.deps/` for a fetched registry bundle (`GET /@id.lemma` identity includes `@`).
11#[must_use]
12pub fn relative_dependency_cache_path(registry_attribute_display: &str) -> PathBuf {
13    let last_slash_position = registry_attribute_display.rfind('/');
14    let (directory_segment, leaf_segment) = match last_slash_position {
15        Some(slice_index) => (
16            &registry_attribute_display[..slice_index],
17            &registry_attribute_display[slice_index + 1..],
18        ),
19        None => ("", registry_attribute_display),
20    };
21    let filename_with_suffix = format!("{}.lemma", leaf_segment);
22    if directory_segment.is_empty() {
23        PathBuf::from(filename_with_suffix)
24    } else {
25        PathBuf::from(directory_segment).join(filename_with_suffix)
26    }
27}
28
29/// Derive registry dependency id from a `.deps/` `.lemma` file path (same string CLI `load_batch` passes as `dependency_id`).
30#[must_use]
31pub fn dependency_identifier_from_dependency_path(
32    workdir: &Path,
33    dependency_path: &Path,
34) -> String {
35    let deps_dir = lemma_deps_dir(workdir);
36    let relative = dependency_path
37        .strip_prefix(&deps_dir)
38        .unwrap_or(dependency_path.as_ref());
39    let without_ext = relative.with_extension("");
40    without_ext.to_string_lossy().to_string()
41}
42
43/// Absolute path where `lemma fetch` writes the bundle for this registry qualifier.
44#[must_use]
45pub fn dependency_cache_file(workdir: &Path, registry_qualifier: &str) -> PathBuf {
46    lemma_deps_dir(workdir).join(relative_dependency_cache_path(registry_qualifier))
47}