fix(ci): clippy + failure on Windows

pull/9/head
WJH 1 year ago committed by GitHub
parent 5d600fef0f
commit c3b8be978e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -49,13 +49,18 @@ impl FileInfo {
} }
fn get_text(&self) -> Cow<'static, str> { fn get_text(&self) -> Cow<'static, str> {
match self.file_type { let text = match self.file_type {
FileType::Root => format!("{}", self.path.display()).into(), FileType::Root => format!("{}", self.path.display()),
FileType::File | FileType::Folder => self FileType::File | FileType::Folder => self
.path .path
.file_name() .file_name()
.map_or("/".into(), |p| p.to_string_lossy().into_owned().into()), .map_or("/".into(), |p| p.to_string_lossy().into_owned()),
} };
#[cfg(test)]
let text = text.replace(std::path::MAIN_SEPARATOR, "/");
text.into()
} }
} }
@ -197,8 +202,7 @@ impl Explorer {
fn new_tree_view(root: PathBuf) -> Result<TreeView<FileInfo>> { fn new_tree_view(root: PathBuf) -> Result<TreeView<FileInfo>> {
let root = FileInfo::root(root); let root = FileInfo::root(root);
let children = root.get_children()?; Ok(TreeView::build_tree(root)?.with_enter_fn(Self::toggle_current))
Ok(TreeView::build_tree(root, children).with_enter_fn(Self::toggle_current))
} }
fn push_history(&mut self, tree_view: TreeView<FileInfo>) { fn push_history(&mut self, tree_view: TreeView<FileInfo>) {
@ -220,31 +224,38 @@ impl Explorer {
fn reveal_file(&mut self, path: PathBuf) -> Result<()> { fn reveal_file(&mut self, path: PathBuf) -> Result<()> {
let current_root = &self.state.current_root; let current_root = &self.state.current_root;
let current_path = path.as_path().to_string_lossy().to_string(); let current_path = &path;
let current_root = current_root.as_path().to_string_lossy().to_string() + "/"; let current_root = format!(
"{}{}",
current_root.as_path().to_string_lossy(),
std::path::MAIN_SEPARATOR
);
let segments = { let segments = {
let stripped = match current_path.strip_prefix(current_root.as_str()) { let stripped = match current_path.strip_prefix(current_root.as_str()) {
Some(stripped) => Ok(stripped), Ok(stripped) => Ok(stripped),
None => { Err(_) => {
let parent = path let parent = path.parent().ok_or_else(|| {
.parent() anyhow::anyhow!("Failed get parent of '{}'", current_path.to_string_lossy())
.ok_or_else(|| anyhow::anyhow!("Failed get parent of '{current_path}'"))?; })?;
self.change_root(parent.into())?; self.change_root(parent.into())?;
current_path current_path
.strip_prefix((parent.to_string_lossy().to_string() + "/").as_str()) .strip_prefix(
.ok_or_else(|| { format!("{}{}", parent.to_string_lossy(), std::path::MAIN_SEPARATOR)
.as_str(),
)
.map_err(|_| {
anyhow::anyhow!( anyhow::anyhow!(
"Failed to strip prefix (parent) '{}' from '{}'", "Failed to strip prefix (parent) '{}' from '{}'",
parent.to_string_lossy(), parent.to_string_lossy(),
current_path current_path.to_string_lossy()
) )
}) })
} }
}?; }?;
stripped stripped
.split(std::path::MAIN_SEPARATOR) .components()
.map(|s| s.to_string()) .map(|c| c.as_os_str().to_string_lossy().to_string())
.collect::<Vec<_>>() .collect::<Vec<_>>()
}; };
self.tree.reveal_item(segments, &self.state.filter)?; self.tree.reveal_item(segments, &self.state.filter)?;
@ -907,7 +918,7 @@ mod test_explorer {
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use std::{fs, path::PathBuf}; use std::{fs, path::PathBuf};
fn dummy_file_tree<'a>(name: &'a str) -> PathBuf { fn dummy_file_tree(name: &str) -> PathBuf {
use build_fs_tree::{dir, file, Build, MergeableFileSystemTree}; use build_fs_tree::{dir, file, Build, MergeableFileSystemTree};
let tree = MergeableFileSystemTree::<&str, &str>::from(dir! { let tree = MergeableFileSystemTree::<&str, &str>::from(dir! {
"index.html" => file!("") "index.html" => file!("")
@ -922,7 +933,7 @@ mod test_explorer {
} }
".gitignore" => file!("") ".gitignore" => file!("")
}); });
let path: PathBuf = format!("test-explorer/{}", name).into(); let path: PathBuf = format!("test-explorer{}{}", std::path::MAIN_SEPARATOR, name).into();
if path.exists() { if path.exists() {
fs::remove_dir_all(path.clone()).unwrap(); fs::remove_dir_all(path.clone()).unwrap();
} }
@ -930,13 +941,11 @@ mod test_explorer {
path path
} }
fn render<'a>(explorer: &mut Explorer) -> String { fn render(explorer: &mut Explorer) -> String {
explorer explorer.tree.render_to_string(Rect::new(0, 0, 50, 10), "")
.tree
.render_to_string(Rect::new(0, 0, 50, 10), &"".to_string())
} }
fn new_explorer<'a>(name: &'a str) -> (PathBuf, Explorer) { fn new_explorer(name: &str) -> (PathBuf, Explorer) {
let path = dummy_file_tree(name); let path = dummy_file_tree(name);
(path.clone(), Explorer::from_path(path, 30).unwrap()) (path.clone(), Explorer::from_path(path, 30).unwrap())
} }
@ -1024,7 +1033,7 @@ mod test_explorer {
// 1. Rename the current file to a name that is lexicographically greater than "index.html" // 1. Rename the current file to a name that is lexicographically greater than "index.html"
explorer explorer
.rename_current(&path.join("who.is").to_string_lossy().into()) .rename_current(&path.join("who.is").display().to_string())
.unwrap(); .unwrap();
// 1a. Expect the file is renamed, and is focused // 1a. Expect the file is renamed, and is focused
@ -1044,7 +1053,7 @@ mod test_explorer {
// 2. Rename the current file into an existing folder // 2. Rename the current file into an existing folder
explorer explorer
.rename_current(&path.join("styles/lol").to_string_lossy().into()) .rename_current(&path.join("styles/lol").display().to_string())
.unwrap(); .unwrap();
// 2a. Expect the file is moved to the folder, and is focused // 2a. Expect the file is moved to the folder, and is focused
@ -1066,7 +1075,7 @@ mod test_explorer {
// 3. Rename the current file into a non-existent folder // 3. Rename the current file into a non-existent folder
explorer explorer
.rename_current(&path.join("new_folder/sponge/bob").to_string_lossy().into()) .rename_current(&path.join("new_folder/sponge/bob").display().to_string())
.unwrap(); .unwrap();
// 3a. Expect the non-existent folder to be created, // 3a. Expect the non-existent folder to be created,
@ -1107,7 +1116,7 @@ mod test_explorer {
// 5. Move cursor to "bob", and move it outside of the current root // 5. Move cursor to "bob", and move it outside of the current root
explorer.tree.move_down(1); explorer.tree.move_down(1);
explorer explorer
.rename_current(&path.join("scripts/bob").to_string_lossy().into()) .rename_current(&path.join("scripts/bob").display().to_string())
.unwrap(); .unwrap();
// 5a. Expect the current root to be "scripts" // 5a. Expect the current root to be "scripts"

@ -339,8 +339,9 @@ impl<T: TreeViewItem> TreeView<T> {
} }
} }
pub fn build_tree(root: T, items: Vec<T>) -> Self { pub fn build_tree(root: T) -> Result<Self> {
Self::new(root, vec_to_tree(items)) let children = root.get_children()?;
Ok(Self::new(root, vec_to_tree(children)))
} }
pub fn with_enter_fn<F>(mut self, f: F) -> Self pub fn with_enter_fn<F>(mut self, f: F) -> Self
@ -1222,7 +1223,7 @@ mod test_tree_view {
name: &'a str, name: &'a str,
} }
fn item<'a>(name: &'a str) -> Item<'a> { fn item(name: &str) -> Item {
Item { name } Item { name }
} }
@ -1273,8 +1274,8 @@ mod test_tree_view {
Rect::new(0, 0, 50, 5) Rect::new(0, 0, 50, 5)
} }
fn render<'a>(view: &mut TreeView<Item<'a>>) -> String { fn render(view: &mut TreeView<Item>) -> String {
view.render_to_string(dummy_area(), &"".to_string()) view.render_to_string(dummy_area(), "")
} }
#[test] #[test]
@ -1624,8 +1625,8 @@ mod test_tree_view {
fn test_move_left_right() { fn test_move_left_right() {
let mut view = dummy_tree_view(); let mut view = dummy_tree_view();
fn render<'a>(view: &mut TreeView<Item<'a>>) -> String { fn render(view: &mut TreeView<Item>) -> String {
view.render_to_string(dummy_area().with_width(20), &"".to_string()) view.render_to_string(dummy_area().with_width(20), "")
} }
assert_eq!( assert_eq!(
@ -1979,13 +1980,13 @@ krabby_patty
// 1. Move to the last child item on the tree // 1. Move to the last child item on the tree
view.move_to_last_line(); view.move_to_last_line();
view.move_to_children(&"".to_string()).unwrap(); view.move_to_children("").unwrap();
view.move_to_last_line(); view.move_to_last_line();
view.move_to_children(&"".to_string()).unwrap(); view.move_to_children("").unwrap();
view.move_to_last_line(); view.move_to_last_line();
view.move_to_children(&"".to_string()).unwrap(); view.move_to_children("").unwrap();
view.move_to_last_line(); view.move_to_last_line();
view.move_to_children(&"".to_string()).unwrap(); view.move_to_children("").unwrap();
// 1a. Expect the current selected item is the last child on the tree // 1a. Expect the current selected item is the last child on the tree
assert_eq!( assert_eq!(
@ -2000,7 +2001,7 @@ krabby_patty
); );
// 2. Refreshes the tree with a filter that will remove the last child // 2. Refreshes the tree with a filter that will remove the last child
view.refresh_with_filter(&"ar".to_string()).unwrap(); view.refresh_with_filter("ar").unwrap();
// 3. Get the current item // 3. Get the current item
let item = view.current_item().unwrap(); let item = view.current_item().unwrap();
@ -2113,7 +2114,7 @@ krabby_patty
} }
} }
fn child<'a>(name: &'a str) -> Item<'a> { fn child(name: &str) -> Item {
Item { Item {
name, name,
children: None, children: None,
@ -2143,8 +2144,8 @@ krabby_patty
} }
} }
fn render<'a>(view: &mut TreeView<Item<'a>>) -> String { fn render(view: &mut TreeView<Item<'_>>) -> String {
view.render_to_string(dummy_area().with_height(3), &"".to_string()) view.render_to_string(dummy_area().with_height(3), "")
} }
let mut view = TreeView::new( let mut view = TreeView::new(

Loading…
Cancel
Save