fourchan-rs 0.1.0

Async 4chan JSON API client and type bindings
Documentation
use serde::Deserialize;

use crate::post::Post;

/// `catalog.json` - a flat list of pages, each holding many threads with their OP 
/// plus up to five `last_replies`.
#[derive(Debug, Clone, Deserialize)]
#[serde(transparent)]
pub struct Catalog(pub Vec<CatalogPage>);

#[derive(Debug, Clone, Deserialize)]
pub struct CatalogPage {
    pub page: u32,
    pub threads: Vec<CatalogThread>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct CatalogThread {
    /// The OP post (with metadata).
    #[serde(flatten)]
    pub op: Post,

    /// Up to five most recent reply previews.
    #[serde(default)]
    pub last_replies: Vec<Post>,
}

impl Catalog {
    pub fn pages(&self) -> &[CatalogPage] { &self.0 }

    /// Iterator over every thread in every page, in order.
    pub fn threads(&self) -> impl Iterator<Item = &CatalogThread> {
        self.0.iter().flat_map(|p| p.threads.iter())
    }
}

impl CatalogThread {
    /// OP number.
    pub fn no(&self) -> u64 { self.op.no }
}