init profile TOFU features

This commit is contained in:
yggverse 2025-07-23 02:13:10 +03:00
parent 5aa6b1464c
commit f831212d40
11 changed files with 403 additions and 44 deletions

View file

@ -0,0 +1,52 @@
use anyhow::Result;
use gtk::{
gio::TlsCertificate,
glib::{DateTime, GString},
};
use sourceview::prelude::TlsCertificateExt;
#[derive(PartialEq, Eq, Hash)]
pub struct Certificate {
id: Option<i64>,
time: DateTime,
tls_certificate: TlsCertificate,
}
impl Certificate {
// Constructors
pub fn from_db(id: Option<i64>, pem: &str, time: DateTime) -> Result<Self> {
Ok(Self {
id,
time,
tls_certificate: TlsCertificate::from_pem(pem)?,
})
}
pub fn from_tls_certificate(tls_certificate: TlsCertificate) -> Result<Self> {
Ok(Self {
id: None,
time: DateTime::now_local()?,
tls_certificate,
})
}
// Getters
pub fn pem(&self) -> GString {
self.tls_certificate.certificate_pem().unwrap()
}
pub fn id(&self) -> Option<i64> {
self.id
}
pub fn time(&self) -> &DateTime {
&self.time
}
pub fn tls_certificate(&self) -> &TlsCertificate {
&self.tls_certificate
}
}

View file

@ -0,0 +1,97 @@
mod row;
use anyhow::Result;
use gtk::glib::DateTime;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use row::Row;
use sqlite::Transaction;
pub struct Database {
pool: Pool<SqliteConnectionManager>,
profile_id: i64,
}
impl Database {
// Constructors
/// Create new `Self`
pub fn init(pool: &Pool<SqliteConnectionManager>, profile_id: i64) -> Self {
Self {
pool: pool.clone(),
profile_id,
}
}
// Getters
/// Get records from database with optional filter by `request`
pub fn records(&self) -> Result<Vec<Row>> {
select(&self.pool.get()?.unchecked_transaction()?, self.profile_id)
}
// Setters
/// Create new record in database
/// * return last insert ID on success
pub fn add(&self, time: &DateTime, pem: &str) -> Result<i64> {
let mut connection = self.pool.get()?;
let tx = connection.transaction()?;
let id = insert(&tx, self.profile_id, time, pem)?;
tx.commit()?;
Ok(id)
}
}
// Low-level DB API
pub fn init(tx: &Transaction) -> Result<usize> {
Ok(tx.execute(
"CREATE TABLE IF NOT EXISTS `profile_tofu`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`profile_id` INTEGER NOT NULL,
`time` INTEGER NOT NULL,
`pem` TEXT NOT NULL,
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`)
)",
[],
)?)
}
pub fn insert(tx: &Transaction, profile_id: i64, time: &DateTime, pem: &str) -> Result<i64> {
tx.execute(
"INSERT INTO `profile_tofu` (
`profile_id`,
`time`,
`pem`
) VALUES (?, ?, ?)",
(profile_id, time.to_unix(), pem),
)?;
Ok(tx.last_insert_rowid())
}
pub fn select(tx: &Transaction, profile_id: i64) -> Result<Vec<Row>> {
let mut stmt = tx.prepare(
"SELECT `id`, `profile_id`, `time`, `pem` FROM `profile_tofu` WHERE `profile_id` = ?",
)?;
let result = stmt.query_map([profile_id], |row| {
Ok(Row {
id: row.get(0)?,
//profile_id: row.get(1)?,
time: DateTime::from_unix_local(row.get(2)?).unwrap(),
pem: row.get(3)?,
})
})?;
let mut records = Vec::new();
for record in result {
let table = record?;
records.push(table);
}
Ok(records)
}

View file

@ -0,0 +1,5 @@
pub struct Row {
pub id: i64,
pub pem: String,
pub time: gtk::glib::DateTime,
}