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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::errors::ParseError;
use crate::parse::{PackageName, PackageVariant};
use crate::{PinnedAbsolutePackageUrl, RepositoryUrl, UnpinnedAbsolutePackageUrl, UrlParts};
use fuchsia_hash::Hash;

/// A URL locating a Fuchsia package.
/// Has the form "fuchsia-pkg://<repository>/<name>[/variant][?hash=<hash>]" where:
///   * "repository" is a valid hostname
///   * "name" is a valid package name
///   * "variant" is an optional valid package variant
///   * "hash" is an optional valid package hash
/// https://fuchsia.dev/fuchsia-src/concepts/packages/package_url
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AbsolutePackageUrl {
    Unpinned(UnpinnedAbsolutePackageUrl),
    Pinned(PinnedAbsolutePackageUrl),
}

impl AbsolutePackageUrl {
    pub(crate) fn from_parts(parts: UrlParts) -> Result<Self, ParseError> {
        let UrlParts { scheme, host, path, hash, resource } = parts;
        let repo = RepositoryUrl::new(
            scheme.ok_or(ParseError::MissingScheme)?,
            host.ok_or(ParseError::MissingHost)?,
        )?;
        if resource.is_some() {
            return Err(ParseError::CannotContainResource);
        }
        Self::new_with_path(repo, &path, hash)
    }

    /// Parse a "fuchsia-pkg://" URL that locates an optionally pinned package.
    pub fn parse(url: &str) -> Result<Self, ParseError> {
        Self::from_parts(UrlParts::parse(url)?)
    }

    /// Create an AbsolutePackageUrl from its component parts and a &str `path` that will be
    /// validated.
    pub fn new_with_path(
        repo: RepositoryUrl,
        path: &str,
        hash: Option<Hash>,
    ) -> Result<Self, ParseError> {
        Ok(match hash {
            None => Self::Unpinned(UnpinnedAbsolutePackageUrl::new_with_path(repo, path)?),
            Some(hash) => Self::Pinned(PinnedAbsolutePackageUrl::new_with_path(repo, path, hash)?),
        })
    }

    /// Create an AbsolutePackageUrl from its component parts.
    pub fn new(
        repo: RepositoryUrl,
        name: PackageName,
        variant: Option<PackageVariant>,
        hash: Option<Hash>,
    ) -> Self {
        match hash {
            None => Self::Unpinned(UnpinnedAbsolutePackageUrl::new(repo, name, variant)),
            Some(hash) => Self::Pinned(PinnedAbsolutePackageUrl::new(repo, name, variant, hash)),
        }
    }

    /// The optional hash of the package.
    pub fn hash(&self) -> Option<Hash> {
        match self {
            Self::Unpinned(_) => None,
            Self::Pinned(pinned) => Some(pinned.hash()),
        }
    }

    pub fn name(&self) -> &PackageName {
        match self {
            Self::Unpinned(unpinned) => &unpinned.name(),
            Self::Pinned(pinned) => pinned.name(),
        }
    }

    /// The URL without the optional package hash.
    pub fn as_unpinned(&self) -> &UnpinnedAbsolutePackageUrl {
        match self {
            Self::Unpinned(unpinned) => &unpinned,
            Self::Pinned(pinned) => pinned.as_unpinned(),
        }
    }

    /// The pinned URL, if the URL is pinned.
    pub fn pinned(self) -> Option<PinnedAbsolutePackageUrl> {
        match self {
            Self::Unpinned(_) => None,
            Self::Pinned(pinned) => Some(pinned),
        }
    }
}

// AbsolutePackageUrl does not maintain any invariants in addition to those already maintained by
// its variants so this is safe.
impl std::ops::Deref for AbsolutePackageUrl {
    type Target = UnpinnedAbsolutePackageUrl;

    fn deref(&self) -> &Self::Target {
        match self {
            Self::Unpinned(unpinned) => &unpinned,
            Self::Pinned(pinned) => &pinned,
        }
    }
}

// AbsolutePackageUrl does not maintain any invariants in addition to those already maintained by
// its variants so this is safe.
impl std::ops::DerefMut for AbsolutePackageUrl {
    fn deref_mut(&mut self) -> &mut Self::Target {
        match self {
            Self::Unpinned(unpinned) => unpinned,
            Self::Pinned(pinned) => pinned,
        }
    }
}

impl std::str::FromStr for AbsolutePackageUrl {
    type Err = ParseError;

    fn from_str(url: &str) -> Result<Self, Self::Err> {
        Self::parse(url)
    }
}

impl std::convert::TryFrom<&str> for AbsolutePackageUrl {
    type Error = ParseError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::parse(value)
    }
}

impl std::convert::From<PinnedAbsolutePackageUrl> for AbsolutePackageUrl {
    fn from(pinned: PinnedAbsolutePackageUrl) -> Self {
        Self::Pinned(pinned)
    }
}

impl std::convert::From<UnpinnedAbsolutePackageUrl> for AbsolutePackageUrl {
    fn from(unpinned: UnpinnedAbsolutePackageUrl) -> Self {
        Self::Unpinned(unpinned)
    }
}

impl std::fmt::Display for AbsolutePackageUrl {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Unpinned(unpinned) => write!(f, "{}", unpinned),
            Self::Pinned(pinned) => write!(f, "{}", pinned),
        }
    }
}

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

impl<'de> serde::Deserialize<'de> for AbsolutePackageUrl {
    fn deserialize<D>(de: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let url = String::deserialize(de)?;
        Ok(Self::parse(&url).map_err(|err| serde::de::Error::custom(err))?)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::errors::PackagePathSegmentError;
    use assert_matches::assert_matches;
    use std::convert::TryFrom as _;

    #[test]
    fn parse_err() {
        for (url, err) in [
            ("example.org/name", ParseError::MissingScheme),
            ("//example.org/name", ParseError::MissingScheme),
            ("///name", ParseError::MissingScheme),
            ("/name", ParseError::MissingScheme),
            ("name", ParseError::MissingScheme),
            ("fuchsia-boot://example.org/name", ParseError::InvalidScheme),
            ("fuchsia-pkg://", ParseError::MissingHost),
            ("fuchsia-pkg://exaMple.org", ParseError::InvalidHost),
            ("fuchsia-pkg://example.org/", ParseError::MissingName),
            (
                "fuchsia-pkg://example.org//",
                ParseError::InvalidPathSegment(PackagePathSegmentError::Empty),
            ),
            ("fuchsia-pkg://example.org/name/variant/extra", ParseError::ExtraPathSegments),
            ("fuchsia-pkg://example.org/name#resource", ParseError::CannotContainResource),
        ] {
            assert_matches!(
                AbsolutePackageUrl::parse(url),
                Err(e) if e == err,
                "the url {:?}", url
            );
            assert_matches!(
                url.parse::<AbsolutePackageUrl>(),
                Err(e) if e == err,
                "the url {:?}", url
            );
            assert_matches!(
                AbsolutePackageUrl::try_from(url),
                Err(e) if e == err,
                "the url {:?}", url
            );
            assert_matches!(
                serde_json::from_str::<AbsolutePackageUrl>(url),
                Err(_),
                "the url {:?}",
                url
            );
        }
    }

    #[test]
    fn parse_ok() {
        for (url, host, name, variant, hash) in [
            ("fuchsia-pkg://example.org/name", "example.org", "name", None, None),
            ("fuchsia-pkg://example.org/name/variant", "example.org", "name", Some("variant"), None),
            (
            "fuchsia-pkg://example.org/name?hash=0000000000000000000000000000000000000000000000000000000000000000", "example.org", "name", None, Some("0000000000000000000000000000000000000000000000000000000000000000")),
            ("fuchsia-pkg://example.org/name/variant?hash=0000000000000000000000000000000000000000000000000000000000000000", "example.org", "name", Some("variant"), Some("0000000000000000000000000000000000000000000000000000000000000000")),
        ] {
            let json_url = format!("\"{url}\"");

            // Creation
            let name = name.parse::<crate::PackageName>().unwrap();
            let variant = variant.map(|v| v.parse::<crate::PackageVariant>().unwrap());
            let hash = hash.map(|h| h.parse::<Hash>().unwrap());
            let validate = |parsed: &AbsolutePackageUrl| {
                assert_eq!(parsed.host(), host);
                assert_eq!(parsed.name(), &name);
                assert_eq!(parsed.variant(), variant.as_ref());
                assert_eq!(parsed.hash(), hash);
            };
            validate(&AbsolutePackageUrl::parse(url).unwrap());
            validate(&url.parse::<AbsolutePackageUrl>().unwrap());
            validate(&AbsolutePackageUrl::try_from(url).unwrap());
            validate(&serde_json::from_str::<AbsolutePackageUrl>(&json_url).unwrap());

            // Stringification
            assert_eq!(
                AbsolutePackageUrl::parse(url).unwrap().to_string(),
                url,
                "the url {:?}",
                url
            );
            assert_eq!(
                serde_json::to_string(&AbsolutePackageUrl::parse(url).unwrap()).unwrap(),
                json_url,
                "the url {:?}",
                url
            );
        }
    }
}