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
use crate::{label::Label, Error};

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PackageRef {
    namespace: Label,
    name: Label,
}

impl PackageRef {
    pub fn namespace(&self) -> &Label {
        &self.namespace
    }

    pub fn name(&self) -> &Label {
        &self.name
    }
}

impl std::fmt::Display for PackageRef {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", self.namespace, self.name)
    }
}

impl<'a> TryFrom<&'a str> for PackageRef {
    type Error = Error;

    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
        let Some((namespace, name)) = value.split_once(':') else {
            return Err(Error::InvalidPackageRef("missing expected ':'".into()));
        };
        Ok(Self {
            namespace: namespace.parse()?,
            name: name.parse()?,
        })
    }
}

impl std::str::FromStr for PackageRef {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.try_into()
    }
}