2025-06-16 16:46:22 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2025-06-23 20:27:16 +02:00
|
|
|
use crate::errors::{NyxError, NyxResult};
|
2025-06-16 16:46:22 +02:00
|
|
|
|
2025-06-23 20:27:16 +02:00
|
|
|
/// Determine `<project-name, path/to/<project>.sqlite>`.
|
2025-06-16 16:46:22 +02:00
|
|
|
pub fn get_project_info(
|
2025-06-23 20:27:16 +02:00
|
|
|
project_path: &Path,
|
|
|
|
|
config_dir: &Path,
|
|
|
|
|
) -> NyxResult<(String, PathBuf)> {
|
|
|
|
|
|
2025-06-17 17:42:41 +02:00
|
|
|
let project_name = project_path
|
|
|
|
|
.file_name()
|
2025-06-23 20:27:16 +02:00
|
|
|
.and_then(|n| n.to_str())
|
|
|
|
|
.ok_or_else(|| NyxError::Other("Unable to determine project name".into()))?;
|
|
|
|
|
|
2025-06-17 17:42:41 +02:00
|
|
|
let db_name = sanitize_project_name(project_name);
|
|
|
|
|
let db_path = config_dir.join(format!("{}.sqlite", db_name));
|
2025-06-23 20:27:16 +02:00
|
|
|
|
|
|
|
|
Ok((project_name.to_owned(), db_path))
|
2025-06-16 16:46:22 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-23 20:27:16 +02:00
|
|
|
pub fn sanitize_project_name(name: &str) -> String {
|
2025-06-17 17:42:41 +02:00
|
|
|
name.to_lowercase()
|
|
|
|
|
.chars()
|
2025-06-23 20:27:16 +02:00
|
|
|
.map(|c| match c {
|
|
|
|
|
' ' | '\t' | '\n' | '\r' => '_',
|
|
|
|
|
c if c.is_alphanumeric() || c == '_' || c == '-' => c,
|
|
|
|
|
_ => '_',
|
2025-06-17 17:42:41 +02:00
|
|
|
})
|
|
|
|
|
.collect::<String>()
|
|
|
|
|
.split('_')
|
|
|
|
|
.filter(|s| !s.is_empty())
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
.join("_")
|
2025-06-16 16:46:22 +02:00
|
|
|
}
|