Skip to main content

newsapi_rust/
models.rs

1//! Data structures representing the API responses.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6/// Represents a news article returned by the API.
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Article {
9    /// The source of the article, containing its ID and name.
10    pub source: Source,
11    /// The author of the article, if available.
12    pub author: Option<String>,
13    /// The title of the article.
14    pub title: String,
15    /// A brief description of the article, if available.
16    pub description: Option<String>,
17    /// The main content of the article, if available.
18    pub content: Option<String>,
19    /// The URL to the full article.
20    pub url: String,
21    /// The URL to the article's image, if available.
22    #[serde(rename = "urlToImage")]
23    pub url_to_image: Option<String>,
24    /// The publication date and time of the article.
25    #[serde(rename = "publishedAt")]
26    pub published_at: DateTime<Utc>,
27}
28
29/// Represents the source of a news article, containing its ID and name.
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct Source {
32    /// The unique identifier of the source, if available.
33    pub id: Option<String>,
34    /// The name of the source.
35    pub name: String,
36}
37
38/// Represents the response from the API, which can be either a success or an error.
39#[derive(Debug, Clone, Deserialize)]
40#[serde(tag = "status")]
41pub enum ApiResponse {
42    /// Indicates a successful response, containing the total number of results and a list of articles.
43    #[serde(rename = "ok")]
44    Success(SuccessResponse),
45    /// Indicates an error response, containing an error code and message.
46    #[serde(rename = "error")]
47    Error(ErrorResponse),
48    /// Represents an unknown response status that is not explicitly handled.
49    #[serde(other)]
50    Unknown,
51}
52
53/// Represents a successful response from the API, containing the total number of results and a list of articles.
54#[derive(Debug, Clone, Deserialize)]
55pub struct SuccessResponse {
56    /// The total number of results available for the query.
57    #[serde(rename = "totalResults")]
58    pub total_results: u32,
59    /// A list of articles returned by the API.
60    pub articles: Vec<Article>,
61}
62
63/// Represents an error response from the API, containing an error code and message.
64#[derive(Debug, Clone, Deserialize)]
65pub struct ErrorResponse {
66    /// The error code returned by the API, indicating the type of error that occurred.
67    pub code: String,
68    /// A human-readable message providing more details about the error.
69    pub message: String,
70}
71
72/// Represents the response from the API when requesting news sources, which can be either a success or an error.
73#[derive(Debug, Clone, Deserialize)]
74#[serde(tag = "status")]
75pub enum SourcesResponse {
76    /// Indicates a successful response, containing a list of news sources.
77    #[serde(rename = "ok")]
78    Ok { sources: Vec<SourceDetail> },
79    /// Indicates an error response, containing an error code and message.
80    #[serde(rename = "error")]
81    Err(ErrorResponse),
82    #[serde(other)]
83    /// Represents an unknown response status that is not explicitly handled.
84    Unknown,
85}
86
87/// Represents the details of a news source.
88#[derive(Debug, Clone, Deserialize)]
89pub struct SourceDetail {
90    /// The unique identifier of the news source.
91    pub id: String,
92    /// The name of the news source.
93    pub name: String,
94    /// A brief description of the news source.
95    pub description: String,
96    /// The URL of the news source's website.
97    pub url: String,
98    /// The category of news that the source primarily covers (e.g., "technology", "sports").
99    pub category: String,
100    /// The language of the news source.
101    pub language: String,
102    /// The country of the news source.
103    pub country: String,
104}