Skip to main content

verso/store/
bookmarks.rs

1use rusqlite::{params, Connection, OptionalExtension};
2
3#[derive(Debug, Clone)]
4pub struct Bookmark {
5    pub book_id: i64,
6    pub mark: String,
7    pub spine_idx: u32,
8    pub char_offset: u64,
9    pub anchor_hash: String,
10}
11
12pub fn set_bookmark(c: &mut Connection, b: &Bookmark) -> anyhow::Result<()> {
13    c.execute(
14        "INSERT INTO bookmarks(book_id, mark, spine_idx, char_offset, anchor_hash)
15         VALUES (?,?,?,?,?)
16         ON CONFLICT(book_id, mark) DO UPDATE SET
17           spine_idx=excluded.spine_idx,
18           char_offset=excluded.char_offset,
19           anchor_hash=excluded.anchor_hash,
20           created_at=CURRENT_TIMESTAMP",
21        params![b.book_id, b.mark, b.spine_idx, b.char_offset, b.anchor_hash],
22    )?;
23    Ok(())
24}
25
26pub fn get_bookmark(c: &Connection, book_id: i64, mark: &str) -> anyhow::Result<Option<Bookmark>> {
27    Ok(c.query_row(
28        "SELECT book_id, mark, spine_idx, char_offset, anchor_hash
29         FROM bookmarks WHERE book_id = ? AND mark = ?",
30        params![book_id, mark],
31        |r| {
32            Ok(Bookmark {
33                book_id: r.get(0)?,
34                mark: r.get(1)?,
35                spine_idx: r.get(2)?,
36                char_offset: r.get(3)?,
37                anchor_hash: r.get(4)?,
38            })
39        },
40    )
41    .optional()?)
42}