1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use Cow;
use Write;
const SLUGIFY2_SYMBOLS: &str = "[\t !\"#$%&'()*-/<=>?@[\\]^_`{|},.:]+";
const NON_ASCII_CHARACTER_THRESHOLD: u32 = 128;
/// Converts a nickname to a URL-safe slug format for API requests
///
/// This function handles special characters and non-ASCII characters in nicknames
/// by encoding them into a format that can be safely used in URLs. Characters that
/// are not ASCII or are in `SLUGIFY_SYMBOLS` are converted to their Unicode code points
/// surrounded by hyphens.
///
/// # Arguments
///
/// * `nickname` - The player nickname to slugify
///
/// # Returns
///
/// Returns `Cow<'_, str>` - Borrowed if no conversion needed, Owned if conversion occurred
///
/// # Examples
///
/// ```
/// use ddapi_rs::prelude::slugify2;
///
/// // ASCII-only nicknames without special symbols are returned as-is
/// assert_eq!(slugify2("Player1"), "Player1");
///
/// // Special symbols and non-ASCII characters are encoded
/// assert_eq!(slugify2("Player@"), "Player-64-");
/// assert_eq!(slugify2("玩家"), "-29609--23478-");
///
/// // Mixed characters
/// assert_eq!(slugify2("Test_Player"), "Test-95-Player");
/// ```
/// Encodes a nickname for safe use in URLs
///
/// This function ensures that nicknames containing special characters, spaces,
/// or non-ASCII characters are properly URL-encoded. ASCII nicknames without
/// control characters are returned as-is for better performance.
///
/// # Arguments
///
/// * `nickname` - The player nickname to URL-encode
///
/// # Returns
///
/// Returns `Cow<'_, str>` -
/// - `Cow::Borrowed` if the nickname is already URL-safe (ASCII without control characters)
/// - `Cow::Owned` with URL-encoded string if encoding is required
///
/// # Examples
///
/// ```
/// use ddapi_rs::prelude::encode;
///
/// // Safe ASCII nicknames are returned without changes
/// assert_eq!(encode("Player1"), "Player1");
/// assert_eq!(encode("abc_XYZ"), "abc_XYZ");
///
/// // Characters requiring encoding are properly handled
/// assert_eq!(encode("Player Server"), "Player%20Server");
/// assert_eq!(encode("Player@Server"), "Player%40Server");
/// assert_eq!(encode("玩家"), "%E7%8E%A9%E5%AE%B6");
/// assert_eq!(encode("emoji🎮"), "emoji%F0%9F%8E%AE");
///
/// // Special cases
/// assert_eq!(encode(""), "");
/// assert_eq!(encode("a b"), "a%20b");
/// ```