From df8bafb8c53676ee99fa818b99f2f0270c90bc1e Mon Sep 17 00:00:00 2001 From: Carlos Iriarte Date: Sun, 27 Dec 2020 22:18:15 -0800 Subject: [PATCH] fix: add json serialization --- Cargo.lock | 5 +++++ Cargo.toml | 4 +++- src/main.rs | 12 ++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e55d6ff..2cf3d04 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -524,6 +524,8 @@ dependencies = [ "kuchiki", "regex", "reqwest", + "serde", + "serde_json", ] [[package]] @@ -1006,6 +1008,9 @@ name = "serde" version = "1.0.118" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800" +dependencies = [ + "serde_derive", +] [[package]] name = "serde_derive" diff --git a/Cargo.toml b/Cargo.toml index 9127aeb..9d0e3be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,6 @@ edition = "2018" [dependencies] reqwest= { version = "0.10.0-alpha.2", features = ["blocking"] } kuchiki= "*" -regex = { version = "1.4.2" } \ No newline at end of file +regex = { version = "1.4.2" } +serde = { version = "1.0", features = ["derive"] } +serde_json = { version = "1.0" } diff --git a/src/main.rs b/src/main.rs index ee145cb..cc56762 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,9 @@ extern crate kuchiki; use kuchiki::traits::*; use regex::Regex; -#[derive(Debug)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug)] struct Person { name: String, dob: i32, @@ -24,6 +26,7 @@ fn main() -> std::result::Result<(), Box> { res.copy_to(&mut buf)?; let s: String = String::from_utf8(buf)?; + let mut presidents: Vec = vec![]; let table_selector = ".wikitable:not(:last-of-type)"; let document = kuchiki::parse_html().one(s); for table_match in document.select(table_selector).unwrap() { @@ -90,9 +93,14 @@ fn main() -> std::result::Result<(), Box> { img: format!("https:{}", img), }; - println!("{:?}", person); + presidents.push(person); } } + let j = serde_json::to_string(&presidents)?; + + // Print, write to a file, or send to an HTTP server. + println!("{}", j); + Ok(()) } -- 2.45.2