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