From bb09087bf135b8b6e2705cab03f3a5804cd5444e Mon Sep 17 00:00:00 2001 From: Yury Date: Thu, 7 Nov 2024 05:46:09 +0300 Subject: [PATCH] Fix for fs path encoding. Fixes #25 --- cidre/src/cf/url.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/cidre/src/cf/url.rs b/cidre/src/cf/url.rs index 715a6295..05be6e4c 100644 --- a/cidre/src/cf/url.rs +++ b/cidre/src/cf/url.rs @@ -1,4 +1,4 @@ -use std::{os::unix::prelude::OsStrExt, path::Path}; +use std::path::Path; use crate::{arc, cf, define_cf_type}; @@ -61,8 +61,8 @@ impl Url { #[inline] pub fn with_path(path: &Path, is_dir: bool) -> Option> { - let bytes = path.as_os_str().as_bytes(); - let encoding = cf::StringEncoding::sys_encoding(); + let bytes = path.to_str()?.as_bytes(); + let encoding = cf::StringEncoding::UTF8; let Some(path) = cf::String::create_with_bytes_no_copy_in( bytes, encoding, @@ -250,3 +250,18 @@ extern "C-unwind" { ) -> Option>; fn CFURLHasDirectoryPath(anURL: &Url) -> bool; } + +#[cfg(test)] +mod tests { + use std::path::Path; + + use crate::cf; + + #[test] + fn basics() { + let path = Path::new("/tmp/MacBook Airのマイク"); + let url = cf::Url::with_file_path(path).unwrap(); + let str = url.fs_path_posix().unwrap(); + assert_eq!("/tmp/MacBook Airのマイク", str.to_string()); + } +}