mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-15 17:20:08 +00:00
update errors handle
This commit is contained in:
parent
48ad344adc
commit
efa3bc48c3
@ -59,25 +59,24 @@ impl Profile {
|
|||||||
// Init writable connection
|
// Init writable connection
|
||||||
let mut connection = match connection.write() {
|
let mut connection = match connection.write() {
|
||||||
Ok(connection) => connection,
|
Ok(connection) => connection,
|
||||||
Err(e) => todo!("{e}"),
|
Err(reason) => todo!("{reason}"),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Init new transaction
|
// Init new transaction
|
||||||
let transaction = match connection.transaction() {
|
let transaction = match connection.transaction() {
|
||||||
Ok(transaction) => transaction,
|
Ok(transaction) => transaction,
|
||||||
Err(e) => todo!("{e}"),
|
Err(reason) => todo!("{reason}"),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Begin migration
|
// Begin migration
|
||||||
match migrate(&transaction) {
|
match migrate(&transaction) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
// Confirm changes
|
// Confirm changes
|
||||||
match transaction.commit() {
|
if let Err(reason) = transaction.commit() {
|
||||||
Ok(_) => {} // @TODO
|
todo!("{reason}")
|
||||||
Err(e) => todo!("{e}"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => todo!("{e}"),
|
Err(reason) => todo!("{reason}"),
|
||||||
}
|
}
|
||||||
} // unlock database
|
} // unlock database
|
||||||
|
|
||||||
@ -85,18 +84,27 @@ impl Profile {
|
|||||||
let database = Rc::new(Database::new(connection.clone()));
|
let database = Rc::new(Database::new(connection.clone()));
|
||||||
|
|
||||||
// Get active profile or create new one
|
// Get active profile or create new one
|
||||||
let profile_id = Rc::new(match database.active() {
|
let profile_id = Rc::new(match database.active().unwrap() {
|
||||||
Some(profile) => profile.id,
|
Some(profile) => profile.id,
|
||||||
None => match database.add(true, DateTime::now_local().unwrap(), None) {
|
None => match database.add(true, DateTime::now_local().unwrap(), None) {
|
||||||
Ok(id) => id,
|
Ok(id) => id,
|
||||||
Err(_) => todo!(),
|
Err(reason) => todo!("{:?}", reason),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Init bookmark component @TODO handle errors
|
||||||
|
let bookmark = Rc::new(Bookmark::new(connection.clone(), profile_id.clone()));
|
||||||
|
|
||||||
|
// Init identity component
|
||||||
|
let identity = Rc::new(match Identity::new(connection, profile_id) {
|
||||||
|
Ok(result) => result,
|
||||||
|
Err(reason) => todo!("{:?}", reason),
|
||||||
|
});
|
||||||
|
|
||||||
// Result
|
// Result
|
||||||
Self {
|
Self {
|
||||||
bookmark: Rc::new(Bookmark::new(connection.clone(), profile_id.clone())),
|
bookmark,
|
||||||
identity: Rc::new(Identity::new(connection, profile_id).unwrap()), // @TODO handle
|
identity,
|
||||||
database,
|
database,
|
||||||
config_path,
|
config_path,
|
||||||
}
|
}
|
||||||
@ -105,8 +113,8 @@ impl Profile {
|
|||||||
|
|
||||||
pub fn migrate(tx: &Transaction) -> Result<(), String> {
|
pub fn migrate(tx: &Transaction) -> Result<(), String> {
|
||||||
// Migrate self components
|
// Migrate self components
|
||||||
if let Err(e) = database::init(tx) {
|
if let Err(reason) = database::init(tx) {
|
||||||
return Err(e.to_string());
|
return Err(reason.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delegate migration to children components
|
// Delegate migration to children components
|
||||||
|
@ -24,7 +24,7 @@ impl Memory {
|
|||||||
/// * validate record with same key does not exist yet
|
/// * validate record with same key does not exist yet
|
||||||
pub fn add(&self, request: String, id: i64) -> Result<(), Error> {
|
pub fn add(&self, request: String, id: i64) -> Result<(), Error> {
|
||||||
match self.index.borrow_mut().insert(request, id) {
|
match self.index.borrow_mut().insert(request, id) {
|
||||||
Some(_) => Err(Error::Overwrite), // @TODO prevent?
|
Some(key) => Err(Error::Overwrite(key)), // @TODO prevent?
|
||||||
None => Ok(()),
|
None => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
NotFound,
|
NotFound,
|
||||||
Overwrite,
|
Overwrite(i64),
|
||||||
}
|
}
|
||||||
|
@ -24,71 +24,45 @@ impl Database {
|
|||||||
// Getters
|
// Getters
|
||||||
|
|
||||||
/// Get all records
|
/// Get all records
|
||||||
pub fn records(&self) -> Vec<Table> {
|
pub fn records(&self) -> Result<Vec<Table>, Error> {
|
||||||
let readable = self.connection.read().unwrap();
|
let readable = self.connection.read().unwrap();
|
||||||
let tx = readable.unchecked_transaction().unwrap();
|
let tx = readable.unchecked_transaction()?;
|
||||||
select(&tx).unwrap()
|
select(&tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get active profile record if exist
|
/// Get active profile record if exist
|
||||||
pub fn active(&self) -> Option<Table> {
|
pub fn active(&self) -> Result<Option<Table>, Error> {
|
||||||
self.records().into_iter().find(|record| record.is_active)
|
let records = self.records()?;
|
||||||
|
Ok(records.into_iter().find(|record| record.is_active))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setters
|
// Setters
|
||||||
|
|
||||||
/// Create new record in `Self` database connected
|
/// Create new record in `Self` database connected
|
||||||
pub fn add(&self, is_active: bool, time: DateTime, name: Option<String>) -> Result<i64, ()> {
|
pub fn add(&self, is_active: bool, time: DateTime, name: Option<String>) -> Result<i64, Error> {
|
||||||
// Begin new transaction
|
// Begin new transaction
|
||||||
let mut writable = self.connection.write().unwrap();
|
let mut writable = self.connection.write().unwrap();
|
||||||
let tx = writable.transaction().unwrap();
|
let tx = writable.transaction()?;
|
||||||
|
|
||||||
// New record has active status
|
// New record has active status
|
||||||
if is_active {
|
if is_active {
|
||||||
// Deactivate other records as only one profile should be active
|
// Deactivate other records as only one profile should be active
|
||||||
for record in select(&tx).unwrap() {
|
for record in select(&tx)? {
|
||||||
let _ = update(&tx, record.id, false, record.time, record.name);
|
update(&tx, record.id, false, record.time, record.name)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new record
|
// Create new record
|
||||||
insert(&tx, is_active, time, name).unwrap();
|
insert(&tx, is_active, time, name)?;
|
||||||
|
|
||||||
// Hold insert ID for result
|
// Hold insert ID for result
|
||||||
let id = last_insert_id(&tx);
|
let id = last_insert_id(&tx);
|
||||||
|
|
||||||
// Done
|
// Done
|
||||||
match tx.commit() {
|
tx.commit()?;
|
||||||
Ok(_) => Ok(id),
|
|
||||||
Err(_) => Err(()), // @TODO
|
Ok(id)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @TODO not in use
|
|
||||||
/// Set `is_active` status `true` for the record with given profile ID
|
|
||||||
/// * reset other records to `false`
|
|
||||||
pub fn activate(&self, id: i64) -> Result<(), ()> {
|
|
||||||
// Begin new transaction
|
|
||||||
let mut writable = self.connection.write().unwrap();
|
|
||||||
let tx = writable.transaction().unwrap();
|
|
||||||
|
|
||||||
// Deactivate other records as only one profile should be active
|
|
||||||
for record in select(&tx).unwrap() {
|
|
||||||
let _ = update(
|
|
||||||
&tx,
|
|
||||||
record.id,
|
|
||||||
if record.id == id { true } else { false },
|
|
||||||
record.time,
|
|
||||||
record.name,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Done
|
|
||||||
match tx.commit() {
|
|
||||||
Ok(_) => Ok(()),
|
|
||||||
Err(_) => Err(()),
|
|
||||||
} // @TODO make sure ID exist and was changed
|
|
||||||
} */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Low-level DB API
|
// Low-level DB API
|
||||||
|
@ -25,11 +25,14 @@ impl Identity {
|
|||||||
|
|
||||||
// Get active identity set for profile or create new one
|
// Get active identity set for profile or create new one
|
||||||
let profile_identity_id = Rc::new(match database.active() {
|
let profile_identity_id = Rc::new(match database.active() {
|
||||||
Some(identity) => identity.id,
|
Ok(result) => match result {
|
||||||
None => match database.add(profile_id, true) {
|
Some(identity) => identity.id,
|
||||||
Ok(id) => id,
|
None => match database.add(profile_id, true) {
|
||||||
Err(_) => return Err(Error::Database),
|
Ok(id) => id,
|
||||||
|
Err(reason) => return Err(Error::DatabaseAdd(reason)),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
Err(reason) => return Err(Error::DatabaseActive(reason)),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Init gemini component
|
// Init gemini component
|
||||||
@ -63,8 +66,8 @@ impl Identity {
|
|||||||
|
|
||||||
pub fn migrate(tx: &Transaction) -> Result<(), String> {
|
pub fn migrate(tx: &Transaction) -> Result<(), String> {
|
||||||
// Migrate self components
|
// Migrate self components
|
||||||
if let Err(e) = database::init(tx) {
|
if let Err(reason) = database::init(tx) {
|
||||||
return Err(e.to_string());
|
return Err(reason.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delegate migration to childs
|
// Delegate migration to childs
|
||||||
|
@ -22,35 +22,36 @@ impl Database {
|
|||||||
// Getters
|
// Getters
|
||||||
|
|
||||||
/// Get all records
|
/// Get all records
|
||||||
pub fn records(&self) -> Vec<Table> {
|
pub fn records(&self) -> Result<Vec<Table>, Error> {
|
||||||
let readable = self.connection.read().unwrap();
|
let readable = self.connection.read().unwrap();
|
||||||
let tx = readable.unchecked_transaction().unwrap();
|
let tx = readable.unchecked_transaction()?;
|
||||||
select(&tx).unwrap()
|
select(&tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get active identity record if exist
|
/// Get active identity record if exist
|
||||||
pub fn active(&self) -> Option<Table> {
|
pub fn active(&self) -> Result<Option<Table>, Error> {
|
||||||
self.records().into_iter().find(|record| record.is_active)
|
let records = self.records()?;
|
||||||
|
Ok(records.into_iter().find(|record| record.is_active))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setters
|
// Setters
|
||||||
|
|
||||||
/// Create new record in `Self` database connected
|
/// Create new record in `Self` database connected
|
||||||
pub fn add(&self, profile_id: Rc<i64>, is_active: bool) -> Result<i64, ()> {
|
pub fn add(&self, profile_id: Rc<i64>, is_active: bool) -> Result<i64, Error> {
|
||||||
// Begin new transaction
|
// Begin new transaction
|
||||||
let mut writable = self.connection.write().unwrap();
|
let mut writable = self.connection.write().unwrap();
|
||||||
let tx = writable.transaction().unwrap();
|
let tx = writable.transaction()?;
|
||||||
|
|
||||||
// New record has active status
|
// New record has active status
|
||||||
if is_active {
|
if is_active {
|
||||||
// Deactivate other records as only one profile should be active
|
// Deactivate other records as only one profile should be active
|
||||||
for record in select(&tx).unwrap() {
|
for record in select(&tx)? {
|
||||||
let _ = update(&tx, record.profile_id, record.id, false);
|
update(&tx, record.profile_id, record.id, false)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new record
|
// Create new record
|
||||||
insert(&tx, profile_id, is_active).unwrap();
|
insert(&tx, profile_id, is_active)?;
|
||||||
|
|
||||||
// Hold insert ID for result
|
// Hold insert ID for result
|
||||||
let id = last_insert_id(&tx);
|
let id = last_insert_id(&tx);
|
||||||
@ -58,7 +59,7 @@ impl Database {
|
|||||||
// Done
|
// Done
|
||||||
match tx.commit() {
|
match tx.commit() {
|
||||||
Ok(_) => Ok(id),
|
Ok(_) => Ok(id),
|
||||||
Err(_) => Err(()), // @TODO
|
Err(reason) => Err(reason),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
Database,
|
DatabaseActive(sqlite::Error),
|
||||||
|
DatabaseAdd(sqlite::Error),
|
||||||
GeminiInit(super::gemini::Error),
|
GeminiInit(super::gemini::Error),
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ impl Memory {
|
|||||||
/// * validate record with same key does not exist yet
|
/// * validate record with same key does not exist yet
|
||||||
pub fn add(&self, id: i64, pem: String) -> Result<(), Error> {
|
pub fn add(&self, id: i64, pem: String) -> Result<(), Error> {
|
||||||
match self.index.borrow_mut().insert(id, pem) {
|
match self.index.borrow_mut().insert(id, pem) {
|
||||||
Some(_) => Err(Error::Overwrite(id)), // @TODO prevent?
|
Some(key) => Err(Error::Overwrite(key)), // @TODO prevent?
|
||||||
None => Ok(()),
|
None => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
NotFound(i64),
|
NotFound(i64),
|
||||||
Overwrite(i64),
|
Overwrite(String),
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user