Skip to content

Commit 6acd4f0

Browse files
committed
Small cleanup
1 parent ae2b7f4 commit 6acd4f0

1 file changed

Lines changed: 19 additions & 25 deletions

File tree

plugins/recent-doc/src/commands.rs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -89,37 +89,31 @@ pub(crate) fn get_recent_documents() -> Result<Vec<String>> {
8989
#[cfg(target_os = "windows")]
9090
{
9191
use std::fs;
92-
use std::path::PathBuf;
93-
use windows::{
94-
core::PWSTR,
95-
Win32::System::Com::CoTaskMemFree,
96-
Win32::UI::Shell::{FOLDERID_Recent, SHGetKnownFolderPath, KNOWN_FOLDER_FLAG},
92+
use windows::Win32::{
93+
Foundation::GetLastError,
94+
System::Com::CoTaskMemFree,
95+
UI::Shell::{FOLDERID_Recent, SHGetKnownFolderPath, KNOWN_FOLDER_FLAG},
9796
};
9897
unsafe {
99-
let recent_path_ptr: PWSTR =
100-
SHGetKnownFolderPath(&FOLDERID_Recent, KNOWN_FOLDER_FLAG(0), None)?;
101-
102-
if !recent_path_ptr.is_null() {
103-
let recent_path = PWSTR::from_raw(recent_path_ptr.0);
104-
let recent_os_string = recent_path.to_string()?;
105-
let recent_folder = PathBuf::from(recent_os_string);
106-
107-
if let Ok(entries) = fs::read_dir(recent_folder) {
108-
for entry in entries.flatten() {
109-
let path = entry.path();
110-
111-
if path.extension().and_then(|s: &std::ffi::OsStr| s.to_str())
112-
== Some("lnk")
113-
{
114-
if let Ok(resolved_path) = resolve_shortcut(&path) {
115-
recent_docs.push(resolved_path);
116-
}
98+
let recent_path = SHGetKnownFolderPath(&FOLDERID_Recent, KNOWN_FOLDER_FLAG(0), None)?;
99+
if recent_path.is_null() {
100+
return Err(crate::error::Error::WindowsError(GetLastError().into()));
101+
}
102+
let recent_path_os_string = recent_path.to_hstring().to_os_string();
103+
104+
if let Ok(entries) = fs::read_dir(recent_path_os_string) {
105+
for entry in entries.flatten() {
106+
let path = entry.path();
107+
108+
if path.extension().and_then(|s: &std::ffi::OsStr| s.to_str()) == Some("lnk") {
109+
if let Ok(resolved_path) = resolve_shortcut(&path) {
110+
recent_docs.push(resolved_path);
117111
}
118112
}
119113
}
120-
121-
CoTaskMemFree(Some(recent_path_ptr.0 as *mut _));
122114
}
115+
116+
CoTaskMemFree(Some(recent_path.0 as *mut _));
123117
}
124118
}
125119

0 commit comments

Comments
 (0)