repo_analyzer/
config.rs

1use anyhow::{Context, Result};
2use serde::Deserialize;
3use std::fs::File;
4use std::io::Read;
5use std::path::Path;
6
7#[derive(Debug, Deserialize)]
8pub struct Config {
9    #[serde(default)]
10    pub api_key: Option<String>,
11    #[serde(default)]
12    pub api_url: Option<String>,
13}
14
15impl Config {
16    pub fn load() -> Result<Self> {
17        // First try to load from config.json in the current directory
18        let config_path = Path::new("config.json");
19        if config_path.exists() {
20            let mut file = File::open(config_path).context("Failed to open config.json")?;
21            let mut contents = String::new();
22            file.read_to_string(&mut contents)
23                .context("Failed to read config.json")?;
24            let config: Config =
25                serde_json::from_str(&contents).context("Failed to parse config.json")?;
26            return Ok(config);
27        }
28
29        // If config.json doesn't exist, use default values
30        Ok(Config {
31            api_key: std::env::var("REPO_ANALYZER_API_KEY").ok(),
32            api_url: std::env::var("REPO_ANALYZER_API_URL").ok(),
33        })
34    }
35}