replace url field with scope, change app version

This commit is contained in:
yggverse 2024-11-27 22:08:58 +02:00
parent 2e17858a39
commit 45f82e7837
4 changed files with 33 additions and 33 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "Yoda"
version = "0.8.2"
version = "0.9.0"
edition = "2021"
license = "MIT"
readme = "README.md"

View File

@ -11,7 +11,7 @@ use memory::Memory;
use sqlite::{Connection, Transaction};
use std::{rc::Rc, sync::RwLock};
/// API for `profile_identity_gemini_id` + `url` auth pairs operations
/// API for `profile_identity_gemini_id` + `scope` auth pairs operations
pub struct Auth {
pub database: Rc<Database>,
pub memory: Rc<Memory>,
@ -37,17 +37,17 @@ impl Auth {
// Actions
/// Apply `profile_identity_gemini_id` certificate as the auth for `url`
/// Apply `profile_identity_gemini_id` certificate as the auth for `scope`
/// * deactivate active auth by remove previous records from `Self` database
/// * reindex `Self` memory index on success
/// * return last insert `profile_identity_gemini_auth_id` on success
pub fn apply(&self, profile_identity_gemini_id: i64, url: &str) -> Result<i64, Error> {
// Cleanup records match `url` (unauthorize)
self.remove(url)?;
pub fn apply(&self, profile_identity_gemini_id: i64, scope: &str) -> Result<i64, Error> {
// Cleanup records match `scope` (unauthorize)
self.remove(scope)?;
// Create new record (auth)
let profile_identity_gemini_auth_id =
match self.database.add(profile_identity_gemini_id, url) {
match self.database.add(profile_identity_gemini_id, scope) {
Ok(id) => id,
Err(reason) => return Err(Error::Database(reason)),
};
@ -60,8 +60,8 @@ impl Auth {
}
/// Remove all records match request (unauthorize)
pub fn remove(&self, url: &str) -> Result<(), Error> {
match self.database.records(Some(url)) {
pub fn remove(&self, scope: &str) -> Result<(), Error> {
match self.database.records(Some(scope)) {
Ok(records) => {
for record in records {
if let Err(reason) = self.database.delete(record.id) {
@ -88,7 +88,7 @@ impl Auth {
for record in records {
if let Err(reason) = self
.memory
.add(record.url, record.profile_identity_gemini_id)
.add(record.scope, record.profile_identity_gemini_id)
{
return Err(Error::Memory(reason));
}

View File

@ -4,10 +4,10 @@ use std::{rc::Rc, sync::RwLock};
pub struct Table {
pub id: i64,
pub profile_identity_gemini_id: i64,
pub url: String,
pub scope: String,
}
/// Storage for `profile_identity_gemini_id` + `url` auth pairs
/// Storage for `profile_identity_gemini_id` + `scope` auth pairs
pub struct Database {
connection: Rc<RwLock<Connection>>,
}
@ -23,13 +23,13 @@ impl Database {
// Actions
/// Create new record in database
pub fn add(&self, profile_identity_gemini_id: i64, url: &str) -> Result<i64, Error> {
pub fn add(&self, profile_identity_gemini_id: i64, scope: &str) -> Result<i64, Error> {
// Begin new transaction
let mut writable = self.connection.write().unwrap(); // @TODO
let tx = writable.transaction()?;
// Create new record
insert(&tx, profile_identity_gemini_id, url)?;
insert(&tx, profile_identity_gemini_id, scope)?;
// Hold insert ID for result
let id = last_insert_id(&tx);
@ -59,11 +59,11 @@ impl Database {
// Getters
/// Get records from database match current `profile_id` optionally filtered by `url`
pub fn records(&self, url: Option<&str>) -> Result<Vec<Table>, Error> {
/// Get records from database match current `profile_id` optionally filtered by `scope`
pub fn records(&self, scope: Option<&str>) -> Result<Vec<Table>, Error> {
let readable = self.connection.read().unwrap(); // @TODO
let tx = readable.unchecked_transaction()?;
select(&tx, url)
select(&tx, scope)
}
}
@ -75,10 +75,10 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`profile_identity_gemini_id` INTEGER NOT NULL,
`url` VARCHAR(1024) NOT NULL,
`scope` VARCHAR(1024) NOT NULL,
FOREIGN KEY (`profile_identity_gemini_id`) REFERENCES `profile_identity_gemini`(`id`),
UNIQUE (`url`)
UNIQUE (`scope`)
)",
[],
)
@ -87,14 +87,14 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
pub fn insert(
tx: &Transaction,
profile_identity_gemini_id: i64,
url: &str,
scope: &str,
) -> Result<usize, Error> {
tx.execute(
"INSERT INTO `profile_identity_gemini_auth` (
`profile_identity_gemini_id`,
`url`
`scope`
) VALUES (?, ?)",
(profile_identity_gemini_id, url),
(profile_identity_gemini_id, scope),
)
}
@ -105,21 +105,21 @@ pub fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> {
)
}
pub fn select(tx: &Transaction, url: Option<&str>) -> Result<Vec<Table>, Error> {
pub fn select(tx: &Transaction, scope: Option<&str>) -> Result<Vec<Table>, Error> {
let mut stmt = tx.prepare(
"SELECT `id`,
`profile_identity_gemini_id`,
`url`
`scope`
FROM `profile_identity_gemini_auth`
WHERE `url` LIKE ?",
WHERE `scope` LIKE ?",
)?;
let result = stmt.query_map([url.unwrap_or("%")], |row| {
let result = stmt.query_map([scope.unwrap_or("%")], |row| {
Ok(Table {
id: row.get(0)?,
profile_identity_gemini_id: row.get(1)?,
url: row.get(2)?,
scope: row.get(2)?,
})
})?;

View File

@ -23,19 +23,19 @@ impl Memory {
// Actions
/// Add new record with `url` as key and `profile_identity_gemini_id` as value
/// Add new record with `scope` as key and `profile_identity_gemini_id` as value
/// * validate record with same key does not exist yet
pub fn add(&self, url: String, profile_identity_gemini_id: i64) -> Result<(), Error> {
pub fn add(&self, scope: String, profile_identity_gemini_id: i64) -> Result<(), Error> {
// Borrow shared index access
let mut index = self.index.borrow_mut();
// Prevent existing key overwrite
if index.contains_key(&url) {
return Err(Error::Overwrite(url));
if index.contains_key(&scope) {
return Err(Error::Overwrite(scope));
}
// Slot should be free, let check it twice
match index.insert(url, profile_identity_gemini_id) {
match index.insert(scope, profile_identity_gemini_id) {
Some(_) => Err(Error::Unexpected),
None => Ok(()),
}
@ -58,7 +58,7 @@ impl Memory {
pub fn match_priority(&self, request: &str) -> Option<Auth> {
let mut result = Vec::new();
// Get all records starts with URL cached, collect length for priority
// Get all records starts with `scope` cached
for (scope, &profile_identity_gemini_id) in self.index.borrow().iter() {
if request.starts_with(scope) {
result.push(Auth {