1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use serde::{Serialize, Deserialize};
use crate::errors::*;

#[derive(Serialize, Deserialize)]
pub struct CliVersion {
    free: String,
    pro: String
}

impl CliVersion {
    pub fn is_free_latest(&self, cur_ver: &str) -> bool { cur_ver == self.free.as_str() }
    pub fn is_pro_latest(&self, cur_ver: &str) -> bool { cur_ver == self.pro.as_str() }
    pub fn get_free(&self) -> &String { &self.free }
    pub fn get_pro(&self) -> &String { &self.pro }
    pub fn serialize(&self) -> String { serde_json::to_string(self).unwrap() }
    pub fn deserialize(content: &String) -> Self { serde_json::from_str(content).unwrap() }

    pub async fn check() -> Result<Self> {
        let url = "http://127.0.0.1:4000/download/version";
        let client = reqwest::Client::new();
        let response = client.get(url).send().await
            .or(Err(format!("Failed to GET from '{}'", &url)))?;
        let content = response.text().await?;
        let cv = Self::deserialize(&content);

        Ok(cv)
    }
}