|
|
@ -1,6 +1,7 @@
|
|
|
|
use std::{
|
|
|
|
use std::{
|
|
|
|
fmt,
|
|
|
|
fmt,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
|
|
|
|
str::FromStr,
|
|
|
|
sync::Arc,
|
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -16,14 +17,6 @@ pub enum Uri {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Uri {
|
|
|
|
impl Uri {
|
|
|
|
// This clippy allow mirrors url::Url::from_file_path
|
|
|
|
|
|
|
|
#[allow(clippy::result_unit_err)]
|
|
|
|
|
|
|
|
pub fn to_url(&self) -> Result<url::Url, ()> {
|
|
|
|
|
|
|
|
match self {
|
|
|
|
|
|
|
|
Uri::File(path) => url::Url::from_file_path(path),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn as_path(&self) -> Option<&Path> {
|
|
|
|
pub fn as_path(&self) -> Option<&Path> {
|
|
|
|
match self {
|
|
|
|
match self {
|
|
|
|
Self::File(path) => Some(path),
|
|
|
|
Self::File(path) => Some(path),
|
|
|
@ -45,81 +38,92 @@ impl fmt::Display for Uri {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct UrlConversionError {
|
|
|
|
pub struct UriParseError {
|
|
|
|
source: url::Url,
|
|
|
|
source: String,
|
|
|
|
kind: UrlConversionErrorKind,
|
|
|
|
kind: UriParseErrorKind,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum UrlConversionErrorKind {
|
|
|
|
pub enum UriParseErrorKind {
|
|
|
|
UnsupportedScheme,
|
|
|
|
UnsupportedScheme(String),
|
|
|
|
UnableToConvert,
|
|
|
|
UnableToConvert,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for UrlConversionError {
|
|
|
|
impl fmt::Display for UriParseError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self.kind {
|
|
|
|
match &self.kind {
|
|
|
|
UrlConversionErrorKind::UnsupportedScheme => {
|
|
|
|
UriParseErrorKind::UnsupportedScheme(scheme) => {
|
|
|
|
write!(
|
|
|
|
write!(f, "unsupported scheme '{scheme}' in URL {}", self.source)
|
|
|
|
f,
|
|
|
|
}
|
|
|
|
"unsupported scheme '{}' in URL {}",
|
|
|
|
UriParseErrorKind::UnableToConvert => {
|
|
|
|
self.source.scheme(),
|
|
|
|
|
|
|
|
self.source
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UrlConversionErrorKind::UnableToConvert => {
|
|
|
|
|
|
|
|
write!(f, "unable to convert URL to file path: {}", self.source)
|
|
|
|
write!(f, "unable to convert URL to file path: {}", self.source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl std::error::Error for UrlConversionError {}
|
|
|
|
impl std::error::Error for UriParseError {}
|
|
|
|
|
|
|
|
|
|
|
|
fn convert_url_to_uri(url: &url::Url) -> Result<Uri, UrlConversionErrorKind> {
|
|
|
|
impl FromStr for Uri {
|
|
|
|
if url.scheme() == "file" {
|
|
|
|
type Err = UriParseError;
|
|
|
|
url.to_file_path()
|
|
|
|
|
|
|
|
.map(|path| Uri::File(helix_stdx::path::normalize(path).into()))
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
.map_err(|_| UrlConversionErrorKind::UnableToConvert)
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
} else {
|
|
|
|
#[cfg(any(unix, target_os = "redox"))]
|
|
|
|
Err(UrlConversionErrorKind::UnsupportedScheme)
|
|
|
|
use std::os::unix::prelude::OsStrExt;
|
|
|
|
}
|
|
|
|
#[cfg(target_os = "wasi")]
|
|
|
|
|
|
|
|
use std::os::wasi::prelude::OsStrExt;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let Some((scheme, rest)) = s.split_once("://") else {
|
|
|
|
|
|
|
|
return Err(Self::Err {
|
|
|
|
|
|
|
|
source: s.to_string(),
|
|
|
|
|
|
|
|
kind: UriParseErrorKind::UnableToConvert,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if scheme != "file" {
|
|
|
|
|
|
|
|
return Err(Self::Err {
|
|
|
|
|
|
|
|
source: s.to_string(),
|
|
|
|
|
|
|
|
kind: UriParseErrorKind::UnsupportedScheme(scheme.to_string()),
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl TryFrom<url::Url> for Uri {
|
|
|
|
// Assert there is no query or fragment in the URI.
|
|
|
|
type Error = UrlConversionError;
|
|
|
|
if s.find(['?', '#']).is_some() {
|
|
|
|
|
|
|
|
return Err(Self::Err {
|
|
|
|
|
|
|
|
source: s.to_string(),
|
|
|
|
|
|
|
|
kind: UriParseErrorKind::UnableToConvert,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn try_from(url: url::Url) -> Result<Self, Self::Error> {
|
|
|
|
let mut bytes = Vec::new();
|
|
|
|
convert_url_to_uri(&url).map_err(|kind| Self::Error { source: url, kind })
|
|
|
|
bytes.extend(percent_encoding::percent_decode(rest.as_bytes()));
|
|
|
|
|
|
|
|
Ok(PathBuf::from(OsStr::from_bytes(&bytes)).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl TryFrom<&url::Url> for Uri {
|
|
|
|
impl TryFrom<&str> for Uri {
|
|
|
|
type Error = UrlConversionError;
|
|
|
|
type Error = UriParseError;
|
|
|
|
|
|
|
|
|
|
|
|
fn try_from(url: &url::Url) -> Result<Self, Self::Error> {
|
|
|
|
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
|
|
|
convert_url_to_uri(url).map_err(|kind| Self::Error {
|
|
|
|
s.parse()
|
|
|
|
source: url.clone(),
|
|
|
|
|
|
|
|
kind,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use super::*;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn unknown_scheme() {
|
|
|
|
fn unknown_scheme() {
|
|
|
|
let url = Url::parse("csharp:/metadata/foo/bar/Baz.cs").unwrap();
|
|
|
|
let uri = "csharp://metadata/foo/barBaz.cs";
|
|
|
|
assert!(matches!(
|
|
|
|
assert_eq!(
|
|
|
|
Uri::try_from(url),
|
|
|
|
uri.parse::<Uri>(),
|
|
|
|
Err(UrlConversionError {
|
|
|
|
Err(UriParseError {
|
|
|
|
kind: UrlConversionErrorKind::UnsupportedScheme,
|
|
|
|
source: uri.to_string(),
|
|
|
|
..
|
|
|
|
kind: UriParseErrorKind::UnsupportedScheme("csharp".to_string()),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
));
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|