|
| 1 | +// Copyright (C) 2020 - 2022, J2 Innovations |
| 2 | + |
| 3 | +//! Haystack tag name utilities. |
| 4 | +//! |
| 5 | +//! Provides functions for validating and converting arbitrary strings into |
| 6 | +//! valid Haystack tag names. |
| 7 | +//! |
| 8 | +//! A valid tag name must match the grammar: |
| 9 | +//! ```text |
| 10 | +//! <alphaLo> (<alphaLo> | <alphaHi> | <digit> | '_')* |
| 11 | +//! ``` |
| 12 | +//! |
| 13 | +//! For more information see <https://project-haystack.org/doc/docHaystack/Kinds> |
| 14 | +
|
| 15 | +/// Returns `true` if `name` is already a valid Haystack tag name. |
| 16 | +/// |
| 17 | +/// A valid tag name starts with a lowercase ASCII letter followed by zero or |
| 18 | +/// more ASCII letters, digits, or underscores. |
| 19 | +/// |
| 20 | +/// # Examples |
| 21 | +/// ``` |
| 22 | +/// use libhaystack::util::is_valid_tag_name; |
| 23 | +/// |
| 24 | +/// assert!(is_valid_tag_name("aValidTag123")); |
| 25 | +/// assert!(!is_valid_tag_name("AValidTag")); |
| 26 | +/// assert!(!is_valid_tag_name("1invalid")); |
| 27 | +/// assert!(!is_valid_tag_name("")); |
| 28 | +/// ``` |
| 29 | +pub fn is_valid_tag_name(name: &str) -> bool { |
| 30 | + let mut chars = name.chars(); |
| 31 | + match chars.next() { |
| 32 | + Some(first) if first.is_ascii_lowercase() => { |
| 33 | + chars.all(|c| c.is_ascii_alphanumeric() || c == '_') |
| 34 | + } |
| 35 | + _ => false, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// Converts an arbitrary string into a valid Haystack tag name. |
| 40 | +/// |
| 41 | +/// The conversion follows the same rules as the TypeScript `toTagName` utility: |
| 42 | +/// |
| 43 | +/// 1. If the string is already a valid tag name, it is returned unchanged. |
| 44 | +/// 2. `.`, `-`, and `/` are replaced with `_`; if at position 0 they become |
| 45 | +/// `v`; if at the last position they are dropped. |
| 46 | +/// 3. A leading digit or `_` is prefixed with `v`. |
| 47 | +/// 4. All remaining invalid characters are stripped. |
| 48 | +/// 5. Spaces trigger camelCase conversion: each word after the first has its |
| 49 | +/// first letter uppercased (provided it is a lowercase letter). |
| 50 | +/// 6. A leading run of uppercase letters on the first word is lowercased. |
| 51 | +/// 7. Returns `"empty"` if no valid characters remain. |
| 52 | +/// |
| 53 | +/// # Examples |
| 54 | +/// ``` |
| 55 | +/// use libhaystack::util::to_tag_name; |
| 56 | +/// |
| 57 | +/// assert_eq!(to_tag_name("oh what a time to be alive"), "ohWhatATimeToBeAlive"); |
| 58 | +/// assert_eq!(to_tag_name("AIR TEMP"), "airTEMP"); |
| 59 | +/// assert_eq!(to_tag_name("1test"), "v1test"); |
| 60 | +/// assert_eq!(to_tag_name(""), "empty"); |
| 61 | +/// ``` |
| 62 | +pub fn to_tag_name(name: &str) -> String { |
| 63 | + if is_valid_tag_name(name) { |
| 64 | + return name.to_string(); |
| 65 | + } |
| 66 | + |
| 67 | + // Step 1: Replace `.`, `-`, `/` with `_`, `v` (at pos 0), or drop (at last pos). |
| 68 | + let char_count = name.chars().count(); |
| 69 | + let last_idx = char_count.saturating_sub(1); |
| 70 | + |
| 71 | + let mut step1 = String::with_capacity(name.len()); |
| 72 | + for (i, c) in name.chars().enumerate() { |
| 73 | + match c { |
| 74 | + '.' | '-' | '/' => { |
| 75 | + if i == 0 { |
| 76 | + step1.push('v'); |
| 77 | + } else if i != last_idx { |
| 78 | + step1.push('_'); |
| 79 | + } |
| 80 | + // last position: drop the character |
| 81 | + } |
| 82 | + _ => step1.push(c), |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Step 2: Prefix with `v` when the string starts with a digit or `_`. |
| 87 | + let step2 = if step1.starts_with(|c: char| c.is_ascii_digit() || c == '_') { |
| 88 | + let mut s = String::with_capacity(step1.len() + 1); |
| 89 | + s.push('v'); |
| 90 | + s.push_str(&step1); |
| 91 | + s |
| 92 | + } else { |
| 93 | + step1 |
| 94 | + }; |
| 95 | + |
| 96 | + // Step 3: Remove all characters that are not ASCII alphanumeric, `_`, or space; trim. |
| 97 | + let step3 = step2 |
| 98 | + .chars() |
| 99 | + .filter(|&c| c.is_ascii_alphanumeric() || c == '_' || c == ' ') |
| 100 | + .collect::<String>(); |
| 101 | + let step3 = step3.trim(); |
| 102 | + |
| 103 | + // Step 4: Split on spaces, camelCase conversion. |
| 104 | + let result: String = step3 |
| 105 | + .split(' ') |
| 106 | + .filter(|part| !part.is_empty()) |
| 107 | + .enumerate() |
| 108 | + .map(|(i, part)| { |
| 109 | + let mut chars = part.chars(); |
| 110 | + let start = match chars.next() { |
| 111 | + Some(c) => c, |
| 112 | + None => return String::new(), |
| 113 | + }; |
| 114 | + |
| 115 | + if i == 0 { |
| 116 | + // Lowercase the leading run of uppercase letters on the first word. |
| 117 | + if start.is_ascii_uppercase() { |
| 118 | + let mut new_part = String::with_capacity(part.len()); |
| 119 | + let mut caps_prefix = true; |
| 120 | + for ch in part.chars() { |
| 121 | + if caps_prefix && ch.is_ascii_uppercase() { |
| 122 | + new_part.push(ch.to_ascii_lowercase()); |
| 123 | + } else { |
| 124 | + caps_prefix = false; |
| 125 | + new_part.push(ch); |
| 126 | + } |
| 127 | + } |
| 128 | + new_part |
| 129 | + } else { |
| 130 | + part.to_string() |
| 131 | + } |
| 132 | + } else { |
| 133 | + // Capitalize the first letter of subsequent words only when it is lowercase. |
| 134 | + if start.is_ascii_alphabetic() && start.is_ascii_lowercase() { |
| 135 | + let mut new_part = String::with_capacity(part.len()); |
| 136 | + new_part.push(start.to_ascii_uppercase()); |
| 137 | + new_part.push_str(&part[start.len_utf8()..]); |
| 138 | + new_part |
| 139 | + } else { |
| 140 | + part.to_string() |
| 141 | + } |
| 142 | + } |
| 143 | + }) |
| 144 | + .collect(); |
| 145 | + |
| 146 | + if result.is_empty() { |
| 147 | + "empty".to_string() |
| 148 | + } else { |
| 149 | + result |
| 150 | + } |
| 151 | +} |
0 commit comments