1pub mod response;
2
3use crate::auth::Authenticator;
4use crate::comments::CommentRetriever;
5use crate::submission::response::SubmissionsResponse;
6use crate::utils::options::{CommentOption, FeedOption};
7use crate::Client;
8use async_trait::async_trait;
9
10use crate::error::Error;
11use crate::responses::listing::{GenericListing, ListingArray};
12
13pub trait SubmissionType<'a>: Sized + Sync + Send {
14 fn get_permalink(&self) -> &String;
15
16 fn to_submission<A: Authenticator>(&'a self, me: &'a Client<A>) -> Submission<'a, A, Self>
17 where
18 Self: SubmissionType<'a>,
19 {
20 Submission {
21 submission: self,
22 me,
23 }
24 }
25}
26
27impl<'a> SubmissionType<'a> for String {
28 fn get_permalink(&self) -> &String {
29 self
30 }
31}
32
33pub struct Submission<'a, A: Authenticator, T: SubmissionType<'a>> {
34 pub submission: &'a T,
35 pub(crate) me: &'a Client<A>,
36}
37
38#[async_trait(?Send)]
39impl<'a, A: Authenticator, T: SubmissionType<'a>> CommentRetriever for Submission<'a, A, T> {
40 async fn get_comments(&self, sort: Option<CommentOption>) -> Result<ListingArray, Error> {
41 let mut path = self.submission.get_permalink().to_string();
42 if let Some(options) = sort {
43 options.extend(&mut path)
44 }
45 return self.me.get_json::<ListingArray>(&path, false, false).await;
46 }
47}
48
49pub type Submissions<'a, A, T> = GenericListing<Submission<'a, A, T>>;
50
51#[async_trait(?Send)]
52pub trait SubmissionRetriever {
53 async fn get_submissions<T: Into<String> + std::marker::Send>(
54 &self,
55 sort: T,
56 feed_options: Option<FeedOption>,
57 ) -> Result<SubmissionsResponse, Error>;
58
59 async fn hot(&self, feed_options: Option<FeedOption>) -> Result<SubmissionsResponse, Error> {
60 return self.get_submissions("hot", feed_options).await;
61 }
62}