Validate a view name.
(name: &str)
| 59 | |
| 60 | /// Validate a view name. |
| 61 | fn validate_view_name(name: &str) -> Result<(), String> { |
| 62 | if name.is_empty() { |
| 63 | return Err("View name cannot be empty".to_string()); |
| 64 | } |
| 65 | |
| 66 | if name.len() > MAX_VIEW_NAME_LENGTH { |
| 67 | return Err(format!( |
| 68 | "View name cannot exceed {} characters", |
| 69 | MAX_VIEW_NAME_LENGTH |
| 70 | )); |
| 71 | } |
| 72 | |
| 73 | for c in INVALID_CHARS { |
| 74 | if name.contains(*c) { |
| 75 | let char_desc = match c { |
| 76 | ' ' => "spaces".to_string(), |
| 77 | '\0' => "null characters".to_string(), |
| 78 | _ => format!("'{}'", c), |
| 79 | }; |
| 80 | return Err(format!("View name cannot contain {}", char_desc)); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if name == "." || name == ".." { |
| 85 | return Err("View name cannot be '.' or '..'".to_string()); |
| 86 | } |
| 87 | |
| 88 | if name.starts_with('.') { |
| 89 | return Err("View name cannot start with a dot".to_string()); |
| 90 | } |
| 91 | |
| 92 | if name.ends_with('.') { |
| 93 | return Err("View name cannot end with a dot".to_string()); |
| 94 | } |
| 95 | |
| 96 | Ok(()) |
| 97 | } |
| 98 | |
| 99 | // Split Command |
| 100 |