1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! Get data about the currently authenticated user.

use crate::{models, Octocrab};

/// Handler for the search API.
///
/// Created with [`Octocrab::search`].
///
/// [`Octocrab::search`]: ../struct.Octocrab.html#method.search
pub struct SearchHandler<'octo> {
    crab: &'octo Octocrab,
}

impl<'octo> SearchHandler<'octo> {
    pub(crate) fn new(crab: &'octo Octocrab) -> Self {
        Self { crab }
    }

    /// Searchs for all the repositories matching the search query.
    /// ```no_run
    ///# async fn run() -> octocrab::Result<()> {
    /// let page = octocrab::instance()
    ///     .search()
    ///     .repositories("tetris language:rust")
    ///     .sort("stars")
    ///     .order("desc")
    ///     .send()
    ///     .await?;
    ///# Ok(())
    ///# }
    /// ```
    pub fn repositories<'query>(
        self,
        query: &'query (impl AsRef<str> + ?Sized),
    ) -> QueryHandler<'octo, 'query, models::Repository> {
        QueryHandler::new(self.crab, "repositories", query.as_ref())
    }

    /// Searchs for all the commits matching the search query.
    /// ```no_run
    ///# async fn run() -> octocrab::Result<()> {
    /// let page = octocrab::instance()
    ///     .search()
    ///     .commits("hello world repo:XAMPPRocky/octocrab")
    ///     .sort("author-date")
    ///     .order("desc")
    ///     .send()
    ///     .await?;
    ///# Ok(())
    ///# }
    /// ```
    pub fn commits<'query>(
        self,
        query: &'query (impl AsRef<str> + ?Sized),
    ) -> QueryHandler<'octo, 'query, models::Commit> {
        QueryHandler::new(self.crab, "commits", query.as_ref())
    }

    /// Searchs for all users matching the search query.
    /// ```no_run
    ///# async fn run() -> octocrab::Result<()> {
    /// let page = octocrab::instance()
    ///     .search()
    ///     .users("bors type:user")
    ///     .sort("followers")
    ///     .order("desc")
    ///     .send()
    ///     .await?;
    ///# Ok(())
    ///# }
    /// ```
    pub fn users<'query>(
        self,
        query: &'query (impl AsRef<str> + ?Sized),
    ) -> QueryHandler<'octo, 'query, models::User> {
        QueryHandler::new(self.crab, "users", query.as_ref())
    }

    /// Searchs for all the issues matching the search query.
    /// ```no_run
    ///# async fn run() -> octocrab::Result<()> {
    /// let page = octocrab::instance()
    ///     .search()
    ///     .issues_and_pull_requests("GitHub Octocrab in:readme user:ferris")
    ///     .sort("comments")
    ///     .order("asc")
    ///     .send()
    ///     .await?;
    ///# Ok(())
    ///# }
    /// ```
    pub fn issues_and_pull_requests<'query>(
        self,
        query: &'query (impl AsRef<str> + ?Sized),
    ) -> QueryHandler<'octo, 'query, models::Issue> {
        QueryHandler::new(self.crab, "issues", query.as_ref())
    }
}

#[derive(Clone, Debug)]
pub enum ContentType {
    TextMatch,
    Default,
}

impl Default for ContentType {
    fn default() -> Self {
        Self::Default
    }
}

/// A handler for handling search queries to GitHub.
#[derive(Clone, Debug, serde::Serialize)]
pub struct QueryHandler<'octo, 'query, T> {
    #[serde(skip)]
    return_type: std::marker::PhantomData<T>,
    #[serde(skip)]
    crab: &'octo Octocrab,
    #[serde(skip)]
    route: &'static str,
    #[serde(skip)]
    content_type: ContentType,
    #[serde(rename = "q")]
    query: &'query str,
    per_page: Option<u8>,
    page: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    sort: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    order: Option<String>,
}

impl<'octo, 'query, T> QueryHandler<'octo, 'query, T> {
    pub(crate) fn new(crab: &'octo Octocrab, route: &'static str, query: &'query str) -> Self {
        Self {
            content_type: ContentType::Default,
            crab,
            order: None,
            page: None,
            per_page: None,
            query,
            return_type: std::marker::PhantomData,
            route,
            sort: None,
        }
    }

    /// Sets the `sort` parameter for the query. The exact parameters for this
    /// method will vary based on what is being searched.
    pub fn sort<S: Into<String>>(mut self, sort: impl Into<Option<S>>) -> Self {
        self.sort = sort.into().map(S::into);
        self
    }

    /// Sets the `order` parameter for the query. The exact parameters for this
    /// method will vary based on what is being searched.
    pub fn order<S: Into<String>>(mut self, order: impl Into<Option<S>>) -> Self {
        self.order = order.into().map(S::into);
        self
    }

    /// Results per page (max 100).
    pub fn per_page(mut self, per_page: impl Into<u8>) -> Self {
        self.per_page = Some(per_page.into());
        self
    }

    /// Page number of the results to fetch.
    pub fn page(mut self, page: impl Into<u32>) -> Self {
        self.page = Some(page.into());
        self
    }

}

impl<'octo, 'query, T: serde::de::DeserializeOwned> QueryHandler<'octo, 'query, T> {
    /// Send the actual request.
    pub async fn send(self) -> crate::Result<crate::Page<T>> {
        self.crab
            .get(&format!("/search/{}", self.route), Some(&self))
            .await
    }
}