Skip to main content

Database

Struct Database 

Source
pub struct Database { /* private fields */ }
Expand description

Thread-safe wrapper around a SQLite connection for URL and API key storage.

Implementations§

Source§

impl Database

Source

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.

Source

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.

Source

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.

Source

pub fn list_users(&self) -> Result<Vec<String>, DatabaseError>

Returns the usernames of all active users.

§Errors

Returns DatabaseError::Sqlite on a database error.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,