-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a polyfill to make the JS tests pass
- Loading branch information
Michael-F-Bryan
committed
May 22, 2023
1 parent
ac43ee5
commit 4cb8694
Showing
6 changed files
with
69 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use std::path::Path; | ||
|
||
use url::Url; | ||
|
||
/// Polyfill for [`Url::from_file_path()`] that works on `wasm32-unknown-unknown`. | ||
pub(crate) fn url_from_file_path(path: impl AsRef<Path>) -> Option<Url> { | ||
let path = path.as_ref(); | ||
|
||
if !path.is_absolute() { | ||
return None; | ||
} | ||
|
||
let mut buffer = String::new(); | ||
|
||
for component in path { | ||
if !buffer.ends_with('/') { | ||
buffer.push('/'); | ||
} | ||
|
||
buffer.push_str(component.to_str()?); | ||
} | ||
|
||
buffer.insert_str(0, "file://"); | ||
|
||
buffer.parse().ok() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
#[cfg(unix)] | ||
fn behaviour_is_identical() { | ||
let inputs = [ | ||
"/", | ||
"/path", | ||
"/path/to/file.txt", | ||
"./path/to/file.txt", | ||
".", | ||
"", | ||
]; | ||
|
||
for path in inputs { | ||
let got = url_from_file_path(path); | ||
let expected = Url::from_file_path(path).ok(); | ||
assert_eq!(got, expected, "Mismatch for \"{path}\""); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters