hadoop_common/fs/
path.rs

1use iref::{IriRef, IriRefBuf};
2
3/// The directory separator, a slash.
4pub const SEPARATOR: &str = "/";
5
6#[derive(Debug, Clone)]
7pub struct Path {
8    uri: IriRefBuf,
9}
10
11impl Path {
12    /// Create a new Path based on the child path resolved against the parent path.
13    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    /// Returns true if the path component (i.e. directory) of this URI is
19    /// absolute.
20    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    /// Construct a path from a URI
31    fn from(uri: IriRefBuf) -> Self {
32        // TODO: normalize uri
33        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        // let path = Path {
65        //     uri: IriBuf::from_string("dev/hello".to_string()).unwrap(),
66        // };
67        // let p = path.uri.as_iri();
68        // println!("path: {:#?}", path);
69    }
70}