1use iref::{IriRef, IriRefBuf};
2
3pub const SEPARATOR: &str = "/";
5
6#[derive(Debug, Clone)]
7pub struct Path {
8 uri: IriRefBuf,
9}
10
11impl Path {
12 pub fn from_parent(parent: &Self, child: &Self) -> anyhow::Result<Self> {
14 let uri = child.uri.resolved(parent.uri.as_iri()?).into();
15 Ok(Self { uri })
16 }
17
18 pub fn is_uri_path_absolute(&self) -> bool {
21 self.uri.path().as_str().starts_with(SEPARATOR)
22 }
23
24 pub fn to_uri(&self) -> IriRef {
25 self.uri.as_iri_ref()
26 }
27}
28
29impl From<IriRefBuf> for Path {
30 fn from(uri: IriRefBuf) -> Self {
32 Self { uri }
34 }
35}
36
37#[cfg(test)]
38mod tests {
39 use std::str::FromStr;
40
41 use iref::IriBuf;
42
43 use super::*;
44
45 #[test]
46 fn test_from_parent() {
47 let parent = Path::from(IriRefBuf::from_str("hdfs://namenode/user/alex/").unwrap());
48 let child = Path::from(IriRefBuf::from_str("database/hive/test.db").unwrap());
49 let path = Path::from_parent(&parent, &child).unwrap();
50 println!("path: {:#?}", path);
51 }
52
53 #[test]
54 fn test_is_uri_path_absolute() {
55 let iri = IriRefBuf::new("/dev/../hello").unwrap();
56 if let Some(scheme) = iri.scheme() {
57 let i = iri.resolved(IriBuf::from_scheme(scheme).as_iri());
58 println!("i: {:#?}", i);
59 } else {
60 let i = iri.resolved(IriRefBuf::from_str("/").unwrap().as_iri().unwrap());
61 println!("i: {:#?}", i);
62 }
63 println!("iri: {:#?}", iri);
64 }
70}