Skip to main content

newsapi_rust/
params.rs

1//! Builder structs for request parameters.
2
3/// Parameters for the `/top-headlines` endpoint.
4///
5/// See [the API documentation](https://newsapi.org/docs/endpoints/top-headlines) for more details.
6#[derive(Debug, Default, Clone)]
7pub struct TopHeadlinesParams {
8    pub(crate) country: Option<String>,
9    pub(crate) category: Option<String>,
10    pub(crate) sources: Option<String>,
11    pub(crate) q: Option<String>,
12    pub(crate) page_size: Option<u32>,
13    pub(crate) page: Option<u32>,
14}
15
16impl TopHeadlinesParams {
17    /// Creates a new `TopHeadlinesParams` with default values.
18    pub fn new() -> Self {
19        Self::default()
20    }
21
22    /// Sets the country code (e.g. "us" for United States).
23    pub fn country(mut self, country: impl Into<String>) -> Self {
24        self.country = Some(country.into());
25        self
26    }
27
28    /// Sets the category (e.g. "business", "entertainment", "health", "science", "sports", "technology").
29    pub fn category(mut self, category: impl Into<String>) -> Self {
30        self.category = Some(category.into());
31        self
32    }
33
34    /// Sets the news sources (e.g. "bbc-news", "cnn").
35    pub fn sources(mut self, sources: impl Into<String>) -> Self {
36        self.sources = Some(sources.into());
37        self
38    }
39
40    /// Sets the search query.
41    pub fn q(mut self, query: impl Into<String>) -> Self {
42        self.q = Some(query.into());
43        self
44    }
45
46    /// Sets the page number.
47    pub fn page(mut self, page: u32) -> Self {
48        self.page = Some(page.max(1));
49        self
50    }
51
52    /// Sets the page size.
53    pub fn page_size(mut self, size: u32) -> Self {
54        self.page_size = Some(size.min(100));
55        self
56    }
57}
58
59/// Parameters for the `/everything` endpoint.
60///
61/// See [the API documentation](https://newsapi.org/docs/endpoints/everything) for more details.
62#[derive(Debug, Clone, Default)]
63pub struct EverythingParams {
64    pub(crate) q: Option<String>,
65    pub(crate) q_in_title: Option<String>,
66    pub(crate) sources: Option<String>,
67    pub(crate) domains: Option<String>,
68    pub(crate) from: Option<String>,
69    pub(crate) to: Option<String>,
70    pub(crate) language: Option<String>,
71    pub(crate) sort_by: Option<String>,
72    pub(crate) page_size: Option<u32>,
73    pub(crate) page: Option<u32>,
74}
75
76impl EverythingParams {
77    /// Creates a new `EverythingParams` with default values.
78    pub fn new() -> Self {
79        Self::default()
80    }
81
82    /// Sets the search query.
83    pub fn q(mut self, query: impl Into<String>) -> Self {
84        self.q = Some(query.into());
85        self
86    }
87
88    /// Sets the search query to only look in article titles.
89    pub fn q_in_title(mut self, query: impl Into<String>) -> Self {
90        self.q_in_title = Some(query.into());
91        self
92    }
93
94    /// Sets the news sources.
95    pub fn sources(mut self, sources: impl Into<String>) -> Self {
96        self.sources = Some(sources.into());
97        self
98    }
99
100    /// Sets the start date for the search range.
101    pub fn from(mut self, from: impl Into<String>) -> Self {
102        self.from = Some(from.into());
103        self
104    }
105
106    /// Sets the end date for the search range.
107    pub fn to(mut self, to: impl Into<String>) -> Self {
108        self.to = Some(to.into());
109        self
110    }
111
112    /// Sets the domains for the search range.
113    pub fn domains(mut self, domains: impl Into<String>) -> Self {
114        self.domains = Some(domains.into());
115        self
116    }
117
118    /// Sets the language for the search results (e.g. "en" for English).
119    pub fn language(mut self, lang: impl Into<String>) -> Self {
120        self.language = Some(lang.into());
121        self
122    }
123
124    /// Sets the sorting method for the search results (e.g. "relevancy", "popularity", "publishedAt").
125    pub fn sort_by(mut self, sort: impl Into<String>) -> Self {
126        self.sort_by = Some(sort.into());
127        self
128    }
129
130    /// Sets the page size for the search results.
131    pub fn page_size(mut self, size: u32) -> Self {
132        self.page_size = Some(size.min(100));
133        self
134    }
135
136    /// Sets the page number for the search results.
137    pub fn page(mut self, page: u32) -> Self {
138        self.page = Some(page.max(1));
139        self
140    }
141}
142
143/// Parameters for the `/sources` endpoint.
144///
145/// See [the API documentation](https://newsapi.org/docs/endpoints/sources) for more details.
146#[derive(Debug, Clone, Default)]
147pub struct SourceParams {
148    pub(crate) category: Option<String>,
149    pub(crate) language: Option<String>,
150    pub(crate) country: Option<String>,
151}
152
153impl SourceParams {
154    /// Creates a new `SourceParams` with default values.
155    pub fn new() -> Self {
156        Self::default()
157    }
158
159    /// Sets the category for the news sources.
160    pub fn category(mut self, category: impl Into<String>) -> Self {
161        self.category = Some(category.into());
162        self
163    }
164
165    /// Sets the language for the news sources.
166    pub fn language(mut self, lang: impl Into<String>) -> Self {
167        self.language = Some(lang.into());
168        self
169    }
170
171    /// Sets the country for the news sources.
172    pub fn country(mut self, country: impl Into<String>) -> Self {
173        self.country = Some(country.into());
174        self
175    }
176}