iceberg_rust_spec/util.rs
1/*!
2This module provides utility functions.
3*/
4use url::Url;
5
6/// Strips URL scheme and authority components from a path string
7///
8/// # Arguments
9/// * `path` - A string that may be a URL or plain path
10///
11/// # Returns
12/// The path component of the URL, or the original string if it's not a valid URL
13///
14/// # Examples
15/// ```
16/// use iceberg_rust_spec::util::strip_prefix;
17/// assert_eq!(strip_prefix("s3://bucket/path"), "/path");
18/// assert_eq!(strip_prefix("/plain/path"), "/plain/path");
19/// ```
20pub fn strip_prefix(path: &str) -> String {
21 match Url::parse(path) {
22 Ok(url) => String::from(url.path()),
23 Err(_) => String::from(path),
24 }
25}
26
27#[cfg(test)]
28mod tests {
29 use crate::util::strip_prefix;
30 #[test]
31 fn strip_prefix_behaves_as_expected() {
32 assert_eq!(strip_prefix("/a/b"), "/a/b");
33 assert_eq!(strip_prefix("file:///a/b"), "/a/b");
34 assert_eq!(strip_prefix("s3://bucket/a/b"), "/a/b");
35 assert_eq!(strip_prefix("gs://bucket/a/b"), "/a/b");
36 assert_eq!(strip_prefix("az://bucket/a/b"), "/a/b");
37 }
38}