Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-core/src/uri.rs')
-rw-r--r--helix-core/src/uri.rs41
1 files changed, 19 insertions, 22 deletions
diff --git a/helix-core/src/uri.rs b/helix-core/src/uri.rs
index cbe0fadd..4e03c58b 100644
--- a/helix-core/src/uri.rs
+++ b/helix-core/src/uri.rs
@@ -1,18 +1,12 @@
-use std::{
- fmt,
- path::{Path, PathBuf},
- sync::Arc,
-};
+use std::path::{Path, PathBuf};
/// A generic pointer to a file location.
///
/// Currently this type only supports paths to local files.
-///
-/// Cloning this type is cheap: the internal representation uses an Arc.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Uri {
- File(Arc<Path>),
+ File(PathBuf),
}
impl Uri {
@@ -29,18 +23,26 @@ impl Uri {
Self::File(path) => Some(path),
}
}
+
+ pub fn as_path_buf(self) -> Option<PathBuf> {
+ match self {
+ Self::File(path) => Some(path),
+ }
+ }
}
impl From<PathBuf> for Uri {
fn from(path: PathBuf) -> Self {
- Self::File(path.into())
+ Self::File(path)
}
}
-impl fmt::Display for Uri {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self {
- Self::File(path) => write!(f, "{}", path.display()),
+impl TryFrom<Uri> for PathBuf {
+ type Error = ();
+
+ fn try_from(uri: Uri) -> Result<Self, Self::Error> {
+ match uri {
+ Uri::File(path) => Ok(path),
}
}
}
@@ -57,16 +59,11 @@ pub enum UrlConversionErrorKind {
UnableToConvert,
}
-impl fmt::Display for UrlConversionError {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+impl std::fmt::Display for UrlConversionError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.kind {
UrlConversionErrorKind::UnsupportedScheme => {
- write!(
- f,
- "unsupported scheme '{}' in URL {}",
- self.source.scheme(),
- self.source
- )
+ write!(f, "unsupported scheme in URL: {}", self.source.scheme())
}
UrlConversionErrorKind::UnableToConvert => {
write!(f, "unable to convert URL to file path: {}", self.source)
@@ -80,7 +77,7 @@ impl std::error::Error for UrlConversionError {}
fn convert_url_to_uri(url: &url::Url) -> Result<Uri, UrlConversionErrorKind> {
if url.scheme() == "file" {
url.to_file_path()
- .map(|path| Uri::File(helix_stdx::path::normalize(path).into()))
+ .map(|path| Uri::File(helix_stdx::path::normalize(path)))
.map_err(|_| UrlConversionErrorKind::UnableToConvert)
} else {
Err(UrlConversionErrorKind::UnsupportedScheme)