use crate::error::Error;
use crate::model::{comment::Comment, link::Link, misc::Fullname, thing::Thing};
use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Listing {
pub modhash: Option<String>,
pub dist: Option<u64>,
pub children: Vec<Thing>,
pub after: Option<Fullname>,
pub before: Option<Fullname>,
#[serde(default)]
pub limit: Option<u64>,
#[serde(default)]
pub count: Option<u64>,
#[serde(default)]
pub show: Option<String>,
}
impl Default for Listing {
fn default() -> Self {
Listing {
modhash: None,
dist: None,
children: Vec::new(),
after: None,
before: None,
limit: None,
count: None,
show: None,
}
}
}
impl TryFrom<Listing> for Vec<Comment> {
type Error = Error;
fn try_from(value: Listing) -> Result<Self, Self::Error> {
let comments: Result<Vec<Comment>, Error> =
value.children.into_iter().map(Thing::try_into).collect();
comments
}
}
impl TryFrom<Listing> for Vec<Link> {
type Error = Error;
fn try_from(value: Listing) -> Result<Self, Self::Error> {
let comments: Result<Vec<Link>, Error> =
value.children.into_iter().map(Thing::try_into).collect();
comments
}
}