Move utf8 utils from zip.rs to utils module

This commit is contained in:
João M. Bezerra 2021-11-10 19:47:36 -03:00
parent ed0e225219
commit f1c0c82323
2 changed files with 23 additions and 22 deletions

View File

@ -170,25 +170,3 @@ fn __unix_set_permissions(file_path: &Path, file: &ZipFile) -> crate::Result<()>
Ok(())
}
mod utf8 {
use std::path::{Path, PathBuf};
fn is_invalid_utf8(path: &Path) -> bool {
#[cfg(unix)]
{
use std::{os::unix::prelude::OsStrExt, str};
let bytes = path.as_os_str().as_bytes();
str::from_utf8(bytes).is_err()
}
#[cfg(not(unix))]
{
path.to_str().is_none()
}
}
pub fn get_invalid_utf8_paths(paths: &[PathBuf]) -> Vec<PathBuf> {
paths.iter().filter_map(|path| is_invalid_utf8(&path).then(|| path.clone())).collect()
}
}

View File

@ -13,3 +13,26 @@ pub use fs::{cd_into_same_dir_as, create_dir_if_non_existent, dir_is_empty, try_
pub use question::{
create_or_ask_overwrite, user_wants_to_continue_decompressing, user_wants_to_overwrite, QuestionPolicy,
};
pub use utf8::{get_invalid_utf8_paths, is_invalid_utf8};
mod utf8 {
use std::path::{Path, PathBuf};
pub fn is_invalid_utf8(path: &Path) -> bool {
#[cfg(unix)]
{
use std::{os::unix::prelude::OsStrExt, str};
let bytes = path.as_os_str().as_bytes();
str::from_utf8(bytes).is_err()
}
#[cfg(not(unix))]
{
path.to_str().is_none()
}
}
pub fn get_invalid_utf8_paths(paths: &[PathBuf]) -> Vec<PathBuf> {
paths.iter().filter_map(|path| is_invalid_utf8(&path).then(|| path.clone())).collect()
}
}