fix(compatibility): String ==> OsString

This commit is contained in:
Aram Drevekenin
2020-05-22 15:31:02 +02:00
parent a5c0b4e027
commit b2bc7d5869
5 changed files with 29 additions and 27 deletions

View File

@@ -1,6 +1,7 @@
use std::collections::{HashMap, VecDeque};
use std::fs::Metadata;
use std::os::unix::fs::MetadataExt; // TODO: support other OSs
use std::ffi::{OsString, OsStr};
use std::path::{Path, PathBuf};
@@ -11,7 +12,7 @@ pub enum FileOrFolder {
}
impl FileOrFolder {
pub fn name (&self) -> &str {
pub fn name (&self) -> &OsStr {
match self {
FileOrFolder::Folder(folder) => &folder.name,
FileOrFolder::File(file) => &file.name,
@@ -27,23 +28,23 @@ impl FileOrFolder {
#[derive(Debug, Clone)]
pub struct File {
pub name: String,
pub name: OsString,
pub size: u64,
}
#[derive(Debug, Clone)]
pub struct Folder {
pub name: String,
pub contents: HashMap<String, FileOrFolder>,
pub name: OsString,
pub contents: HashMap<OsString, FileOrFolder>,
pub size: u64,
pub num_descendants: u64,
}
impl Folder {
pub fn new (path: &PathBuf) -> Self {
let base_folder_name = path.iter().last().expect("could not get path base name").to_string_lossy();
let base_folder_name = path.iter().last().expect("could not get path base name");
Self {
name: String::from(base_folder_name),
name: base_folder_name.to_os_string(),
contents: HashMap::new(),
size: 0,
num_descendants: 0,
@@ -70,7 +71,7 @@ impl Folder {
return
}
if path_length > 1 {
let name = String::from(path.iter().next().expect("could not get next path element for folder").to_string_lossy());
let name = path.iter().next().expect("could not get next path element for folder").to_os_string();
let path_entry = self.contents.entry(name.clone()).or_insert(
FileOrFolder::Folder(
Folder {
@@ -87,7 +88,7 @@ impl Folder {
_ => {}
};
} else {
let name = String::from(path.iter().next().expect("could not get next path element for file").to_string_lossy());
let name = path.iter().next().expect("could not get next path element for file").to_os_string();
self.num_descendants += 1;
self.contents.insert(name.clone(),
FileOrFolder::Folder(
@@ -107,7 +108,7 @@ impl Folder {
return
}
if path_length > 1 {
let name = String::from(path.iter().next().expect("could not get next path element for folder").to_string_lossy());
let name = path.iter().next().expect("could not get next path element for folder").to_os_string();
let path_entry = self.contents.entry(name.clone()).or_insert(
FileOrFolder::Folder(
Folder {
@@ -127,7 +128,7 @@ impl Folder {
_ => {}
};
} else {
let name = String::from(path.iter().next().expect("could not get next path element for file").to_string_lossy());
let name = path.iter().next().expect("could not get next path element for file").to_os_string();
self.size += size;
self.num_descendants += 1;
self.contents.insert(name.clone(),
@@ -140,8 +141,8 @@ impl Folder {
);
}
}
pub fn path(&self, folder_names: &Vec<String>) -> Option<&FileOrFolder> {
let mut folders_to_traverse: VecDeque<String> = VecDeque::from(folder_names.to_owned());
pub fn path(&self, folder_names: &Vec<OsString>) -> Option<&FileOrFolder> {
let mut folders_to_traverse: VecDeque<OsString> = VecDeque::from(folder_names.to_owned());
let next_name = folders_to_traverse.pop_front().expect("could not find next path folder1");
let next_in_path = &self.contents.get(&next_name)?;
if folders_to_traverse.is_empty() {
@@ -152,8 +153,8 @@ impl Folder {
Some(next_in_path)
}
}
pub fn delete_path(&mut self, folder_names: &Vec<String>) {
let mut folders_to_traverse: VecDeque<String> = VecDeque::from(folder_names.to_owned()); // TODO: better
pub fn delete_path(&mut self, folder_names: &Vec<OsString>) {
let mut folders_to_traverse: VecDeque<OsString> = VecDeque::from(folder_names.to_owned()); // TODO: better
if folder_names.len() == 1 {
let name = folder_names.last().expect("could not find last item in path");
let removed_size = &self.contents.get(name).expect("could not find folder").size();

View File

@@ -1,10 +1,11 @@
use crate::state::files::{FileOrFolder, Folder};
use std::path::{Path, PathBuf};
use std::ffi::{OsString, OsStr};
use std::fs::Metadata;
pub struct FileTree {
base_folder: Folder,
current_folder_names: Vec<String>,
current_folder_names: Vec<OsString>,
pub space_freed: u64, // TODO: move elsewhere
pub path_in_filesystem: PathBuf,
}
@@ -46,12 +47,12 @@ impl FileTree {
}
return full_path;
}
pub fn item_in_current_folder(&self, item_name: &str) -> Option<&FileOrFolder> {
pub fn item_in_current_folder(&self, item_name: &OsStr) -> Option<&FileOrFolder> {
let current_folder = &self.get_current_folder();
current_folder.path(&vec![String::from(item_name)])
current_folder.path(&vec![item_name.to_os_string()])
}
pub fn enter_folder(&mut self, folder_name: &str) {
self.current_folder_names.push(String::from(folder_name));
pub fn enter_folder(&mut self, folder_name: &OsStr) {
self.current_folder_names.push(folder_name.to_os_string());
}
pub fn leave_folder(&mut self) -> bool { // true => succeeded, false => at base folder
match self.current_folder_names.pop() {
@@ -59,13 +60,12 @@ impl FileTree {
None => false
}
}
pub fn delete_file(&mut self, file_name: &str) {
pub fn delete_file(&mut self, file_name: &OsStr) {
let path_to_delete = &mut self.current_folder_names.clone();
path_to_delete.push(String::from(file_name));
path_to_delete.push(file_name.to_os_string());
self.base_folder.delete_path(&path_to_delete);
}
pub fn add_entry(&mut self, entry_metadata: &Metadata, entry_full_path: &Path, base_path_length: &usize) {
self.base_folder.add_entry(entry_metadata, entry_full_path, base_path_length);
}
}

View File

@@ -1,4 +1,5 @@
use tui::layout::Rect;
use std::ffi::OsString;
use crate::state::files::{FileOrFolder, Folder};
use crate::state::tiles::{TreeMap, RectFloat};
@@ -11,7 +12,7 @@ pub enum FileType {
#[derive(Debug, Clone)]
pub struct FileMetadata {
pub name: String,
pub name: OsString,
pub size: u64,
pub descendants: Option<u64>,
pub percentage: f64, // 1.0 is 100% (0.5 is 50%, etc.)
@@ -39,7 +40,7 @@ impl Board {
for (name, file_or_folder) in &folder.contents {
files.push({
let size = file_or_folder.size();
let name = String::from(name);
let name = name.clone();
let (descendants, file_type) = match file_or_folder {
FileOrFolder::Folder(folder) => (Some(folder.num_descendants), FileType::Folder),
FileOrFolder::File(_file) => (None, FileType::File),
@@ -80,7 +81,7 @@ impl Board {
for (name, file_or_folder) in &folder.contents {
files.push({
let size = file_or_folder.size();
let name = String::from(name);
let name = name.clone();
let (descendants, file_type) = match file_or_folder {
FileOrFolder::Folder(folder) => (Some(folder.num_descendants), FileType::Folder),
FileOrFolder::File(_file) => (None, FileType::File),

View File

@@ -102,7 +102,7 @@ impl<'a> Widget for MessageBox<'a> {
full_path.push(&self.file_to_delete.name());
let full_path = full_path.into_os_string().into_string().expect("could not convert os string to string");
let file_name = &self.file_to_delete.name();
let file_name = &self.file_to_delete.name().to_string_lossy();
let full_path_display = String::from(full_path);
let file_name_line = if text_length > full_path_display.len() as u16 {

View File

@@ -76,7 +76,7 @@ fn draw_small_files_rect_on_grid(buf: &mut Buffer, rect: Rect) {
fn draw_rect_text_on_grid(buf: &mut Buffer, rect: &Rect, file_rect: &FileRect) { // TODO: better, combine args
let max_text_length = if rect.width > 2 { rect.width - 2 } else { 0 };
let name = &file_rect.file_metadata.name;
let name = &file_rect.file_metadata.name.to_string_lossy();
let descendant_count = &file_rect.file_metadata.descendants;
let percentage = &file_rect.file_metadata.percentage;