Skip to content

Commit 9f0e589

Browse files
committed
fix(hyperlink): Support windows URLs
1 parent 4624d60 commit 9f0e589

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

  • crates/anstyle-hyperlink/src

crates/anstyle-hyperlink/src/file.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,29 @@ const PATH_SEGMENT: &percent_encoding::AsciiSet = &PATH.add(b'/').add(b'%');
6363
const SPECIAL_PATH_SEGMENT: &percent_encoding::AsciiSet = &PATH_SEGMENT.add(b'\\');
6464

6565
fn encode_path(path: &std::path::Path, url: &mut String) {
66-
// skip the root component
6766
let mut is_path_empty = true;
68-
for component in path.components().skip(1) {
67+
68+
for component in path.components() {
6969
is_path_empty = false;
70-
url.push_str(URL_PATH_SEP);
71-
let component = component.as_os_str().to_str()?;
72-
url.extend(percent_encoding::percent_encode(
73-
component.as_bytes(),
74-
SPECIAL_PATH_SEGMENT,
75-
));
70+
match component {
71+
std::path::Component::Prefix(prefix) => {
72+
let component = prefix.as_os_str().to_string_lossy();
73+
url.push_str(&component);
74+
}
75+
std::path::Component::RootDir => {}
76+
std::path::Component::CurDir => {}
77+
std::path::Component::ParentDir => {
78+
url.push_str("..");
79+
}
80+
std::path::Component::Normal(part) => {
81+
url.push_str(URL_PATH_SEP);
82+
let component = part.to_string_lossy();
83+
url.extend(percent_encoding::percent_encode(
84+
component.as_bytes(),
85+
SPECIAL_PATH_SEGMENT,
86+
));
87+
}
88+
}
7689
}
7790
if is_path_empty {
7891
// An URL's path must not be empty

0 commit comments

Comments
 (0)