unobtanium 3.0.0

Opinioated Web search engine library with crawler and viewer companion.
Documentation
use rusqlite::Connection;

use std::path::Path;

use crate::database::Database;
use crate::database::DatabaseError;
use crate::database::DatabaseOpeningError;
use crate::database::DatabaseTransaction;

pub struct CrawlerDatabase {
	base_db: Database,
}

pub struct CrawlerDatabaseTransaction<'a> {
	pub base_transaction: DatabaseTransaction<'a>
}

impl CrawlerDatabase {
	/// Initalize from a base database.
	pub fn initalize_from_base(db: Database) -> Result<Self, DatabaseOpeningError> {
		let crawler_db = Self {
			base_db: db,
		};
		crawler_db.check_crawler_schema()?;
		crawler_db.initalize_crawler_database()
			.map_err(|e| e.while_initlizing(crawler_db.base_db.path()))?;
		Ok(crawler_db)
	}

	pub fn open_writable(path: impl AsRef<Path>) -> Result<Self, DatabaseOpeningError> {
		let database = Database::open_writable(&path)?;
		Self::initalize_from_base(database)
	}

	pub fn open_read_only(path: impl AsRef<Path>) -> Result<Self, DatabaseOpeningError> {
		let database = Self {
			base_db: Database::open_read_only(&path)?
		};
		database.check_crawler_schema()?;
		Ok(database)
	}


	/// Grants access read only to the  base database.
	pub fn base(&self) -> &Database {
		&self.base_db
	}

	/// Grants mutable access to the uderlying base database.
	pub fn base_mut(&mut self) -> &mut Database {
		&mut self.base_db
	}

	/// Starts a database transaction and returns a handle for it.
	pub fn start_transaction(
		&mut self
	) -> Result<CrawlerDatabaseTransaction, DatabaseError> {
		return Ok(CrawlerDatabaseTransaction{
			base_transaction: self.base_db.start_transaction()?,
		})
	}

	/// Returns a handle for the database connection.
	pub fn connection(&self) -> &Connection {
		&self.base_db.connection
	}

}

impl<'a> CrawlerDatabaseTransaction<'a> {

	/// Returns a handle for the database connection.
	pub fn connection(&self) -> &Connection {
		&self.base_transaction.transaction
	}

	/// Commit changes made within this transaction to the database.
	pub fn commit(self) -> Result<(), DatabaseError> {
		return self.base_transaction.commit();
	}
}