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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use anyhow::{bail, Result};
use camino::{Utf8Path, Utf8PathBuf};

/// An absolute path to a configured location on disk
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Root(Utf8PathBuf);

impl Root {
    /// Constructs a new root from the given path, which must be absolute
    pub fn new(path: impl AsRef<Utf8Path>) -> Result<Self> {
        path.as_ref().to_owned().try_into()
    }
    /// The absolute path of this root
    pub fn path(&self) -> &Utf8Path {
        &self.0
    }
}

impl AsRef<Utf8Path> for Root {
    fn as_ref(&self) -> &Utf8Path {
        &self.0
    }
}

impl TryFrom<Utf8PathBuf> for Root {
    type Error = anyhow::Error;

    fn try_from(value: Utf8PathBuf) -> Result<Self, Self::Error> {
        if !is_normalized(value.as_str()) {
            bail!("Root must be a normalized path: {}", value);
        }
        if !value.is_absolute() {
            bail!("Invalid root; path must be absolute: {}", value);
        }
        Ok(Root(value))
    }
}

impl TryFrom<&Utf8Path> for Root {
    type Error = anyhow::Error;

    fn try_from(value: &Utf8Path) -> Result<Self, Self::Error> {
        value.to_owned().try_into()
    }
}

impl TryFrom<&str> for Root {
    type Error = anyhow::Error;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Utf8PathBuf::from(value).try_into()
    }
}

fn is_normalized(path: impl AsRef<Utf8Path>) -> bool {
    let path = path.as_ref().as_str();
    !((path.ends_with('/') && path != "/") || path.contains("//") || path.contains("/./"))
}