use crate::errors::ParseError;
use crate::parse::{validate_resource_path, PackageName, PackageVariant};
use crate::{AbsolutePackageUrl, RepositoryUrl, UrlParts};
use fuchsia_hash::Hash;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AbsoluteComponentUrl {
package: AbsolutePackageUrl,
resource: String,
}
impl AbsoluteComponentUrl {
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 })
}
pub fn parse(url: &str) -> Result<Self, ParseError> {
Self::from_parts(UrlParts::parse(url)?)
}
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 })
}
pub fn resource(&self) -> &str {
&self.resource
}
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)
}
}
impl std::ops::Deref for AbsoluteComponentUrl {
type Target = AbsolutePackageUrl;
fn deref(&self) -> &Self::Target {
&self.package
}
}
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::*;
use crate::errors::{PackagePathSegmentError, ResourcePathError};
use assert_matches::assert_matches;
use 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";
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());
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]
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(), "☺");
}
}