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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// 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,
        parse::{validate_resource_path, PackageName, PackageVariant},
        AbsolutePackageUrl, RepositoryUrl, UrlParts,
    },
    fuchsia_hash::Hash,
};

/// A URL locating a Fuchsia component.
/// Has the form "fuchsia-pkg://<repository>/<name>[/variant][?hash=<hash>]#<resource>" 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
///   * "resource" is a valid resource path
/// https://fuchsia.dev/fuchsia-src/concepts/packages/package_url
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AbsoluteComponentUrl {
    package: AbsolutePackageUrl,
    resource: String,
}

impl AbsoluteComponentUrl {
    /// Create an AbsoluteComponentUrl from its component parts.
    pub fn new(
        repo: RepositoryUrl,
        name: PackageName,
        variant: Option<PackageVariant>,
        hash: Option<Hash>,
        resource: String,
    ) -> Result<Self, ParseError> {
        let () = validate_resource_path(&resource).map_err(ParseError::InvalidResourcePath)?;
        Ok(Self { package: AbsolutePackageUrl::new(repo, name, variant, hash), resource })
    }

    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)?,
        )?;
        let package = AbsolutePackageUrl::new_with_path(repo, &path, hash)?;
        let resource = resource.ok_or(ParseError::MissingResource)?;
        Ok(Self { package, resource })
    }

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

    /// Create an `AbsoluteComponentUrl` from a package URL and a resource path.
    pub fn from_package_url_and_resource(
        package: AbsolutePackageUrl,
        resource: String,
    ) -> Result<Self, ParseError> {
        let () = validate_resource_path(&resource).map_err(ParseError::InvalidResourcePath)?;
        Ok(Self { package, resource })
    }

    /// The resource path of this URL.
    pub fn resource(&self) -> &str {
        &self.resource
    }

    /// The package URL of this URL (this URL without the resource path).
    pub fn package_url(&self) -> &AbsolutePackageUrl {
        &self.package
    }

    pub(crate) fn into_package_and_resource(self) -> (AbsolutePackageUrl, String) {
        let Self { package, resource } = self;
        (package, resource)
    }
}

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

    fn deref(&self) -> &Self::Target {
        &self.package
    }
}

// AbsoluteComponentUrl does not maintain any invariants on its `package` field in addition to those
// already maintained by AbsolutePackageUrl so this is safe.
impl std::ops::DerefMut for AbsoluteComponentUrl {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.package
    }
}

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

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

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

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

impl std::fmt::Display for AbsoluteComponentUrl {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}#{}",
            self.package,
            percent_encoding::utf8_percent_encode(&self.resource, crate::FRAGMENT)
        )
    }
}

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

impl<'de> serde::Deserialize<'de> for AbsoluteComponentUrl {
    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::*,
        crate::errors::{PackagePathSegmentError, ResourcePathError},
        assert_matches::assert_matches,
        std::convert::TryFrom as _,
    };

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

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

            // 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: &AbsoluteComponentUrl| {
                assert_eq!(parsed.host(), host);
                assert_eq!(parsed.name(), &name);
                assert_eq!(parsed.variant(), variant.as_ref());
                assert_eq!(parsed.hash(), hash);
                assert_eq!(parsed.resource(), resource);
            };
            validate(&AbsoluteComponentUrl::parse(url).unwrap());
            validate(&url.parse::<AbsoluteComponentUrl>().unwrap());
            validate(&AbsoluteComponentUrl::try_from(url).unwrap());
            validate(&serde_json::from_str::<AbsoluteComponentUrl>(&json_url).unwrap());

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

    #[test]
    // Verify that resource path is validated at all, exhaustive testing of resource path
    // validation is performed by the tests on `validate_resource_path`.
    fn from_package_url_and_resource_err() {
        for (resource, err) in [
            ("", ParseError::InvalidResourcePath(ResourcePathError::PathIsEmpty)),
            ("/", ParseError::InvalidResourcePath(ResourcePathError::PathStartsWithSlash)),
        ] {
            let package = "fuchsia-pkg://example.org/name".parse::<AbsolutePackageUrl>().unwrap();
            assert_eq!(
                AbsoluteComponentUrl::from_package_url_and_resource(package, resource.into()),
                Err(err),
                "the resource {:?}",
                resource
            );
        }
    }

    #[test]
    fn from_package_url_and_resource_ok() {
        let package = "fuchsia-pkg://example.org/name".parse::<AbsolutePackageUrl>().unwrap();

        let component =
            AbsoluteComponentUrl::from_package_url_and_resource(package.clone(), "resource".into())
                .unwrap();
        assert_eq!(component.resource(), "resource");

        let component =
            AbsoluteComponentUrl::from_package_url_and_resource(package.clone(), "☺".into())
                .unwrap();
        assert_eq!(component.resource(), "☺");
    }
}