fourchan-rs 0.1.1

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

use crate::post::{Attachment, Post};

/// A full thread, as returned by `/{board}/thread/{no}.json`.
#[derive(Debug, Clone, Deserialize)]
pub struct Thread {
    pub posts: Vec<Post>,
}

impl Thread {
    /// The OP post, `thread.posts[0]`, present by construction.
    pub fn op(&self) -> &Post { &self.posts[0] }

    /// All non-OP posts.
    pub fn replies(&self) -> &[Post] { &self.posts[1..] }

    /// OP number. Convenience for `op().no`.
    pub fn no(&self) -> u64 { self.op().no }

    /// Whether the thread is archived. Archived threads keep their JSON but
    /// their attachments are purged from the CDN, so image URL's 404.
    pub fn is_archived(&self) -> bool { self.op().archived }

    /// Every attachment in the thread, OP first, in post order.
    pub fn attachments(&self) -> impl Iterator<Item = &Attachment> {
        self.posts.iter().filter_map(|p| p.attachment.as_ref())
    }

    /// Attachments whose extension is a still image. See [`Attachment::is_image`].
    pub fn image_attachments(&self) -> impl Iterator<Item = &Attachment> {
        self.attachments().filter(|a| a.is_image())
    }
}