[][src]Struct shorty::Shortener

pub struct Shortener { /* fields omitted */ }

Shortener is the struct exposing methods lookup and shorten.

lookup attempts to resolve an ID to a URL. If no URL is found or an error occurs, it returns None, otherwise it returns Some(url).

shorten takes an optional API key and a URL to shorten. If the API key is present, it will validate it and shorten the URL only if validation passes. Otherwise, it will just shorten the URL.

Shortener interacts with a RedisFacade, which makes it easier to work with the redis crate and simplifies testing.

Implementations

impl Shortener[src]

pub fn new(
    id_length: usize,
    id_alphabet: Vec<char>,
    id_generation_max_attempts: u8,
    redis: RedisFacade,
    rate_limit_period: usize,
    rate_limit: i64
) -> Shortener
[src]

Creates a new Shortener

id_length is the length of the generated ID.

id_alphabet is the alphabet used in the ID: a decent one is a-zA-Z0-9 as each entry has 62 possible values and is ASCII

id_generation_max_attempts is the number of attempts to generate an unique ID when a conflict is detected.

redis is a RedisFacade instance.

rate_limit_period is the amount of seconds during which calls to shorten will be counted.

rate_limit is the max number of calls that can be made to shorten in a period.

pub fn lookup(&self, id: &str) -> Option<String>[src]

Looks up a URL by the given ID. If no URL is found or an error occurs, it returns None, otherwise it returns Some(url).

pub fn shorten(
    &self,
    api_key: &Option<&str>,
    host: Option<&str>,
    url: &str
) -> Result<ShortenerResult, ShortenerError>
[src]

Shortens an URL, returning a ShortenerResult holding the provided URL and the generated ID.

If the optional API key is present, it will validate it and shorten the URL only if validation passes.

If the optional host is present, it will ensure that the url to shorten is not a url from the same host that's running shorty (which would create a link loop)

Otherwise, it will just shorten the URL.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.