rust_commit_tracker/services/
scraper.rs

1use crate::models::{CommitInfo, CommitsResponse};
2use std::error::Error;
3
4pub struct CommitScraper {
5    client: reqwest::Client,
6}
7
8#[derive(Debug)]
9pub struct CommitResult {
10    pub commit: CommitInfo,
11    pub total_commits: u32,
12    pub position: u32, // Position in the list (1 = latest)
13}
14
15impl CommitScraper {
16    pub fn new() -> Self {
17        Self {
18            client: reqwest::Client::new(),
19        }
20    }
21
22    pub async fn fetch_latest_commit(&self, url: &str) -> Result<CommitResult, Box<dyn Error>> {
23        let response = self.client.get(url).send().await?;
24        let commits_response: CommitsResponse = response.json().await?;
25
26        let commit = commits_response
27            .results
28            .into_iter()
29            .next()
30            .ok_or("No commits found in response")?;
31
32        Ok(CommitResult {
33            commit,
34            total_commits: commits_response.total,
35            position: 1, // Latest commit is always position 1
36        })
37    }
38}
39
40impl Default for CommitScraper {
41    fn default() -> Self {
42        Self::new()
43    }
44}