rush_sync_server/server/utils/
validation.rs1use crate::core::prelude::*;
2use crate::server::types::ServerInfo;
3use std::collections::HashMap;
4
5pub fn find_server<'a>(
6 servers: &'a HashMap<String, ServerInfo>,
7 identifier: &str,
8) -> Result<&'a ServerInfo> {
9 if let Ok(index) = identifier.parse::<usize>() {
10 if index > 0 && index <= servers.len() {
11 let mut server_list: Vec<_> = servers.values().collect();
12 server_list.sort_by(|a, b| a.created_at.cmp(&b.created_at));
13 return server_list
14 .get(index - 1)
15 .copied()
16 .ok_or_else(|| AppError::Validation("Server index out of range".to_string()));
17 }
18 }
19
20 for server in servers.values() {
21 if server.name == identifier || server.id.starts_with(identifier) {
22 return Ok(server);
23 }
24 }
25
26 Err(AppError::Validation(format!(
27 "Server '{}' nicht gefunden",
28 identifier
29 )))
30}
31
32pub fn validate_server_name(name: &str) -> Result<()> {
33 if name.is_empty() {
34 return Err(AppError::Validation(
35 "Server name cannot be empty".to_string(),
36 ));
37 }
38 if name.len() > 50 {
39 return Err(AppError::Validation("Server name too long".to_string()));
40 }
41 if !name
42 .chars()
43 .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
44 {
45 return Err(AppError::Validation(
46 "Server name can only contain alphanumeric characters, hyphens and underscores"
47 .to_string(),
48 ));
49 }
50 Ok(())
51}
52
53pub fn validate_port(port: u16) -> Result<()> {
54 if port < 1024 {
55 return Err(AppError::Validation("Port must be >= 1024".to_string()));
56 }
57 Ok(())
58}