mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 15:10:34 +01:00
a84689073d
# Objective Rust's standard library `Path` API is platform-dependent, so you get wrong results when processing a Windows path on Unix, or vice versa. Zed needs this functionality because we support remote development, where cross-platform path handling is routine. Zed's `PathStyle` takes on this responsibility. But handling paths can be really tricky: we need to handle paths with non-UTF-8 characters, a problem I ran into in #63075. And thanks to Microsoft, there are 5 different kinds of valid path prefixes! Not to mention the `/` and `\` separators, which was another problem I ran into in #63102. So we need to make some changes to the `PathStyle` API. ## Solution I'm not an expert on this low-level API design, or on API design in general. But the people who maintain the Rust standard library are. Since `PathStyle`'s API is largely a cross-platform version of the standard library's `Path`, the most reliable and straightforward approach I could come up with is to bring the standard library's implementation into the Zed repo and make the necessary modifications to make it work cross-platform. It turns out Rust already has a great abstraction: the platform-dependent part of `Path` is packaged under `library/std/src/sys/path` and selected at compile time, while the `Path` implementation just uses the abstracted constants and methods: https://github.com/rust-lang/rust/blob/6474e999898cad2289175e2a7d253f5d0f574fa2/library/std/src/path.rs#L96 So we can add some helper methods to `PathStyle` to represent the compile-time-determined pieces of the standard library. Then we bring in the rest, achieving our goal of making `PathStyle`'s functionality the same as the standard library's, but cross-platform. Overall, the `parent()` and `file_name()` methods I introduced in #62038 have been reimplemented, and a new `starts_with()` method has been added. Their implementations are nearly the same as the ones in the standard library. To make these methods work, the standard library's `Components` struct was added in a new file, `crates/path/src/path_component.rs`, which is the core of reliable path handling; and `crates/path/src/windows_prefix.rs` was added to handle Windows paths. Most of the code in these two files is the same as the standard library's; I only made some small tweaks to make it work with `PathStyle`. ## Testing New tests for `starts_with` have been added. ## Self-Review Checklist: - [x] I've reviewed my own diff for quality, security, and reliability - [x] Unsafe blocks (if any) have justifying comments - [x] The content adheres to Zed's UI standards ([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) and [icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md) guidelines) - [x] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable --- Release Notes: - N/A