wallhaven_rs/models/request/
search.rs

1mod query;
2
3use bon::Builder;
4pub use query::*;
5use serde::Serialize;
6use serde_with::{
7    DisplayFromStr, StringWithSeparator, formats::SpaceSeparator, serde_as, skip_serializing_none,
8};
9
10use crate::{
11    AspectRatio, Categories, Color, Purities, Resolution, SortingOrder, SortingType, ToplistRange,
12};
13
14/// The search request object.
15#[serde_as]
16#[derive(Serialize, Builder, Clone, Debug)]
17#[builder(finish_fn(vis = "", name = build_internal))]
18#[skip_serializing_none]
19pub struct SearchRequest {
20    /// A list of items of which the query is composed of
21    ///
22    /// See [`SearchQueryItem`] for more information
23    #[serde_as(as = "Option<StringWithSeparator::<SpaceSeparator, SearchQueryItem>>")]
24    #[serde(rename = "q")]
25    query: Option<Vec<SearchQueryItem>>,
26    /// Turn categories on or off
27    categories: Option<Categories>,
28    /// Turn categories on or off, NSFW requires a valid api key
29    purity: Option<Purities>,
30    /// The sorting type.
31    sorting: Option<SortingType>,
32    /// The sorting order
33    order: Option<SortingOrder>,
34    /// The toplist range.
35    ///
36    /// Sorting MUST be set to [`SortingType::Toplist`]
37    #[serde(rename = "topRange")]
38    toplist_range: Option<ToplistRange>,
39    #[serde(rename = "atleast")]
40    /// Minimum resolution allowed
41    at_least: Option<Resolution>,
42    /// List of exact wallpaper resolutions
43    resolutions: Option<Vec<Resolution>>,
44    /// List of aspect ratios
45    ratios: Option<Vec<AspectRatio>>,
46    /// Search by color
47    #[serde(rename = "colors")]
48    #[serde_as(as = "Option<DisplayFromStr>")]
49    color: Option<Color>,
50    /// Pagination, from 1->inf
51    ///
52    /// (Not actually infinite)
53    page: Option<u64>,
54    /// Optional seed for random results
55    seed: Option<String>,
56}
57
58impl<S: search_request_builder::IsComplete> SearchRequestBuilder<S> {
59    /// Builds the request.
60    ///
61    /// # Errors
62    ///
63    /// Throws an error if `toplist_range` is set and `sorting` isn't `SortingType::Toplist`
64    pub fn build(self) -> Result<SearchRequest, String> {
65        let req = self.build_internal();
66
67        match (req.toplist_range, req.sorting) {
68            (Some(_), Some(SortingType::Toplist)) => Ok(req),
69            (Some(_), _) => Err("toplist_range is set and sorting != Toplist".to_string()),
70            _ => Ok(req),
71        }
72    }
73}