use time::OffsetDateTime;
use crate::{
errors::HackerNewsClientError,
items::{HackerNewsItem, HackerNewsItemType},
HackerNewsID,
};
#[derive(Debug)]
pub struct HackerNewsPollOption {
pub id: HackerNewsID,
pub poll: u32,
pub score: u32,
pub created_at: OffsetDateTime,
pub text: String,
pub by: String,
}
impl TryFrom<HackerNewsItem> for HackerNewsPollOption {
type Error = HackerNewsClientError;
fn try_from(item: HackerNewsItem) -> Result<Self, Self::Error> {
if item.get_item_type() != HackerNewsItemType::PollOption {
return Err(HackerNewsClientError::InvalidTypeMapping(
item.get_item_type(),
));
}
if item.poll.is_none() {
return Err(HackerNewsClientError::AssociatedParentNotFound(item.id));
}
Ok(Self {
id: item.id,
created_at: item.created_at,
text: item.text.unwrap_or_default(),
by: item.by.unwrap_or_default(),
score: item.score.unwrap_or(0),
poll: item.poll.unwrap(),
})
}
}