[][src]Struct rusty_xkcd::Comic

pub struct Comic {
    pub title: String,
    pub url: String,
    pub img_url: String,
    pub alt_text: String,
    pub number: i32,
    pub date: Date<Utc>,
}

The struct containing all xkcd-comic related data and methods

Usage

There are three main ways to get a fully primed Comic

Get the comic by number

let comic = Comic::get_comic(100).unwrap(); // Get a comic by it's number

However, getting a comic does have limits, as requesting a comic that does not exist will throw an InvalidNumber error

let comic = Comic::get_comic(-1).unwrap(); // Too low!
let comic = Comic::get_comic(999_999).unwrap(); // Too high!

Get the latest comic

let comic = Comic::get_latest_comic().unwrap(); // Get the latest comic

Get a random comic

let comic = Comic::get_random_comic().unwrap(); // Get a random comic

Data

Data from the Comic struct can be extracted in one of two ways:

By 'dotting' the instance

let comic_number = Comic::get_random_comic().unwrap().number; // Get the comic's number

Or by using one of the data methods

let comic_number = Comic::get_random_comic().unwrap().get_number(); // Get the latest comic's number

Errors

There are two errors that can be thrown while acquiring a comic

Invalid Number

An invalid number error comes from your software or the end user requesting an xkcd comic with a number that is either less than or equal to zero or greater than the newest xkcd comic's number. For those who speak code more fluently than english, here's a snippet:

if input_number <= 0 || input_number > latest_comic_number {
    throw_error();
}

Request Error

A request error can happen for any number of reasons, but all are related to some sort of failure in querying the xkcd api

Fields

title: String

Title of the comic

url: String

Url of the comic https://xkcd.com/{comic_number}

img_url: String

Image Url of tht comic https://imgs.xkcd.com/comics/{image_name}.png

alt_text: String

Alt text or tooltip text of the comic

number: i32

Number of the comic

date: Date<Utc>

Date that the comic was published

Methods

impl Comic[src]

pub fn get_comic(comic_num: i32) -> Result<Comic, Error>[src]

Fetches the chosen xkcd comic via i32

Usage

let comic: Comic = Comic::get_comic(100).unwrap(); // Get xkcd comic number 100
println!("{:?}", comic);
println!("{}", comic.number);
println!("{}", comic.url);

pub fn get_latest_comic() -> Result<Comic, Error>[src]

Fetches the latest xkcd comic.

Usage

let comic: Comic = Comic::get_latest_comic().unwrap(); // Get the latest xkcd comic
println!("{:?}", comic);
println!("{}", comic.number);
println!("{}", comic.url);

pub fn get_random_comic() -> Result<Comic, Error>[src]

Fetches a random xkcd comic

Usage

let comic: Comic = Comic::get_random_comic().unwrap(); // Get a random xkcd comic
println!("{:?}", comic);
println!("{}", comic.number);
println!("{}", comic.url);

pub fn get_title(&self) -> String[src]

Fetches the current comic's title

pub fn get_url(&self) -> String[src]

Fetches the current comic's url

pub fn get_img_url(&self) -> String[src]

Fetches the current comic's image url

pub fn get_alt_text(&self) -> String[src]

Fetches the current comic's alt/tooltip text

pub fn get_number(&self) -> i32[src]

Fetches the current comic's number

pub fn get_date(&self) -> Date<Utc>[src]

Fetches the current comic's date

Trait Implementations

impl Debug for Comic[src]

Auto Trait Implementations

impl Send for Comic

impl Sync for Comic

Blanket Implementations

impl<T> From for T[src]

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

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

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

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

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

The type returned in the event of a conversion error.

impl<T> Erased for T