1use time::OffsetDateTime;
4
5use crate::{
6 errors::HackerNewsClientError,
7 items::{HackerNewsItem, HackerNewsItemType},
8 HackerNewsID,
9};
10
11#[derive(Debug)]
13pub struct HackerNewsStory {
14 pub id: HackerNewsID,
16 pub number_of_comments: u32,
18 pub comments: Vec<HackerNewsID>,
20 pub score: u32,
22 pub created_at: OffsetDateTime,
24 pub title: String,
26 pub url: String,
28 pub by: String,
30}
31
32impl TryFrom<HackerNewsItem> for HackerNewsStory {
33 type Error = HackerNewsClientError;
34
35 fn try_from(item: HackerNewsItem) -> Result<Self, Self::Error> {
36 if item.get_item_type() != HackerNewsItemType::Story {
37 return Err(HackerNewsClientError::InvalidTypeMapping(
38 item.get_item_type(),
39 ));
40 }
41
42 Ok(Self {
43 id: item.id,
44 number_of_comments: item.descendants.unwrap_or(0),
45 comments: item.kids.unwrap_or_default(),
46 score: item.score.unwrap_or(0),
47 created_at: item.created_at,
48 title: item.title.unwrap_or_default(),
49 url: item.url.unwrap_or_default(),
50 by: item.by.unwrap_or_default(),
51 })
52 }
53}