BlueGit

ict rusty-teams

Files

rusty-teams / crates / store / src / meta.rs

meta.rs

Initial commit
metis Daniel Däschle · pushed by metis · 7 weeks ago
06e0ae51 History
meta.rs 1.1 KiB Download Raw
1use chrono::{DateTime, Utc};
2use rusqlite::{OptionalExtension, params};
3
4use crate::error::Result;
5use crate::store::Store;
6use crate::time::{from_millis, to_millis};
7
8impl Store {
9 pub fn meta(&self, key: &str) -> Result<Option<String>> {
10 let connection = self.lock()?;
11 Ok(connection
12 .query_row("SELECT value FROM meta WHERE key = ?1", [key], |row| {
13 row.get(0)
14 })
15 .optional()?)
16 }
17
18 pub fn set_meta(&self, key: &str, value: &str) -> Result<()> {
19 let connection = self.lock()?;
20 connection.execute(
21 "INSERT INTO meta (key, value) VALUES (?1, ?2) ON CONFLICT (key) DO UPDATE SET value = excluded.value",
22 params![key, value],
23 )?;
24 Ok(())
25 }
26
27 pub fn meta_time(&self, key: &str) -> Result<Option<DateTime<Utc>>> {
28 Ok(self
29 .meta(key)?
30 .and_then(|value| value.parse().ok())
31 .map(from_millis))
32 }
33
34 pub fn set_meta_time(&self, key: &str, time: DateTime<Utc>) -> Result<()> {
35 self.set_meta(key, &to_millis(time).to_string())
36 }
37}