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.
|
//! 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:
|
/// This sanitizes user input to create safe filesystem paths by:
|
||||||
/// - Using the person name if provided and non-empty, otherwise falling back to ID
|
/// - 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
|
/// - Replacing unsafe characters with underscores
|
||||||
/// - Trimming whitespace
|
/// - Trimming whitespace and underscores
|
||||||
/// - Limiting length to 50 characters
|
/// - Limiting length to 50 characters
|
||||||
///
|
///
|
||||||
/// Note: The frontend has a JavaScript version of this function that must be
|
/// 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 {
|
pub fn sanitize_folder_name(name: Option<&str>, id: &str) -> String {
|
||||||
let base = name.filter(|n| !n.is_empty()).unwrap_or(id);
|
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
|
let sanitized: String = base
|
||||||
|
.to_lowercase()
|
||||||
.chars()
|
.chars()
|
||||||
.map(|c| {
|
.map(|c| {
|
||||||
if c.is_alphanumeric() || c == '-' || c == '_' || c == ' ' {
|
if c.is_alphanumeric() || c == '-' || c == '_' {
|
||||||
c
|
c
|
||||||
} else {
|
} else {
|
||||||
'_'
|
'_'
|
||||||
|
|
@ -25,8 +28,8 @@ pub fn sanitize_folder_name(name: Option<&str>, id: &str) -> String {
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// Trim whitespace and limit length
|
// Trim whitespace/underscores and limit length
|
||||||
let trimmed = sanitized.trim();
|
let trimmed = sanitized.trim_matches(|c: char| c.is_whitespace() || c == '_');
|
||||||
if trimmed.len() > 50 {
|
if trimmed.len() > 50 {
|
||||||
trimmed[..50].to_string()
|
trimmed[..50].to_string()
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -40,7 +43,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sanitize_folder_name_with_name() {
|
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]
|
#[test]
|
||||||
|
|
@ -53,7 +56,7 @@ mod tests {
|
||||||
fn test_sanitize_folder_name_replaces_unsafe_chars() {
|
fn test_sanitize_folder_name_replaces_unsafe_chars() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
sanitize_folder_name(Some("John/Doe:Test"), "id"),
|
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() {
|
fn test_sanitize_folder_name_trims_whitespace() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
sanitize_folder_name(Some(" John Doe "), "id"),
|
sanitize_folder_name(Some(" John Doe "), "id"),
|
||||||
"John Doe"
|
"john_doe"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue