ergast-rs 0.3.2

An async client for getting Formula 1 schedules, qualifying, and race results powered by the Ergast API
Documentation
use async_trait::async_trait;
use eyre::{eyre, Result};

use crate::apis::{race_table::RaceTable, request::Request, response::Response};
use crate::Ergast;

/// This is a mock version of the `Ergast` client which will not perform network requests.
///
/// Instead it will use pre-saved responses from the Ergast API to return mock data.
///
/// For example:
///
/// ```
/// # use eyre::Result;
/// use ergast_rs::{Ergast, ErgastMock};
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<()> {
/// let ergast = ErgastMock::new()?;
/// let schedule = ergast.schedule(None).await?;
/// #    Ok(())
/// # }
/// ```
pub struct ErgastMock {}

#[async_trait]
impl Ergast for ErgastMock {
    async fn query(&self, _request: Request) -> Result<RaceTable> {
        let json = include_str!("./fixtures/api.f1.2022.2.qualifying.json");
        let resp: Response = serde_json::from_str(json)?;
        resp.data.race_table.ok_or(eyre!("Missing race table"))
    }

    async fn schedule(&self, _season: Option<u32>) -> Result<RaceTable> {
        let json = include_str!("./fixtures/api.f1.current.json");
        let resp: Response = serde_json::from_str(json)?;
        resp.data.race_table.ok_or(eyre!("Missing race table"))
    }

    async fn race_results(&self, _season: Option<u32>, _round: Option<u32>) -> Result<RaceTable> {
        let json = include_str!("./fixtures/api.f1.2020.16.results.json");
        let resp: Response = serde_json::from_str(json)?;
        resp.data.race_table.ok_or(eyre!("Missing race table"))
    }

    async fn sprint_results(&self, _season: Option<u32>, _round: Option<u32>) -> Result<RaceTable> {
        let json = include_str!("./fixtures/api.f1.2021.10.sprint.json");
        let resp: Response = serde_json::from_str(json)?;
        resp.data.race_table.ok_or(eyre!("Missing race table"))
    }

    async fn qualifying_results(
        &self,
        _season: Option<u32>,
        _round: Option<u32>,
    ) -> Result<RaceTable> {
        let json = include_str!("./fixtures/api.f1.2022.2.qualifying.json");
        let resp: Response = serde_json::from_str(json)?;
        resp.data.race_table.ok_or(eyre!("Missing race table"))
    }
}

impl ErgastMock {
    pub fn new() -> Result<Self> {
        Ok(Self {})
    }
}