Ensure sqlez build succeeds on Windows (#7072)
On Windows, `OsStr` must be a valid [WTF-8](https://simonsapin.github.io/wtf-8/) sequence, and there are no safety ways converting from bytes to OsStr in std. So I added `PathExt::try_from_bytes` and use it in `sqlez`.
This commit is contained in:
parent
30b9cef5ba
commit
631f885900
5 changed files with 71 additions and 8 deletions
|
@ -31,6 +31,9 @@ git2 = { workspace = true, optional = true }
|
|||
dirs = "3.0"
|
||||
take-until = "0.2.0"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
tendril = "0.4.3"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile.workspace = true
|
||||
git2.workspace = true
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
use std::{
|
||||
ffi::OsStr,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use globset::{Glob, GlobMatcher};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
@ -40,6 +43,29 @@ pub trait PathExt {
|
|||
fn compact(&self) -> PathBuf;
|
||||
fn icon_suffix(&self) -> Option<&str>;
|
||||
fn extension_or_hidden_file_name(&self) -> Option<&str>;
|
||||
fn try_from_bytes<'a>(bytes: &'a [u8]) -> anyhow::Result<Self>
|
||||
where
|
||||
Self: From<&'a Path>,
|
||||
{
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::prelude::OsStrExt;
|
||||
Ok(Self::from(Path::new(OsStr::from_bytes(bytes))))
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use anyhow::anyhow;
|
||||
use tendril::fmt::{Format, WTF8};
|
||||
WTF8::validate(bytes)
|
||||
.then(|| {
|
||||
// Safety: bytes are valid WTF-8 sequence.
|
||||
Self::from(Path::new(unsafe {
|
||||
OsStr::from_encoded_bytes_unchecked(bytes)
|
||||
}))
|
||||
})
|
||||
.ok_or_else(|| anyhow!("Invalid WTF-8 sequence: {bytes:?}"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AsRef<Path>> PathExt for T {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue