config_tools: filter out non-existing history path

filter out non-existing history path

Tracked-On: #6691
Signed-off-by: Weiyi Feng <weiyix.feng@intel.com>
This commit is contained in:
Weiyi Feng 2022-06-14 08:52:35 +08:00 committed by acrnsi-robot
parent 3c47f285cc
commit 35e4aed110

View File

@ -283,9 +283,24 @@ static mut WORKING_FOLDER: String = String::new();
#[tauri::command]
pub fn get_history(history_type: HistoryType) -> Result<String, ()> {
let configurator = Configurator::new();
let history = serde_json::to_string(configurator.get_history(history_type))
.unwrap_or_else(|_| String::from("[]"));
Ok(history)
let history = configurator.get_history(history_type);
// filter out empty string and not exist history path
let clean_history: Vec<&String> = match history_type {
HistoryType::WorkingFolder => history
.into_iter()
.filter(|s| !s.is_empty())
.filter(|s| Path::new(s).is_dir())
.collect::<Vec<_>>(),
_ => history
.into_iter()
.filter(|s| !s.is_empty())
.filter(|s| Path::new(s).is_file())
.collect::<Vec<_>>(),
};
let history_json_text =
serde_json::to_string(&clean_history).unwrap_or_else(|_| String::from("[]"));
Ok(history_json_text)
}
#[tauri::command]