use serde::Deserialize;
use serde::Serialize;
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Posts(Vec<Post>);
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Post {
pub preview_url: String,
pub sample_url: String,
pub file_url: String,
pub hash: String,
pub width: i64,
pub height: i64,
pub id: i64,
pub image: String,
pub owner: String,
pub parent_id: i64,
pub sample: bool,
pub sample_height: i64,
pub sample_width: i64,
pub score: i64,
pub tags: String,
pub has_notes: bool,
}
#[derive(Debug)]
pub struct MiniPost<'a> {
pub id: u64,
pub preview_url: &'a str,
pub sample_url: &'a str,
pub file_url: &'a str,
pub width: u64,
pub height: u64,
pub sample_height: u64,
pub sample_width: u64,
pub tags: Vec<&'a str>,
}
impl<'a> Into<MiniPost<'a>> for &'a Post {
fn into(self) -> MiniPost<'a> {
MiniPost {
id: self.id as u64,
preview_url: &self.preview_url,
sample_url: &self.sample_url,
file_url: &self.file_url,
width: self.width as u64,
height: self.height as u64,
sample_height: self.sample_height as u64,
sample_width: self.sample_width as u64,
tags: self.tags.split(' ').collect(),
}
}
}
impl Posts {
pub fn get_p_urls(&self) -> Vec<&str> {
let mut urls: Vec<&str> = vec![];
self.0.iter().for_each(|x| urls.push(&x.preview_url));
urls
}
pub fn get_s_urls(&self) -> Vec<&str> {
let mut urls: Vec<&str> = vec![];
self.0.iter().for_each(|x| urls.push(&x.sample_url));
urls
}
pub fn get_f_urls(&self) -> Vec<&str> {
let mut urls: Vec<&str> = vec![];
self.0.iter().for_each(|x| urls.push(&x.file_url));
urls
}
pub fn get_urls_ext(&self) -> Vec<MiniPost> {
self.0.iter().map(|x| x.into()).collect()
}
pub fn get_url_ext(&self) -> Option<MiniPost> {
self.0.first().map(|x| x.into())
}
pub fn get_p_url(&self) -> Option<&str> {
self.0.first().map(|x| x.preview_url.as_str())
}
pub fn get_s_url(&self) -> Option<&str> {
self.0.first().map(|x| x.sample_url.as_str())
}
pub fn get_f_url(&self) -> Option<&str> {
self.0.first().map(|x| x.file_url.as_str())
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
#[tokio::test]
async fn create_make_link_search() {
let binding: Posts = Params::init().make_link().search().await.unwrap();
let result = binding.get_f_urls();
println!("{:#?}", result)
}
#[tokio::test]
async fn create_make_link_search_with_id() {
let binding: Posts = Params::init()
.id(10542274)
.make_link()
.search()
.await
.unwrap();
let result = binding.get_f_urls();
println!("{:#?}", result)
}
}