Last active
February 9, 2021 19:28
-
-
Save LuisValdesZero/6f1dfaa2d8ce1f91dcd33ac17245d893 to your computer and use it in GitHub Desktop.
Ownership in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "cli-0" | |
version = "0.1.0" | |
authors = ["Luis Valdes <[email protected]>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
structopt = "0.3.13" | |
anyhow = "1.0" | |
indicatif= "0.15.0" | |
log = "0.4.11" | |
env_logger = "0.8.2" | |
reqwest = { version = "0.11", features = ["json"] } | |
tokio = { version = "1", features = ["full"] } | |
serde = { version = "1.0.118", features = ["derive"] } | |
serde_json = "1.0" | |
jq-rs = "0.4.1" | |
snailquote = "0.3.0" | |
cli-table = "0.4" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use cli_table::{format::Justify, print_stdout, Cell, Style, Table}; | |
use serde::{Serialize, Deserialize}; | |
#[derive(Debug, PartialEq, Serialize, Deserialize)] | |
struct Person { | |
name: HashMap<String, String>, | |
email: String | |
} | |
#[derive(Debug, PartialEq, Serialize, Deserialize)] | |
struct Results { | |
results: Vec<Person> | |
} | |
#[derive(Table)] | |
struct PeopleTable<'a> { | |
#[table(name = "Name", justify = "Justify::Right")] | |
name: &'a str, | |
#[table(name = "Email")] | |
email: &'a str, | |
} | |
#[tokio::main] | |
async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
let request_url = "https://randomuser.me/api/?results=10"; | |
let response = reqwest::get(request_url).await?; | |
let result: Results = response.json().await?; | |
let mut result_table: Vec<PeopleTable> = Vec::new(); | |
for person in result.results.iter() { | |
let whole_name = format!("{} {} {}", person.name.get("title").unwrap(), | |
person.name.get("first").unwrap(), | |
person.name.get("last").unwrap()); | |
let person_data = PeopleTable { | |
name: &whole_name, | |
email: &person.email | |
}; | |
result_table.push(person_data); | |
} | |
print_stdout(result_table); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment