warg_loader/
package.rs

1use crate::{label::Label, Error};
2
3#[derive(Clone, Debug, PartialEq, Eq, Hash)]
4pub struct PackageRef {
5    namespace: Label,
6    name: Label,
7}
8
9impl PackageRef {
10    pub fn namespace(&self) -> &Label {
11        &self.namespace
12    }
13
14    pub fn name(&self) -> &Label {
15        &self.name
16    }
17}
18
19impl std::fmt::Display for PackageRef {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        write!(f, "{}:{}", self.namespace, self.name)
22    }
23}
24
25impl<'a> TryFrom<&'a str> for PackageRef {
26    type Error = Error;
27
28    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
29        let Some((namespace, name)) = value.split_once(':') else {
30            return Err(Error::InvalidPackageRef("missing expected ':'".into()));
31        };
32        Ok(Self {
33            namespace: namespace.parse()?,
34            name: name.parse()?,
35        })
36    }
37}
38
39impl std::str::FromStr for PackageRef {
40    type Err = Error;
41
42    fn from_str(s: &str) -> Result<Self, Self::Err> {
43        s.try_into()
44    }
45}