refactor: switch to .expect() from match
Using .expect() for Options and Results add more readability to the code.merge-requests/8/head
parent
9594ed6539
commit
1b9f20b648
|
@ -32,44 +32,45 @@ impl EchidnaCoreEditor {
|
||||||
|
|
||||||
let cancellable = gio::Cancellable::new();
|
let cancellable = gio::Cancellable::new();
|
||||||
let filepath = file_location.path().expect("No filepath");
|
let filepath = file_location.path().expect("No filepath");
|
||||||
let file_info_result =
|
let info = file_location
|
||||||
file_location.query_info("*", gio::FileQueryInfoFlags::NONE, Some(&cancellable));
|
.query_info("*", gio::FileQueryInfoFlags::NONE, Some(&cancellable))
|
||||||
match file_info_result {
|
.expect("Could not query the info for file");
|
||||||
Ok(info) => match info.content_type() {
|
|
||||||
Some(content_type) => {
|
|
||||||
println!(
|
|
||||||
"Opened {} and found its content type is {}.",
|
|
||||||
"file",
|
|
||||||
content_type.to_string()
|
|
||||||
);
|
|
||||||
let buffer = this_imp.sourceview.buffer().downcast::<Buffer>().expect("Cannot downcast the sourceview's buffer. Maybe the sourceview's buffer is not IsA<sourceview::Buffer>.");
|
|
||||||
let language_manager = LanguageManager::new();
|
|
||||||
let language = language_manager.guess_language(Some(&info.name().to_str().expect("Could not open the file because its name is not supported by Unicode.")), None);
|
|
||||||
|
|
||||||
match language {
|
let content_type = info
|
||||||
Some(lang) => buffer.set_language(Some(&lang)),
|
.content_type()
|
||||||
None => {}
|
.expect(format!("It does not seem like {:?} has a type", filepath).as_str());
|
||||||
|
{
|
||||||
|
println!(
|
||||||
|
"Opened {} and found its content type is {}.",
|
||||||
|
"file",
|
||||||
|
content_type.to_string()
|
||||||
|
);
|
||||||
|
let buffer = this_imp.sourceview.buffer().downcast::<Buffer>().expect("Cannot downcast the sourceview's buffer. Maybe the sourceview's buffer is not IsA<sourceview::Buffer>.");
|
||||||
|
let language_manager = LanguageManager::new();
|
||||||
|
let language = language_manager.guess_language(
|
||||||
|
Some(&info.name().to_str().expect(
|
||||||
|
"Could not open the file because its name is not supported by Unicode.",
|
||||||
|
)),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
|
||||||
|
match language {
|
||||||
|
Some(lang) => buffer.set_language(Some(&lang)),
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
let file_loader: FileLoader = FileLoader::new(&buffer, &file);
|
||||||
|
|
||||||
|
file_loader.load_async(
|
||||||
|
glib::Priority::default(),
|
||||||
|
Some(&cancellable),
|
||||||
|
|_, _| {},
|
||||||
|
|result| {
|
||||||
|
if result.is_err() {
|
||||||
|
panic!(result.err());
|
||||||
}
|
}
|
||||||
|
},
|
||||||
let file_loader: FileLoader = FileLoader::new(&buffer, &file);
|
);
|
||||||
|
|
||||||
file_loader.load_async(
|
|
||||||
glib::Priority::default(),
|
|
||||||
Some(&cancellable),
|
|
||||||
|_, _| {},
|
|
||||||
|result| {
|
|
||||||
if result.is_err() {
|
|
||||||
panic!(result.err());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
None => println!("It does not seem like {:?} has a type", filepath),
|
|
||||||
},
|
|
||||||
Err(e) => println!(
|
|
||||||
"Could not retrieve file information for {:?} because:\n{}",
|
|
||||||
filepath, e
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this
|
this
|
||||||
|
|
|
@ -92,90 +92,73 @@ impl WorkspaceImplementedEditor for EchidnaEditor {
|
||||||
.path()
|
.path()
|
||||||
.expect("Could not get the file path of the file.");
|
.expect("Could not get the file path of the file.");
|
||||||
let filepath = Path::new(&filepath_raw);
|
let filepath = Path::new(&filepath_raw);
|
||||||
let file_info_result =
|
let info = file
|
||||||
file.query_info("*", gio::FileQueryInfoFlags::NONE, Some(&cancellable));
|
.query_info("*", gio::FileQueryInfoFlags::NONE, Some(&cancellable))
|
||||||
|
.expect(format!(
|
||||||
|
"Could not retrieve file information for {:?}",
|
||||||
|
filepath
|
||||||
|
));
|
||||||
|
let content_type = info
|
||||||
|
.content_type()
|
||||||
|
.expect(format!("Found no content type for {:?}", filepath));
|
||||||
|
println!(
|
||||||
|
"Opened {} and found its content type is {}.",
|
||||||
|
"file",
|
||||||
|
content_type.to_string()
|
||||||
|
);
|
||||||
|
let content_cancellable = Cancellable::new();
|
||||||
|
let content = file
|
||||||
|
.load_contents(Some(&content_cancellable))
|
||||||
|
.expect("Could not load the file contents for {:?}", filepath);
|
||||||
|
|
||||||
match file_info_result {
|
let (int_vec, _byte_string) = content;
|
||||||
Ok(info) => {
|
let workspace = serde_json::from_slice::<MonacoWorkspace>(&int_vec).expect(format!(
|
||||||
match info.content_type() {
|
"Could not parse the workspace file of {:?}",
|
||||||
Some(content_type) => {
|
filepath
|
||||||
println!(
|
));
|
||||||
"Opened {} and found its content type is {}.",
|
|
||||||
"file",
|
|
||||||
content_type.to_string()
|
|
||||||
);
|
|
||||||
let content_cancellable = Cancellable::new();
|
|
||||||
let file_content = file.load_contents(Some(&content_cancellable));
|
|
||||||
|
|
||||||
match file_content {
|
for folder in workspace.folders {
|
||||||
Ok(content) => {
|
let path = RelativePath::new(&folder.path);
|
||||||
let (int_vec, _byte_string) = content;
|
let folder = File::for_path(path.to_path(filepath));
|
||||||
|
|
||||||
match serde_json::from_slice::<MonacoWorkspace>(&int_vec) {
|
// Do something with the folder, perhaps lists its child and .
|
||||||
Ok(workspace) => {
|
self.open_folder(folder);
|
||||||
for folder in workspace.folders {
|
|
||||||
let path = RelativePath::new(&folder.path);
|
|
||||||
|
|
||||||
let folder = File::for_path(path.to_path(filepath));
|
|
||||||
|
|
||||||
// Do something with the folder, perhaps lists its child and .
|
|
||||||
self.open_folder(folder);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => println!(
|
|
||||||
"Could not parse {:#?} because of:\n{}",
|
|
||||||
filepath, e
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => println!("Could not open {:?} because:\n{}", filepath, e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => println!("It does not seem like {:?} has a type", filepath),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => println!(
|
|
||||||
"Could not retrieve file information for {:?} because:\n{}",
|
|
||||||
filepath, e
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
fn recursive_add_files_into_tree_store(&self, parent_file: File, tree: &TreeStore) {
|
fn recursive_add_files_into_tree_store(&self, parent_file: File, tree: &TreeStore) {
|
||||||
let child_enumerate_cancellable = Cancellable::new();
|
let child_enumerate_cancellable = Cancellable::new();
|
||||||
let child_files = parent_file.enumerate_children(
|
let child_files = parent_file
|
||||||
|
.enumerate_children(
|
||||||
"*",
|
"*",
|
||||||
FileQueryInfoFlags::NONE,
|
FileQueryInfoFlags::NONE,
|
||||||
Some(&child_enumerate_cancellable),
|
Some(&child_enumerate_cancellable),
|
||||||
);
|
)
|
||||||
let filepath = &parent_file
|
.expect(
|
||||||
.path()
|
format!(
|
||||||
.expect("Could not get the file path of the file.");
|
|
||||||
match child_files {
|
|
||||||
Ok(files) => {
|
|
||||||
for file_iter in files {
|
|
||||||
match file_iter {
|
|
||||||
Ok(file_info) => {
|
|
||||||
let file = parent_file.child(file_info.name());
|
|
||||||
let tree_iter = tree.append(None);
|
|
||||||
tree.set_value(&tree_iter, 2, &file_info.name().to_str().to_value());
|
|
||||||
|
|
||||||
if file_info.file_type() == FileType::Directory {
|
|
||||||
self.recursive_add_files_into_tree_store(file, tree);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => println!("Could not get information on file because of:\n{}", e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => println!(
|
|
||||||
"Could not look up the children files of {:?} because:\n{:#?}",
|
"Could not look up the children files of {:?} because:\n{:#?}",
|
||||||
filepath, e
|
filepath
|
||||||
),
|
)
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
let filepath = &parent_file
|
||||||
|
.path()
|
||||||
|
.expect("Could not get the file path of the file.");
|
||||||
|
|
||||||
|
for file_iter in files {
|
||||||
|
let file_info = file_iter.expect();
|
||||||
|
let file = parent_file.child(file_info.name());
|
||||||
|
let tree_iter = tree.append(None);
|
||||||
|
tree.set_value(&tree_iter, 2, &file_info.name().to_str().to_value());
|
||||||
|
|
||||||
|
if file_info.file_type() == FileType::Directory {
|
||||||
|
self.recursive_add_files_into_tree_store(file, tree);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue