pub const PROTOCOL_FILE: &str = "file://";
pub const PROTOCOL_NAME_FILE: &str = "file";
pub fn parse_storage_identifier(identifier: &str) -> (&str, &str) {
if identifier.starts_with(PROTOCOL_FILE) {
(
PROTOCOL_NAME_FILE,
identifier.trim_start_matches(PROTOCOL_FILE),
)
} else {
(PROTOCOL_NAME_FILE, identifier)
}
}
pub fn has_protocol(identifier: &str, protocol: &str) -> bool {
protocol == PROTOCOL_NAME_FILE && identifier.starts_with(PROTOCOL_FILE)
}
pub fn extract_path(identifier: &str) -> &str {
parse_storage_identifier(identifier).1
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_file_protocol() {
let (protocol, path) = parse_storage_identifier("file:///path/to/db");
assert_eq!(protocol, "file");
assert_eq!(path, "/path/to/db");
}
#[test]
fn test_parse_no_protocol() {
let (protocol, path) = parse_storage_identifier("my_database.db");
assert_eq!(protocol, "file");
assert_eq!(path, "my_database.db");
}
#[test]
fn test_has_protocol() {
assert!(has_protocol("file:///path/to/db", "file"));
assert!(!has_protocol("my_database.db", "file"));
}
#[test]
fn test_extract_path() {
assert_eq!(extract_path("file:///path/to/db"), "/path/to/db");
assert_eq!(extract_path("my_database.db"), "my_database.db");
}
}