pub struct Database { /* private fields */ }Expand description
Thread-safe wrapper around a SQLite connection for URL and API key storage.
Implementations§
Source§impl Database
impl Database
Sourcepub fn new(path: impl AsRef<Path>, create: bool) -> Result<Self, DatabaseError>
pub fn new(path: impl AsRef<Path>, create: bool) -> Result<Self, DatabaseError>
Opens (and optionally creates) the database at path.
Set create to true to allow creating a new database file;
set it to false to fail if the file does not already exist.
§Errors
Returns DatabaseError::Sqlite if the file cannot be opened.
Sourcepub fn init(&self) -> Result<(), DatabaseError>
pub fn init(&self) -> Result<(), DatabaseError>
Creates the required tables if they do not already exist.
§Errors
Returns DatabaseError::Sqlite if the schema cannot be applied.
Sourcepub fn create_user(&self, username: &str) -> Result<(), DatabaseError>
pub fn create_user(&self, username: &str) -> Result<(), DatabaseError>
Creates a new active user with the given username.
§Errors
Returns DatabaseError::UsernameAlreadyInUse if the username
is already taken, or DatabaseError::Sqlite on a database error.
Sourcepub fn list_users(&self) -> Result<Vec<String>, DatabaseError>
pub fn list_users(&self) -> Result<Vec<String>, DatabaseError>
Returns the usernames of all active users.
§Errors
Returns DatabaseError::Sqlite on a database error.
Sourcepub fn delete_user(&self, username: &str) -> Result<(), DatabaseError>
pub fn delete_user(&self, username: &str) -> Result<(), DatabaseError>
Deactivates the user with the given username and all of their
API keys.
§Errors
Returns DatabaseError::NotFound if no active user with that
name exists, or DatabaseError::Sqlite on a database error.
Sourcepub fn create_api_key(&self, username: &str) -> Result<String, DatabaseError>
pub fn create_api_key(&self, username: &str) -> Result<String, DatabaseError>
Generates and stores a new API key for username, returning the
plaintext key.
§Errors
Returns DatabaseError::NotFound if no active user with that
name exists, DatabaseError::CouldNotGenerateApiKey if a
unique key could not be generated, or DatabaseError::Sqlite
on a database error.
Sourcepub fn check_api_key(&self, key: &str) -> Result<String, DatabaseError>
pub fn check_api_key(&self, key: &str) -> Result<String, DatabaseError>
Validates a plaintext API key and returns the associated username.
§Errors
Returns DatabaseError::NotFound if the key is invalid or
belongs to an inactive user, DatabaseError::ApiKey if the key
cannot be decoded, or DatabaseError::Sqlite on a database error.
Sourcepub fn check_api_key_by_hash(
&self,
key_hash: &str,
) -> Result<String, DatabaseError>
pub fn check_api_key_by_hash( &self, key_hash: &str, ) -> Result<String, DatabaseError>
Validates an API key by its SHA-256 hex hash and returns the associated username.
§Errors
Returns DatabaseError::NotFound if the hash is not found or
belongs to an inactive user, or DatabaseError::Sqlite on a
database error.
Sourcepub fn list_api_keys(
&self,
username: &str,
) -> Result<Vec<String>, DatabaseError>
pub fn list_api_keys( &self, username: &str, ) -> Result<Vec<String>, DatabaseError>
Returns the SHA-256 hex hashes of all active API keys for
username.
§Errors
Returns DatabaseError::Sqlite on a database error.
Sourcepub fn delete_api_key(&self, key: &str) -> Result<(), DatabaseError>
pub fn delete_api_key(&self, key: &str) -> Result<(), DatabaseError>
Deactivates an API key identified by its plaintext value.
§Errors
Returns DatabaseError::NotFound if the key is not found,
DatabaseError::ApiKey if the key cannot be decoded, or
DatabaseError::Sqlite on a database error.
Sourcepub fn delete_api_key_by_hash(
&self,
key_hash: &str,
) -> Result<(), DatabaseError>
pub fn delete_api_key_by_hash( &self, key_hash: &str, ) -> Result<(), DatabaseError>
Deactivates an API key identified by its SHA-256 hex hash.
§Errors
Returns DatabaseError::NotFound if the hash is not found, or
DatabaseError::Sqlite on a database error.
Sourcepub fn get_url(&self, code: &str) -> Result<String, DatabaseError>
pub fn get_url(&self, code: &str) -> Result<String, DatabaseError>
Looks up the full URL for a short code and increments its hit
counter.
§Errors
Returns DatabaseError::NotFound if the code does not exist, or
DatabaseError::Sqlite on a database error.
Sourcepub fn create_code(
&self,
url: &str,
code: &str,
created_by: &str,
) -> Result<(), DatabaseError>
pub fn create_code( &self, url: &str, code: &str, created_by: &str, ) -> Result<(), DatabaseError>
Stores a new short code mapping to url, attributed to
created_by.
§Errors
Returns DatabaseError::CodeAlreadyInUse if code is already
taken, or DatabaseError::Sqlite on a database error.
Sourcepub fn list_codes(&self) -> Result<Vec<UrlInfo>, DatabaseError>
pub fn list_codes(&self) -> Result<Vec<UrlInfo>, DatabaseError>
Returns information about all shortened URLs, ordered by creation time.
§Errors
Returns DatabaseError::Sqlite on a database error.
Sourcepub fn get_url_info(&self, code: &str) -> Result<UrlInfo, DatabaseError>
pub fn get_url_info(&self, code: &str) -> Result<UrlInfo, DatabaseError>
Returns information about a shortened URL without incrementing its hit counter.
§Errors
Returns DatabaseError::NotFound if the code does not exist, or
DatabaseError::Sqlite on a database error.
Sourcepub fn delete_code(&self, code: &str) -> Result<(), DatabaseError>
pub fn delete_code(&self, code: &str) -> Result<(), DatabaseError>
Deletes the short code and its URL mapping from the database.
§Errors
Returns DatabaseError::NotFound if the code does not exist, or
DatabaseError::Sqlite on a database error.