Skip to main content

codetether_agent/rlm/oracle/storage/
helpers.rs

1//! Path resolution and spool-directory helpers.
2//!
3//! Provides the default spool path and endpoint normalization
4//! used when constructing the storage layer.
5//!
6//! # Examples
7//!
8//! ```ignore
9//! let dir = default_spool_dir();
10//! assert!(dir.ends_with("traces/pending"));
11//! ```
12
13use std::path::PathBuf;
14
15/// Resolve the directory where oracle traces are buffered
16/// before upload to remote storage.
17///
18/// Reads `CODETETHER_ORACLE_SPOOL_DIR` if set; otherwise
19/// falls back to `$HOME/.codetether/traces/pending`.
20///
21/// # Examples
22///
23/// ```ignore
24/// std::env::set_var("CODETETHER_ORACLE_SPOOL_DIR", "/tmp/spool");
25/// assert_eq!(default_spool_dir(), PathBuf::from("/tmp/spool"));
26/// ```
27pub fn default_spool_dir() -> PathBuf {
28    if let Ok(path) = std::env::var("CODETETHER_ORACLE_SPOOL_DIR") {
29        return PathBuf::from(path);
30    }
31    let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
32    PathBuf::from(home)
33        .join(".codetether")
34        .join("traces")
35        .join("pending")
36}
37
38/// Ensure an endpoint string starts with a scheme.
39///
40/// Returns the input unchanged when it already begins with
41/// `http://` or `https://`. Otherwise prepends `https://` when
42/// `secure` is true, or `http://` when false.
43///
44/// # Examples
45///
46/// ```ignore
47/// assert_eq!(normalize_endpoint("minio:9000", false), "http://minio:9000");
48/// assert_eq!(normalize_endpoint("https://s3.aws", true), "https://s3.aws");
49/// ```
50pub fn normalize_endpoint(endpoint: &str, secure: bool) -> String {
51    let endpoint = endpoint.trim_end_matches('/');
52    if endpoint.starts_with("http://") || endpoint.starts_with("https://") {
53        endpoint.to_string()
54    } else if secure {
55        format!("https://{endpoint}")
56    } else {
57        format!("http://{endpoint}")
58    }
59}