Skip to main content

tranquility/model/
misc.rs

1//! Various enums that you can match on.
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6/// The quarantine permissions.
7pub struct QuarantinePermissions {
8    /// If styles are allowed.
9    pub styles: bool,
10    /// If sharing is allowed.
11    pub sharing: bool,
12    /// If subreddit images are allowed.
13    pub sr_images: bool,
14    /// If the subscriber count is visible.
15    pub subscriber_count: bool,
16    /// If media is allowed.
17    pub media: bool,
18    /// If polls are allowed.
19    pub polls: bool,
20    /// If videos are allowed.
21    pub videos: bool,
22    /// If images are allowed.
23    pub images: bool,
24    /// If crossposts are allowed.
25    pub crossposts: bool,
26}
27
28#[serde(rename_all = "lowercase")]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30/// The type of a subreddit.
31pub enum SubredditType {
32    /// Anyone can post to this subreddit.
33    Public,
34    /// Only certain users can post to this subreddit.
35    Private,
36}
37
38#[serde(rename_all = "lowercase")]
39#[derive(Debug, Clone, Serialize, Deserialize)]
40/// The type of a submission.
41pub enum CommentSort {
42    /// Absolute (total karma) ranking.
43    Top,
44    /// Relative (percentage-based) ranking.
45    Best,
46    /// Prioritize controversial comments.
47    Controversial,
48    /// Newest comments.
49    New,
50}
51
52#[serde(rename_all = "lowercase")]
53#[derive(Debug, Clone, Serialize, Deserialize)]
54/// The allowed types of submissions in a Subreddit.
55pub enum SubredditSubmissionType {
56    /// All submissions allowed.
57    Any,
58    /// Only link submissions allowed.
59    Link,
60    /// Only text posts allowed.
61    Text,
62}
63
64#[serde(rename_all = "lowercase")]
65#[derive(Debug, Clone, Serialize, Deserialize)]
66/// The type of a submission.
67pub enum SubmissionType {
68    /// URL link.
69    Link,
70    /// Self-post.
71    Text,
72}
73
74#[serde(rename_all = "lowercase")]
75#[derive(Debug, Clone, Serialize, Deserialize)]
76/// A way to sort links..
77pub enum LinkSort {
78    /// Posts made in the past hour.
79    Hour,
80    /// Posts made in the past day.
81    Day,
82    /// Posts made in the past week.
83    Week,
84    /// Posts made in the past month
85    Month,
86    /// Posts made in the past year.
87    Year,
88    /// All posts.
89    All,
90}
91
92#[serde(rename_all = "lowercase")]
93#[derive(Debug, Clone, Serialize, Deserialize)]
94/// The type of an award.
95pub enum AwardType {
96    /// Can be used anywhere.
97    Global,
98    /// Belonging to a subreddit.
99    Community,
100}
101
102#[serde(rename_all = "UPPERCASE")]
103#[derive(Debug, Clone, Serialize, Deserialize)]
104/// The subtype of an award.
105pub enum AwardSubtype {
106    /// Can be used anywhere.
107    Global,
108    /// Belonging to a subreddit.
109    Community,
110    /// Premium.
111    Premium,
112}
113
114/// Parameters for a GET query, a key-value tuple of Strings.
115#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct Params(Vec<(String, String)>);
117
118impl Params {
119    /// Creates a new Params struct.
120    pub fn new() -> Self {
121        Self(Vec::new())
122    }
123
124    /// Adds a parameter.
125    pub fn add(mut self, key: &str, value: &str) -> Self {
126        self.0.push((key.into(), value.into()));
127        self
128    }
129}
130
131impl Default for Params {
132    fn default() -> Self {
133        Self(Vec::new())
134    }
135}
136
137/// Fullname is the reddit unique ID for a thing, including the type prefix.
138#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
139pub struct Fullname(String);
140
141impl AsRef<str> for Fullname {
142    fn as_ref(&self) -> &str {
143        &self.0
144    }
145}
146
147impl Fullname {
148    /// Gets the ID36 portion of a Fullname.
149    pub fn name(&self) -> String {
150        let parts: Vec<&str> = self.as_ref().split('_').collect();
151        parts[1].to_owned()
152    }
153
154    /// Gets the prefix of a Fullname (t1, t2, ...)
155    pub fn prefix(&self) -> String {
156        let parts: Vec<&str> = self.as_ref().split('_').collect();
157        parts[0].to_owned()
158    }
159}