noapi_functions/
config.rs

1use serde::{Deserialize, Serialize};
2use std::{fs, io::Read, path::Path};
3
4#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5#[serde(rename_all = "lowercase")]
6pub enum JsRuntime {
7    Deno,
8    Bun,
9    Node,
10}
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
13#[serde(rename_all = "lowercase")]
14pub enum PackageManager {
15    Deno,
16    Bun,
17    NPM,
18    PNPM,
19    YARN,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct Config {
24    pub js_runtime: JsRuntime,
25    pub package_manager: PackageManager,
26}
27
28impl Config {
29    pub fn to_string(&self) -> String {
30        toml::to_string_pretty(self).expect("Failed to serialize config")
31    }
32
33    /*fn save_to_file(&self) {
34        let config_path = Path::new("NoApi.toml");
35        let toml_str = toml::to_string_pretty(self).expect("Failed to serialize config");
36        fs::write(&config_path, toml_str).expect("Failed to write config file");
37    }*/
38
39    pub fn from_file() -> Result<Self, Box<dyn std::error::Error>> {
40        let config_path = Path::new("NoApi.toml");
41        let mut file = fs::File::open(&config_path)?;
42        let mut contents = String::new();
43        file.read_to_string(&mut contents)?;
44        let config: Config = toml::from_str(&contents)?;
45        Ok(config)
46    }
47}