Trait pastebin::DbInterface [] [src]

pub trait DbInterface: Send + Sync {
    type Error: Send + Sync + Error + 'static;
    fn store_data(
        &self,
        data: Vec<u8>,
        file_name: Option<String>,
        mime_type: String,
        best_before: Option<DateTime<Utc>>
    ) -> Result<u64, Self::Error>;
fn load_data(&self, id: u64) -> Result<Option<PasteEntry>, Self::Error>;
fn get_file_name(&self, id: u64) -> Result<Option<String>, Self::Error>;
fn remove_data(&self, id: u64) -> Result<(), Self::Error>;
fn max_data_size(&self) -> usize; }

Interface to a database.

To store and retrieve pastes from a database we only need several functions. And we can describe them to be abstract enough to be easily used with just any kind of database, be it SQL, NoSQL or just a hash table or whatever.

Thread safety

This trait is required to be thread safe (Send + Sync) since it will be used from multiple threads.

Errors handling

An implementation must provide an Error type, which must be thread safe as well and also have a 'static lifetime.

Should some method return an error it will be logged by the web server, but will not be send to an http client, it will just receive an internal server error: 500.

Associated Types

Required Methods

Stores the data into the database and returns a unique ID that should be used later to access the data.

Return value

The function is expected to return a unique ID.

Loads data from the database.

Returns corresponding data if found, None otherwise.

Gets a file name of a paste (if any).

Removes data from the database.

Normally we don't care whether an object exists in the database or not, so an implementation doesn't have to check that fact, and usually databases are okay with attempts to remove something that doesn't exist.

Returns the maximum data size that could be handled.

This is useful, for example, for MongoDB which has a limit on a BSON document size.

Implementors