Skip to main content

chan/
catalog.rs

1use serde::Deserialize;
2
3use crate::post::Post;
4
5/// `catalog.json` - a flat list of pages, each holding many threads with their OP 
6/// plus up to five `last_replies`.
7#[derive(Debug, Clone, Deserialize)]
8#[serde(transparent)]
9pub struct Catalog(pub Vec<CatalogPage>);
10
11#[derive(Debug, Clone, Deserialize)]
12pub struct CatalogPage {
13    pub page: u32,
14    pub threads: Vec<CatalogThread>,
15}
16
17#[derive(Debug, Clone, Deserialize)]
18pub struct CatalogThread {
19    /// The OP post (with metadata).
20    #[serde(flatten)]
21    pub op: Post,
22
23    /// Up to five most recent reply previews.
24    #[serde(default)]
25    pub last_replies: Vec<Post>,
26}
27
28impl Catalog {
29    pub fn pages(&self) -> &[CatalogPage] { &self.0 }
30
31    /// Iterator over every thread in every page, in order.
32    pub fn threads(&self) -> impl Iterator<Item = &CatalogThread> {
33        self.0.iter().flat_map(|p| p.threads.iter())
34    }
35}
36
37impl CatalogThread {
38    /// OP number.
39    pub fn no(&self) -> u64 { self.op.no }
40}