Skip to main content

solar_core/tool/semver_release/
plugin.rs

1use std::{path::Path, str::FromStr};
2
3use clap::ValueEnum;
4use reqwest::blocking::Client;
5use rust_dl::downloader::download_sync;
6use serde::{Deserialize, Serialize};
7use serde_json::{Map, Value};
8use url::Url;
9
10use crate::{Global, SolarError};
11
12#[derive(ValueEnum, Clone, PartialEq, Debug, Serialize, Deserialize, Ord, PartialOrd, Eq)]
13pub enum Plugin {
14    Cargo,
15}
16
17impl Plugin {
18    pub fn bin_name(&self) -> &str {
19        match self {
20            Self::Cargo => "semver-cargo",
21        }
22    }
23
24    pub fn download_url(&self) -> Result<Url, SolarError> {
25        match self {
26            Self::Cargo => Global::semver_cargo_exec_download(),
27        }
28    }
29
30    pub fn get_config(&self, client: &Client) -> Result<Map<String, Value>, SolarError> {
31        let url = match self {
32            Self::Cargo => Url::parse(
33                "https://github.com/nraynes/semver-cargo/raw/refs/heads/master/sample.plugin.config.json",
34            )?,
35        };
36        let response = client.get(url).send()?;
37        let value = &Value::from_str(&response.text()?)?;
38        Ok(value
39            .as_object()
40            .ok_or("Could not parse plugin config.")?
41            .clone())
42    }
43
44    pub fn download_exec(&self, download_path: &Path) -> Result<(), SolarError> {
45        Ok(download_sync(
46            self.download_url()?,
47            download_path.join(self.bin_name()),
48        )?)
49    }
50}