diff --git a/src/main.rs b/src/main.rs index 060f8aa..3de9306 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,37 +5,40 @@ use std::io; use std::str::from_utf8; use std::time::Duration; +#[derive(Debug)] struct BookData { title: String, authors: Vec, thumbnail: String, description: String, } + #[allow(unused)] #[derive(Debug, Deserialize)] struct BookRoot { items: Vec, } -#[allow(unused)] +#[allow(unused, non_snake_case)] #[derive(Debug, Deserialize)] struct Item { - volumeinfo: VolumeInfo, + volumeInfo: VolumeInfo, } -#[allow(unused)] +#[allow(unused, non_snake_case)] #[derive(Debug, Deserialize)] struct VolumeInfo { title: String, authors: Option>, description: Option, - imagelinks: Option, + imageLinks: Option, } -#[allow(unused)] +#[allow(unused, non_snake_case)] #[derive(Debug, Deserialize)] struct ImageLinks { thumbnail: Option, + smallThumbnail: Option, } #[tokio::main] @@ -80,5 +83,44 @@ async fn get_book_data(isbn: String) { .json::() .await .unwrap(); - println!("{:?}", book_data); + let book_metadata: Vec = book_data.items.into_iter().map(BookData::from).collect(); + println!("{}", &book_metadata[0]); +} + +impl From for BookData { + fn from(item: Item) -> Self { + let info = item.volumeInfo; + BookData { + title: info.title, + authors: info.authors.unwrap_or_default(), + description: info.description.as_deref().unwrap_or("").to_string(), + thumbnail: info + .imageLinks + .as_ref() + .and_then(|links| links.thumbnail.as_deref()) + .unwrap_or("") + .to_string(), + } + } +} + +impl std::fmt::Display for BookData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "Title: {}\nAuthors: {}\nDescription: {}\nThumbnail: {}", + self.title, + if self.authors.is_empty() { + "Unknown".to_string() + } else { + self.authors.join(", ") + }, + if self.description.is_empty() { + "No description available".to_string() + } else { + self.description.clone() + }, + self.thumbnail + ) + } }