credence_lib/util/
uri_path.rs

1use super::constants::*;
2
3use std::str::*;
4
5/// URI path segments.
6pub fn uri_path_segments(uri_path: &str) -> Split<'_, char> {
7    uri_path.split(PATH_SEPARATOR)
8}
9
10/// The last segment in the URI path.
11pub fn uri_path_last_segment(uri_path: &str) -> &str {
12    match uri_path.rfind(PATH_SEPARATOR) {
13        Some(last_slash) => &uri_path[last_slash + 1..],
14        None => uri_path,
15    }
16}
17
18/// Whether the URI path has any segment that begins with ".".
19pub fn uri_path_has_hidden_segment(uri_path: &str) -> bool {
20    for segment in uri_path_segments(uri_path) {
21        if segment.starts_with(HIDDEN_PATH_PREFIX) {
22            return true;
23        }
24    }
25
26    false
27}
28
29/// Join URI paths.
30pub fn uri_path_join(uri_path_1: &str, uri_path_2: &str) -> String {
31    uri_path_1.trim_end_matches(PATH_SEPARATOR).to_string() + PATH_SEPARATOR_STRING + uri_path_2
32}
33
34/// Up one segment for the URI path.
35pub fn uri_path_parent(uri_path: &str) -> &str {
36    let mut last_slash = uri_path.rfind(PATH_SEPARATOR).unwrap_or(1);
37    if last_slash == 0 {
38        last_slash = 1
39    }
40    &uri_path[..last_slash]
41}