use crate::vfs::vercel_kv_types::*;
#[macro_export]
macro_rules! log_debug {
($($arg:tt)*) => {
log::debug!($($arg)*)
};
}
pub fn normalize_path(path: &str) -> String {
let normalized = path.trim_start_matches('/').to_string();
normalized
}
pub fn is_directory_path(path: &str) -> bool {
let is_explicit_dir = path.ends_with('/') || path.is_empty();
if is_explicit_dir {
log_debug!("Path '{}' is explicitly a directory", path);
return true;
}
log_debug!(
"Path '{}' might be a file or directory without trailing slash",
path
);
false
}
pub fn get_parent_directory(path: &str) -> String {
let path = path.trim_end_matches('/');
let parent = match path.rfind('/') {
Some(idx) => path[..=idx].to_string(),
None => "".to_string(),
};
parent
}
pub fn get_filename(path: &str) -> String {
let path = path.trim_end_matches('/');
let filename = match path.rfind('/') {
Some(idx) => path[idx + 1..].to_string(),
None => path.to_string(), };
filename
}
pub fn get_key_with_suffix(path: &str, suffix: &str) -> String {
let key = format!("{}{}", path, suffix);
key
}
pub fn get_content_key(path: &str) -> String {
get_key_with_suffix(path, FILE_CONTENT_SUFFIX)
}
pub fn get_directory_marker_key(path: &str) -> String {
let normalized = normalize_path(path);
if normalized.is_empty() {
DIRECTORY_MARKER_SUFFIX.trim_start_matches('/').to_string()
} else {
format!(
"{}{}",
normalized.trim_end_matches('/'),
DIRECTORY_MARKER_SUFFIX
)
}
}
pub fn get_github_tree_cache_key(owner: &str, repo: &str, branch: &str, recursive: bool) -> String {
let recursive_flag = if recursive { "1" } else { "0" };
format!(
"{}/{}@{}@{}{}",
owner, repo, branch, recursive_flag, GITHUB_TREE_CACHE_SUFFIX
)
}
pub fn guess_file_type(path: &str) -> String {
let extension = path
.rsplit('.')
.next()
.filter(|&ext| !ext.contains('/'))
.map(|ext| ext.to_lowercase());
let file_type = match extension {
Some(ext) => match ext.as_str() {
"txt" => "text/plain",
"html" | "htm" => "text/html",
"css" => "text/css",
"js" => "application/javascript",
"json" => "application/json",
"xml" => "application/xml",
"png" => "image/png",
"jpg" | "jpeg" => "image/jpeg",
"gif" => "image/gif",
"svg" => "image/svg+xml",
"pdf" => "application/pdf",
"md" => "text/markdown",
"ini" => "text/plain",
"yaml" | "yml" => "application/yaml",
"conf" => "text/plain",
"rs" => "text/rust", "toml" => "application/toml", "wasm" => "application/wasm", _ => "application/octet-stream",
}
.to_string(),
None => "application/octet-stream".to_string(),
};
file_type
}