pub(crate) fn id_from_path<'a>(path: &'a str, prefix: &str) -> Option<&'a str> {
path.strip_prefix(prefix)
.filter(|id| !id.is_empty() && !id.contains('/'))
}
pub(crate) fn id_from_path_with_suffix<'a>(
path: &'a str,
prefix: &str,
suffix: &str,
) -> Option<&'a str> {
path.strip_prefix(prefix)
.and_then(|rest| rest.strip_suffix(suffix))
.filter(|id| !id.is_empty() && !id.contains('/'))
}
#[cfg(test)]
mod tests {
use super::{id_from_path, id_from_path_with_suffix};
#[test]
fn id_from_path_requires_a_single_segment() {
assert_eq!(id_from_path("/v1/x/abc", "/v1/x/"), Some("abc"));
assert_eq!(id_from_path("/v1/x/", "/v1/x/"), None);
assert_eq!(id_from_path("/v1/x/a/b", "/v1/x/"), None);
assert_eq!(id_from_path("/other/abc", "/v1/x/"), None);
}
#[test]
fn id_from_path_with_suffix_brackets_the_id() {
assert_eq!(
id_from_path_with_suffix("/v1/x/abc/cancel", "/v1/x/", "/cancel"),
Some("abc")
);
assert_eq!(
id_from_path_with_suffix("/v1/x/abc", "/v1/x/", "/cancel"),
None
);
assert_eq!(
id_from_path_with_suffix("/v1/x//cancel", "/v1/x/", "/cancel"),
None
);
}
}