pullcaps/
models.rs

1//! The data model underlying the PushShift API.
2use chrono::serde::ts_seconds;
3use chrono::{DateTime, Utc};
4use serde::Deserialize;
5
6pub(crate) trait AsAttrs {
7    fn attrs(&self) -> &Attrs;
8}
9
10/// Common attributes between  [`Post`]'s and [`Comment`]'s.
11#[derive(Clone, Debug, Deserialize)]
12pub struct Attrs {
13    /// A unique ID identify the content.
14    pub id: String,
15
16    /// The score of this content.
17    pub score: i32,
18
19    /// A permalink to this content.
20    pub permalink: Option<String>,
21
22    /// The date at which this content was created.
23    #[serde(rename = "created_utc", with = "ts_seconds")]
24    pub date: DateTime<Utc>,
25}
26
27/// A single comment on a reddit [`Post`].
28#[derive(Clone, Debug, Deserialize)]
29pub struct Comment {
30    #[serde(flatten)]
31    pub author: Author,
32
33    #[serde(flatten)]
34    pub subreddit: SubReddit,
35
36    #[serde(flatten)]
37    pub attrs: Attrs,
38
39    pub body: String,
40    pub parent_id: String,
41}
42
43impl AsAttrs for Comment {
44    fn attrs(&self) -> &Attrs {
45        &self.attrs
46    }
47}
48
49/// A single reddit post.
50#[derive(Clone, Debug, Deserialize)]
51pub struct Post {
52    #[serde(flatten)]
53    pub author: Author,
54
55    #[serde(flatten)]
56    pub subreddit: SubReddit,
57
58    #[serde(flatten)]
59    pub attrs: Attrs,
60
61    /// URL of the linked content.
62    #[serde(rename = "url")]
63    pub content_url: String,
64
65    /// URL to the comment page for this post.
66    #[serde(rename = "full_link")]
67    pub comment_url: String,
68
69    /// The text of this post, if a self-post.
70    #[serde(rename = "selftext")]
71    pub self_text: Option<String>,
72}
73
74impl AsAttrs for Post {
75    fn attrs(&self) -> &Attrs {
76        &self.attrs
77    }
78}
79
80/// The author of a [`Post`] or [`Comment`].
81#[derive(Clone, Debug, Deserialize)]
82pub struct Author {
83    #[serde(rename = "author_fullname")]
84    pub id: Option<String>,
85    #[serde(rename = "author")]
86    pub name: String,
87}
88
89/// The subreddit associated to a [`Post`] or [`Comment`]
90#[derive(Clone, Debug, Deserialize)]
91pub struct SubReddit {
92    #[serde(rename = "subreddit_id")]
93    pub id: String,
94    #[serde(rename = "subreddit")]
95    pub name: String,
96}