[][src]Function urban_rs::fetch_definition

pub async fn fetch_definition<'_, '_>(
    client: &'_ Client,
    word: &'_ str
) -> Result<Vec<Definition>, UrbanError>

Get a list of definitions trough a reqwest client by word.

Example

use std::io;

use tokio::runtime::Runtime;

println!("What word do you want defined?");
let mut word = String::new();
io::stdin()
    .read_line(&mut word)
    .expect("Failed to read line");

// A reqwest client is needed to use the urban API
let client = reqwest::Client::new();

// The function is async. Thus it needs an executor to be ran from inside a non-async
// function.
if let Ok(result) = Runtime::new()
    .expect("Failed to create tokio runtime")
    .block_on(urban_rs::fetch_definition(&client, &word))
{

    // the result is a vector of definitions. If it has no length then there were no words
    // found
    if result.is_empty() {
        println!("No words were found");
        return;
    }

    let first_result = &result[0];
    println!("Definition for {}:\n{}", first_result.word(), first_result.definition());

} else {
    println!("An error has occured while fetching the definition");
}

This example asks the user for a word and returns the first definition from Urban Dictionary.

Errors

The error type of the result is UrbanError. Which is an enum of three types.

  • ReqwestError
  • SerdeError
  • UnknownJsonError

ReqwestError

This error occurs when reqwest fails to fetch from the Urban API.

SerdeError

This error occurs when the json received is invalid.

UnknownJsonError

This error occurs when the json received is valid but does not have the expected structure.

Empty result

There is a fourth case. In which there are no entries in Urban Dictionary for the looked up word. In which case the Vector returned will be empty.