shared/domain/
search.rs

1//! types for searching
2
3use macros::make_path_parts;
4use serde::{Deserialize, Serialize};
5use strum_macros::EnumIter;
6
7make_path_parts!(CreateSearchKeyPath => "/v1/search/key");
8
9/// Represents the response given when an api key for algolia is requested.
10#[derive(Serialize, Deserialize, Debug, Clone)]
11pub struct CreateSearchKeyResponse {
12    /// The key to be used with algolia
13    pub key: String,
14    // todo: add the expire time
15}
16
17make_path_parts!(WebImageSearchPath => "/v1/search/web/image");
18
19/// Search for images via the given query string.
20#[derive(Serialize, Deserialize, Clone, Debug, Default)]
21#[serde(rename_all = "camelCase")]
22pub struct WebImageSearchQuery {
23    /// The query string.
24    #[serde(default)]
25    pub q: String,
26
27    /// Image type string
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub image_type: Option<ImageType>,
30}
31
32/// Represents different types of images
33#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, EnumIter)]
34#[serde(rename_all = "camelCase")]
35pub enum ImageType {
36    /// Animated Gif Images
37    Clipart = 0,
38    /// Clip art images
39    AnimatedGif = 1,
40    /// Photographs (excluding line drawings, animated gifs, and clip art)
41    Photo = 2,
42    /// Line drawings
43    Line = 3,
44    /// Images with transparent backgrounds
45    Transparent = 4,
46}
47
48/// Image types used in query string
49impl ImageType {
50    ///
51    #[must_use]
52    pub fn to_str(self) -> &'static str {
53        match self {
54            Self::Clipart => "Clipart",
55            Self::AnimatedGif => "AnimatedGif",
56            Self::Photo => "Photo",
57            Self::Line => "Line",
58            Self::Transparent => "Transparent",
59        }
60    }
61}
62
63/// A single image as returned from the web
64#[derive(Serialize, Deserialize, Debug, Clone)]
65pub struct WebImageSearchItem {
66    /// A URL to the thumbnail of the image.
67    pub thumbnail_url: url::Url,
68    /// A URL to the original image.
69    pub url: url::Url,
70}
71
72/// Response for successful search.
73/// TODO: support pagation
74#[derive(Serialize, Deserialize, Debug)]
75pub struct WebImageSearchResponse {
76    /// the images returned.
77    pub images: Vec<WebImageSearchItem>,
78}