bddap_aoc/
input_getter.rs

1use super::api;
2use super::persist::{Config, Profile};
3
4pub struct Getter {
5    profile: Profile,
6}
7
8impl Getter {
9    pub fn load() -> anyhow::Result<Self> {
10        let config = Config::load()?;
11        let profile = config.get_default_profile();
12        Ok(Self { profile })
13    }
14
15    pub fn get_input(&self, year: usize, day: usize) -> Result<String, api::Error> {
16        // try getting from cache
17        if let Some(input) = self.profile.get_cached(year, day)? {
18            return Ok(input);
19        }
20
21        // otherwise, get from api
22        let session_token = self.profile.get_session_token().ok_or(anyhow::anyhow!(
23            "No session token found. Please run the login command."
24        ))?;
25        let ret = api::get_input(session_token, year, day)?;
26
27        // cache it
28        self.profile.set_cached(year, day, &ret)?;
29
30        Ok(ret)
31    }
32}