Skip to main content

vector_core/db/
saved.rs

1//! Files the user saved into one of Vector's folders. They are the user's: nothing Vector keeps,
2//! releases or clears there deletes one.
3
4/// The spelling a saved file is recorded under: its real path when it resolves.
5fn spelling(path: &str) -> String {
6    std::fs::canonicalize(path).map(|p| p.to_string_lossy().into_owned()).unwrap_or_else(|_| path.to_string())
7}
8
9pub fn mark(path: &str) -> Result<(), String> {
10    let conn = super::get_write_connection_guard_static()?;
11    conn.execute("INSERT OR IGNORE INTO saved_files (path) VALUES (?1)", [spelling(path)])
12        .map_err(|e| format!("mark saved: {e}"))?;
13    Ok(())
14}
15
16/// `path` holds a new file now, not the one that was saved there.
17pub fn unmark(path: &str) -> Result<(), String> {
18    let conn = super::get_write_connection_guard_static()?;
19    conn.execute("DELETE FROM saved_files WHERE path=?1 OR path=?2", [path.to_string(), spelling(path)])
20        .map_err(|e| format!("unmark saved: {e}"))?;
21    Ok(())
22}
23
24pub fn is_saved(path: &str) -> Result<bool, String> {
25    let conn = super::get_db_connection_guard_static()?;
26    conn.query_row(
27        "SELECT EXISTS(SELECT 1 FROM saved_files WHERE path=?1 OR path=?2)",
28        [path.to_string(), spelling(path)],
29        |r| r.get(0),
30    )
31    .map_err(|e| format!("is_saved: {e}"))
32}
33
34pub fn all() -> Result<Vec<String>, String> {
35    let conn = super::get_db_connection_guard_static()?;
36    let mut stmt = conn.prepare("SELECT path FROM saved_files").map_err(|e| format!("saved files: {e}"))?;
37    let rows = stmt.query_map([], |r| r.get::<_, String>(0)).map_err(|e| format!("saved files: {e}"))?;
38    Ok(rows.flatten().collect())
39}