Sanitize accents out of folder name
This commit is contained in:
parent
49f3dd6386
commit
6b8963a81a
2 changed files with 10 additions and 1 deletions
|
|
@ -50,6 +50,7 @@ tokio-util = "0.7"
|
||||||
futures-util = "0.3"
|
futures-util = "0.3"
|
||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
dotenvy = "0.15"
|
dotenvy = "0.15"
|
||||||
|
unicode-normalization = "0.1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio-test = "0.4"
|
tokio-test = "0.4"
|
||||||
|
|
|
||||||
10
src/utils.rs
10
src/utils.rs
|
|
@ -1,5 +1,7 @@
|
||||||
//! Shared utility functions.
|
//! Shared utility functions.
|
||||||
|
|
||||||
|
use unicode_normalization::{char::is_combining_mark, UnicodeNormalization};
|
||||||
|
|
||||||
/// Create a safe snake_case 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:
|
||||||
|
|
@ -15,8 +17,14 @@
|
||||||
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);
|
||||||
|
|
||||||
|
// Remove accents by decomposing to NFD and filtering combining marks
|
||||||
|
let without_accents: String = base
|
||||||
|
.nfd()
|
||||||
|
.filter(|c| !is_combining_mark(*c))
|
||||||
|
.collect();
|
||||||
|
|
||||||
// Convert to lowercase and replace unsafe characters with underscores
|
// Convert to lowercase and replace unsafe characters with underscores
|
||||||
let sanitized: String = base
|
let sanitized: String = without_accents
|
||||||
.to_lowercase()
|
.to_lowercase()
|
||||||
.chars()
|
.chars()
|
||||||
.map(|c| {
|
.map(|c| {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue