use time::OffsetDateTime;
use crate::{
errors::HackerNewsClientError,
items::{HackerNewsItem, HackerNewsItemType},
HackerNewsID,
};
#[derive(Debug)]
pub struct HackerNewsStory {
pub id: HackerNewsID,
pub number_of_comments: u32,
pub comments: Vec<HackerNewsID>,
pub score: u32,
pub created_at: OffsetDateTime,
pub title: String,
pub url: String,
pub by: String,
}
impl TryFrom<HackerNewsItem> for HackerNewsStory {
type Error = HackerNewsClientError;
fn try_from(item: HackerNewsItem) -> Result<Self, Self::Error> {
if item.get_item_type() != HackerNewsItemType::Story {
return Err(HackerNewsClientError::InvalidTypeMapping(
item.get_item_type(),
));
}
Ok(Self {
id: item.id,
number_of_comments: item.descendants.unwrap_or(0),
comments: item.kids.unwrap_or_default(),
score: item.score.unwrap_or(0),
created_at: item.created_at,
title: item.title.unwrap_or_default(),
url: item.url.unwrap_or_default(),
by: item.by.unwrap_or_default(),
})
}
}