load_freedesktop/
cache.rs1use super::EntryData;
2use std::io::{Read, Write};
3
4pub fn get_cache_path() -> String {
5 std::env::home_dir()
6 .map(|p| p.to_string_lossy().to_string())
7 .unwrap_or("~".to_string())
8 + "/.cache/app_launch_ent.svd"
9}
10
11pub fn cache_entries(entries: Vec<EntryData>) -> Result<(), Box<dyn std::error::Error>> {
12 let path = get_cache_path();
13 let bytes = postcard::to_extend(&entries, Vec::new())?;
14 let mut file = std::fs::File::create(&path)?;
15 file.write_all(&bytes)?;
16 Ok(())
17}
18pub fn get_cached_entries() -> Result<Vec<EntryData>, Box<dyn std::error::Error>> {
19 let path = get_cache_path();
20 let file = std::fs::File::open(&path)?;
21 let bytes = file.bytes().filter_map(|b| b.ok()).collect::<Vec<_>>();
22 let ent = postcard::from_bytes::<Vec<EntryData>>(&bytes)?;
23 Ok(ent)
24}