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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::str::FromStr;

use super::{NamedPackageIdent, PackageHash, PackageParseError};

#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub enum PackageIdent {
    Named(NamedPackageIdent),
    Hash(PackageHash),
}

impl PackageIdent {
    pub fn as_named(&self) -> Option<&NamedPackageIdent> {
        if let Self::Named(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn as_hash(&self) -> Option<&PackageHash> {
        if let Self::Hash(v) = self {
            Some(v)
        } else {
            None
        }
    }
}

impl From<NamedPackageIdent> for PackageIdent {
    fn from(value: NamedPackageIdent) -> Self {
        Self::Named(value)
    }
}

impl From<PackageHash> for PackageIdent {
    fn from(value: PackageHash) -> Self {
        Self::Hash(value)
    }
}

impl std::str::FromStr for PackageIdent {
    type Err = PackageParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Ok(hash) = PackageHash::from_str(s) {
            Ok(Self::Hash(hash))
        } else if let Ok(named) = NamedPackageIdent::from_str(s) {
            Ok(Self::Named(named))
        } else {
            Err(PackageParseError::new(
                s,
                "invalid package ident: expected a hash or a valid named package identifier",
            ))
        }
    }
}

impl std::fmt::Display for PackageIdent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Named(n) => n.fmt(f),
            Self::Hash(h) => h.fmt(f),
        }
    }
}

impl serde::Serialize for PackageIdent {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::ser::Serializer,
    {
        self.to_string().serialize(serializer)
    }
}

impl<'de> serde::Deserialize<'de> for PackageIdent {
    fn deserialize<D>(deserializer: D) -> Result<PackageIdent, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        Self::from_str(&s).map_err(serde::de::Error::custom)
    }
}

impl schemars::JsonSchema for PackageIdent {
    fn schema_name() -> String {
        "PackageIdent".to_string()
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        String::json_schema(gen)
    }
}