Debian Codegraph @

helix-core/src/uri.rs

raw symbols SCIP
1 use std::{
2 fmt,
3 path::{Path, PathBuf},
4 sync::Arc,
5 };
6
7 /// A generic pointer to a file location.
8 ///
9 /// Currently this type only supports paths to local files.
10 ///
11 /// Cloning this type is cheap: the internal representation uses an Arc.
12 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
13 #[non_exhaustive]
14 pub enum Uri {
15 File(Arc<Path>),
16 }
17
18 impl Uri {
19 // This clippy allow mirrors helix_stdx::Url::from_file_path
20 #[allow(clippy::result_unit_err)]
21 pub fn to_url(&self) -> Result<helix_stdx::Url, ()> {
22 match self {
23 Uri::File(path) => helix_stdx::Url::from_file_path(path),
24 }
25 }
26
27 pub fn as_path(&self) -> Option<&Path> {
28 match self {
29 Self::File(path) => Some(path),
30 }
31 }
32 }
33
34 impl From<PathBuf> for Uri {
35 fn from(path: PathBuf) -> Self {
36 Self::File(path.into())
37 }
38 }
39
40 impl From<&Path> for Uri {
41 fn from(path: &Path) -> Self {
42 Self::File(path.into())
43 }
44 }
45
46 impl fmt::Display for Uri {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 match self {
49 Self::File(path) => write!(f, "{}", path.display()),
50 }
51 }
52 }
53
54 #[derive(Debug)]
55 pub struct UrlConversionError {
56 source: helix_stdx::Url,
57 kind: UrlConversionErrorKind,
58 }
59
60 #[derive(Debug)]
61 pub enum UrlConversionErrorKind {
62 UnsupportedScheme,
63 UnableToConvert,
64 }
65
66 impl fmt::Display for UrlConversionError {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 match self.kind {
69 UrlConversionErrorKind::UnsupportedScheme => {
70 write!(
71 f,
72 "unsupported scheme '{}' in URL {}",
73 self.source.scheme(),
74 self.source
75 )
76 }
77 UrlConversionErrorKind::UnableToConvert => {
78 write!(f, "unable to convert URL to file path: {}", self.source)
79 }
80 }
81 }
82 }
83
84 impl std::error::Error for UrlConversionError {}
85
86 fn convert_url_to_uri(url: &helix_stdx::Url) -> Result<Uri, UrlConversionErrorKind> {
87 if url.scheme() == "file" {
88 url.to_file_path()
89 .map(|path| Uri::File(helix_stdx::path::normalize(path).into()))
90 .map_err(|_| UrlConversionErrorKind::UnableToConvert)
91 } else {
92 Err(UrlConversionErrorKind::UnsupportedScheme)
93 }
94 }
95
96 impl TryFrom<helix_stdx::Url> for Uri {
97 type Error = UrlConversionError;
98
99 fn try_from(url: helix_stdx::Url) -> Result<Self, Self::Error> {
100 convert_url_to_uri(&url).map_err(|kind| Self::Error { source: url, kind })
101 }
102 }
103
104 impl TryFrom<&helix_stdx::Url> for Uri {
105 type Error = UrlConversionError;
106
107 fn try_from(url: &helix_stdx::Url) -> Result<Self, Self::Error> {
108 convert_url_to_uri(url).map_err(|kind| Self::Error {
109 source: url.clone(),
110 kind,
111 })
112 }
113 }
114
115 #[cfg(test)]
116 mod test {
117 use super::*;
118 use helix_stdx::Url;
119
120 #[test]
121 fn unknown_scheme() {
122 let url = Url::parse("csharp:/metadata/foo/bar/Baz.cs").unwrap();
123 assert!(matches!(
124 Uri::try_from(url),
125 Err(UrlConversionError {
126 kind: UrlConversionErrorKind::UnsupportedScheme,
127 ..
128 })
129 ));
130 }
131 }