Use snake_case for folder name
This commit is contained in:
parent
cc9ac97284
commit
c7304bb1c8
1 changed files with 12 additions and 9 deletions
21
src/utils.rs
21
src/utils.rs
|
|
@ -1,11 +1,13 @@
|
|||
//! Shared utility functions.
|
||||
|
||||
/// Create a safe folder name from person name or ID.
|
||||
/// Create a safe snake_case folder name from person name or ID.
|
||||
///
|
||||
/// This sanitizes user input to create safe filesystem paths by:
|
||||
/// - Using the person name if provided and non-empty, otherwise falling back to ID
|
||||
/// - Converting to lowercase
|
||||
/// - Replacing spaces with underscores
|
||||
/// - Replacing unsafe characters with underscores
|
||||
/// - Trimming whitespace
|
||||
/// - Trimming whitespace and underscores
|
||||
/// - Limiting length to 50 characters
|
||||
///
|
||||
/// Note: The frontend has a JavaScript version of this function that must be
|
||||
|
|
@ -13,11 +15,12 @@
|
|||
pub fn sanitize_folder_name(name: Option<&str>, id: &str) -> String {
|
||||
let base = name.filter(|n| !n.is_empty()).unwrap_or(id);
|
||||
|
||||
// Replace unsafe characters with underscores
|
||||
// Convert to lowercase and replace unsafe characters with underscores
|
||||
let sanitized: String = base
|
||||
.to_lowercase()
|
||||
.chars()
|
||||
.map(|c| {
|
||||
if c.is_alphanumeric() || c == '-' || c == '_' || c == ' ' {
|
||||
if c.is_alphanumeric() || c == '-' || c == '_' {
|
||||
c
|
||||
} else {
|
||||
'_'
|
||||
|
|
@ -25,8 +28,8 @@ pub fn sanitize_folder_name(name: Option<&str>, id: &str) -> String {
|
|||
})
|
||||
.collect();
|
||||
|
||||
// Trim whitespace and limit length
|
||||
let trimmed = sanitized.trim();
|
||||
// Trim whitespace/underscores and limit length
|
||||
let trimmed = sanitized.trim_matches(|c: char| c.is_whitespace() || c == '_');
|
||||
if trimmed.len() > 50 {
|
||||
trimmed[..50].to_string()
|
||||
} else {
|
||||
|
|
@ -40,7 +43,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_sanitize_folder_name_with_name() {
|
||||
assert_eq!(sanitize_folder_name(Some("John Doe"), "abc123"), "John Doe");
|
||||
assert_eq!(sanitize_folder_name(Some("John Doe"), "abc123"), "john_doe");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -53,7 +56,7 @@ mod tests {
|
|||
fn test_sanitize_folder_name_replaces_unsafe_chars() {
|
||||
assert_eq!(
|
||||
sanitize_folder_name(Some("John/Doe:Test"), "id"),
|
||||
"John_Doe_Test"
|
||||
"john_doe_test"
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -61,7 +64,7 @@ mod tests {
|
|||
fn test_sanitize_folder_name_trims_whitespace() {
|
||||
assert_eq!(
|
||||
sanitize_folder_name(Some(" John Doe "), "id"),
|
||||
"John Doe"
|
||||
"john_doe"
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue