implement to_string method, prevent memory index overwrite on validation step

This commit is contained in:
yggverse 2024-11-23 18:04:57 +02:00
parent cda94cba2e
commit c86dca53bd
10 changed files with 100 additions and 21 deletions

View File

@ -119,18 +119,24 @@ impl Gemini {
Value::ProfileIdentityGeminiId(value) => Some(value),
Value::UseGuestSession => None,
Value::GenerateNewAuth => Some(
profile
match profile
.identity
.gemini
.make(None, &widget.form.name.value().unwrap())
.unwrap(),
{
Ok(profile_identity_gemini_id) => profile_identity_gemini_id,
Err(reason) => todo!("{}", reason.to_string()),
},
),
Value::ImportPem => Some(
profile
match profile
.identity
.gemini
.add(&widget.form.file.pem.take().unwrap())
.unwrap(),
{
Ok(profile_identity_gemini_id) => profile_identity_gemini_id,
Err(reason) => todo!("{}", reason.to_string()),
},
),
};

View File

@ -98,7 +98,7 @@ impl Profile {
// Init identity component
let identity = Rc::new(match Identity::new(connection, profile_id) {
Ok(result) => result,
Err(reason) => todo!("{:?}", reason),
Err(reason) => todo!("{:?}", reason.to_string()),
});
// Result

View File

@ -28,12 +28,12 @@ impl Bookmark {
match database.records(None) {
Ok(records) => {
for record in records {
if memory.add(record.request, record.id).is_err() {
todo!()
if let Err(reason) = memory.add(record.request, record.id) {
todo!("{}", reason.to_string())
}
}
}
Err(reason) => todo!("{reason}"),
Err(reason) => todo!("{}", reason.to_string()),
}
// Return new `Self`

View File

@ -23,8 +23,17 @@ impl Memory {
/// Add new record with `request` as key and `id` as value
/// * validate record with same key does not exist yet
pub fn add(&self, request: String, id: i64) -> Result<(), Error> {
match self.index.borrow_mut().insert(request, id) {
Some(key) => Err(Error::Overwrite(key)), // @TODO prevent?
// Borrow shared index access
let mut index = self.index.borrow_mut();
// Prevent existing key overwrite
if index.contains_key(&request) {
return Err(Error::Overwrite(request));
}
// Slot should be free, let check it twice
match index.insert(request, id) {
Some(_) => return Err(Error::Unexpected),
None => Ok(()),
}
}
@ -34,15 +43,15 @@ impl Memory {
pub fn delete(&self, request: &str) -> Result<(), Error> {
match self.index.borrow_mut().remove(request) {
Some(_) => Ok(()),
None => Err(Error::NotFound),
None => Err(Error::Unexpected), // @TODO
}
}
/// Get `id` by `request` from memory index
pub fn get(&self, request: &str) -> Result<i64, Error> {
match self.index.borrow().get(request) {
Some(&id) => Ok(id),
None => Err(Error::NotFound),
Some(&value) => Ok(value),
None => Err(Error::Unexpected), // @TODO
}
}
}

View File

@ -1,5 +1,16 @@
#[derive(Debug)]
pub enum Error {
NotFound,
Overwrite(i64),
Overwrite(String),
Unexpected,
}
impl Error {
pub fn to_string(&self) -> String {
match self {
Self::Overwrite(key) => {
format!("Overwrite attempt for existing record `{key}`")
}
Self::Unexpected => format!("Unexpected error"),
}
}
}

View File

@ -55,7 +55,7 @@ impl Identity {
if let Some(id) = self.gemini.auth.memory.match_priority(request) {
match self.gemini.memory.get(id) {
Ok(pem) => return Some(pem),
Err(reason) => todo!("{:?}", reason),
Err(reason) => todo!("{:?}", reason.to_string()),
}
}
None

View File

@ -3,3 +3,16 @@ pub enum Error {
Database(sqlite::Error),
Gemini(super::gemini::Error),
}
impl Error {
pub fn to_string(&self) -> String {
match self {
Self::Database(reason) => {
format!("Database error: {}", reason.to_string())
}
Self::Gemini(reason) => {
format!("Could not init Gemini identity: {}", reason.to_string())
}
}
}
}

View File

@ -1,7 +1,22 @@
#[derive(Debug)]
pub enum Error {
Auth(super::auth::Error),
Certificate(Box<dyn std::error::Error>), // @TODO
Certificate(Box<dyn std::error::Error>),
Database(sqlite::Error),
Memory(super::memory::Error),
}
impl Error {
pub fn to_string(&self) -> String {
match self {
Self::Auth(reason) => format!("Could not create auth: {}", reason.to_string()),
Self::Certificate(reason) => {
format!("Could not create certificate: {}", reason.to_string())
}
Self::Database(reason) => {
format!("Database error: {}", reason.to_string())
}
Self::Memory(reason) => format!("Memory error: {}", reason.to_string()),
}
}
}

View File

@ -22,9 +22,18 @@ impl Memory {
/// Add new record with `id` as key and `pem` as value
/// * validate record with same key does not exist yet
pub fn add(&self, id: i64, pem: String) -> Result<(), Error> {
match self.index.borrow_mut().insert(id, pem) {
Some(key) => Err(Error::Overwrite(key)), // @TODO prevent?
pub fn add(&self, profile_identity_gemini_id: i64, pem: String) -> Result<(), Error> {
// Borrow shared index access
let mut index = self.index.borrow_mut();
// Prevent existing key overwrite
if index.contains_key(&profile_identity_gemini_id) {
return Err(Error::Overwrite(profile_identity_gemini_id));
}
// Slot should be free, let check it twice
match index.insert(profile_identity_gemini_id, pem) {
Some(_) => return Err(Error::Unexpected),
None => Ok(()),
}
}

View File

@ -2,5 +2,21 @@
pub enum Error {
Clear,
NotFound(i64),
Overwrite(String),
Overwrite(i64),
Unexpected,
}
impl Error {
pub fn to_string(&self) -> String {
match self {
Self::Clear => format!("Could not cleanup memory index"),
Self::NotFound(key) => {
format!("Record `{key}` not found in memory index")
}
Self::Overwrite(key) => {
format!("Overwrite attempt for existing record `{key}`")
}
Self::Unexpected => format!("Unexpected error"),
}
}
}