mkpm_sl/
application.rs

1use crate::{List, ReadJson, Retrieve, Search};
2use serde_derive::{Deserialize, Serialize};
3use serde_json::from_str;
4use std::io;
5use std::io::{Cursor, Error, ErrorKind};
6
7#[derive(Debug, Clone, Deserialize, Serialize)]
8pub struct Application {
9    pub name: String,
10    pub targets: Vec<String>,
11}
12
13impl List for Application{
14    fn list(item: &Vec<Self>) where Self: Sized {
15        println!("//// AVAILABLE APPLICATIONS");
16        for i in item{
17            println!("// {} => {:?}", &i.name, &i.targets)
18        }
19    }
20}
21
22impl ReadJson for Application {
23    fn read_json(path: &str) -> Result<Vec<Self>, Error> {
24        let s = std::fs::read_to_string(path)?;
25        let v: Vec<Self> = from_str(&s).unwrap();
26        Ok(v)
27    }
28}
29
30impl Search for Application {
31    fn search(to_check: &str, object: &Vec<Self>) -> Result<Self, String> {
32        for i in object {
33            if i.name == to_check {
34                return Ok(i.to_owned());
35            }
36        }
37        let msg = format!("Unable to find specified application, {}", to_check);
38        return Err(msg);
39    }
40}
41
42impl Application {
43    pub async fn get(&self, target: &str) -> Retrieve<()> {
44        let rel_link = format!("http://mkproj.com/releases/{}/{}", &self.name, target);
45        for t in &self.targets {
46            if target == t {
47                let resp = reqwest::get(&rel_link).await?;
48                let file_name = format!("{}_{}.zip", &self.name, target);
49                let mut file = std::fs::File::create(file_name)?;
50                let mut content = Cursor::new(resp.bytes().await?);
51                io::copy(&mut content, &mut file)?;
52                return Ok(());
53            }
54        }
55        return Err(Box::new(Error::from(ErrorKind::NotFound)));
56    }
57}