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
//! Path utility functions.
/// Canonicalize an HTTP path by collapsing duplicate slashes and removing
/// trailing slashes.
///
/// This function normalizes a path into a single canonical representation by:
///
/// - Collapsing consecutive `/` characters into a single `/`
/// - Removing trailing slashes
/// - Preserving the root path (`"/"`)
///
/// If normalization would result in an empty string (which happens when the
/// input contains only slashes), the function returns `"/"`.
///
/// This ensures that logically equivalent paths map to the same canonical
/// value. For example, `/api`, `/api/`, `/api//`, and `/api///` all normalize
/// to `/api`.
///
/// # Examples
///
/// ```
/// use fastrust::canonicalize_path;
///
/// assert_eq!(canonicalize_path("/api"), "/api");
/// assert_eq!(canonicalize_path("/api/"), "/api");
/// assert_eq!(canonicalize_path("/api///"), "/api");
/// assert_eq!(canonicalize_path("/api//users"), "/api/users");
/// assert_eq!(canonicalize_path("///api///users//"), "/api/users");
/// assert_eq!(canonicalize_path("/"), "/");
/// assert_eq!(canonicalize_path("///"), "/");
/// ```
///
/// # Notes
///
/// - Consecutive slashes are collapsed into a single `/`.
/// - Trailing slashes are removed except for the root path.
/// - The function allocates a new `String` to produce the normalized path.