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    /// All images
47    All = 5,
48    /// Illustration images
49    Illustration = 6,
50    /// Vector images
51    Vector = 7,
52}
53
54/// Image types used in query string
55impl ImageType {
56    ///
57    #[must_use]
58    pub fn to_str(self) -> &'static str {
59        match self {
60            Self::Clipart => "Clipart",
61            Self::AnimatedGif => "AnimatedGif",
62            Self::Photo => "Photo",
63            Self::Line => "Line",
64            Self::Transparent => "Transparent",
65            Self::All => "All",
66            Self::Illustration => "Illustration",
67            Self::Vector => "Vector",
68        }
69    }
70
71    /// Returns the types that are enabled for the web image search.
72    pub fn enabled_types() -> Vec<ImageType> {
73        vec![Self::All, Self::Photo, Self::Illustration, Self::Vector]
74    }
75}
76
77/// A single image as returned from the web
78#[derive(Serialize, Deserialize, Debug, Clone)]
79pub struct WebImageSearchItem {
80    /// A URL to the thumbnail of the image.
81    pub thumbnail_url: url::Url,
82    /// A URL to the original image.
83    pub url: url::Url,
84}
85
86/// Response for successful search.
87/// TODO: support pagation
88#[derive(Serialize, Deserialize, Debug)]
89pub struct WebImageSearchResponse {
90    /// the images returned.
91    pub images: Vec<WebImageSearchItem>,
92}