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
//! IRI string helpers shared by the upgrade and downgrade engines.
/// Returns the trailing segment of an IRI, after the rightmost `/`,
/// `#`, or `:`. URN identities (`urn:sbol:foo:bar:1`) use `:` as the
/// separator; HTTP IRIs use `/` or `#`. A plain `rsplit('/')` would
/// return the entire URN, which then concatenates into a malformed
/// nested IRI when used as a child segment.
pub(crate) fn last_iri_segment(iri: &str) -> &str {
let split = iri.rfind('/').max(iri.rfind('#')).max(iri.rfind(':'));
match split {
Some(idx) if idx + 1 < iri.len() => &iri[idx + 1..],
_ => iri,
}
}
#[cfg(test)]
mod tests {
use super::last_iri_segment;
#[test]
fn handles_http_paths() {
assert_eq!(
last_iri_segment("https://example.org/lab/promoter"),
"promoter"
);
}
#[test]
fn handles_hash_fragments() {
assert_eq!(last_iri_segment("http://example.org/ns#frag"), "frag");
}
#[test]
fn handles_pure_urns() {
// A `rsplit('/')` would return the entire URN, which then
// concatenates into a malformed nested IRI at the call site.
assert_eq!(last_iri_segment("urn:sbol:design:promoter:1"), "1");
}
#[test]
fn handles_mixed_urns() {
assert_eq!(last_iri_segment("urn:sbol:design:promoter/1"), "1");
}
}