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