Skip to main content

codewhale_runtime/
catalog_cache.rs

1use std::path::PathBuf;
2
3/// Filename under the CodeWhale `catalog` state dir.
4pub const CACHE_FILE: &str = "models-dev-catalog.json";
5
6/// Resolve the on-disk cache path under the CodeWhale `catalog` state dir.
7///
8/// Under `cfg(test)` this is confined the same way settings and config paths
9/// are: `resolve_state_dir` lives in `codewhale-config`, which is compiled as a
10/// plain dependency here and so has no view of this crate's isolated test root.
11/// Without this shield a test that never asked for the developer's catalog read
12/// their real `~/.codewhale/catalog` and rendered against whatever models they
13/// last fetched (#5359).
14#[must_use]
15pub fn cache_path() -> Option<PathBuf> {
16    #[cfg(any(test, all(feature = "test-thread-scoped-state", debug_assertions)))]
17    {
18        if !crate::test_support::guarded_environment_provides_state_paths() {
19            return Some(
20                crate::test_support::unsealed_test_state_root()
21                    .join("catalog")
22                    .join(CACHE_FILE),
23            );
24        }
25    }
26    codewhale_config::resolve_state_dir("catalog")
27        .ok()
28        .map(|dir| dir.join(CACHE_FILE))
29}
30
31pub fn valid_catalog_model_id(value: &str) -> bool {
32    !value.is_empty()
33        && value.len() <= 256
34        && value.bytes().any(|byte| byte.is_ascii_alphanumeric())
35        && value.bytes().all(|byte| {
36            byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b':' | b'/' | b'-' | b'~')
37        })
38}