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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::utils::git;
use crate::{Error, Result};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Sources {
    Git {
        #[serde(flatten)]
        tree: git::Tree,
        version: String,
    },
    Url {
        url: String,
        hash: Option<String>,
        version: String,
    },
}

impl Sources {
    pub fn download(&self, dest_dir: impl AsRef<Path>) -> Result<PathBuf> {
        std::fs::create_dir_all(&dest_dir).map_err(|e| Error::SourcesDownloadFailed {
            sources: self.clone(),
            reason: e,
        })?;

        match self {
            Sources::Git { tree, .. } => git::archive(tree.clone()).to_dir(dest_dir),
            Sources::Url { .. } => todo!(),
        }
        .map_err(|e| Error::SourcesDownloadFailed {
            sources: self.clone(),
            reason: e,
        })
    }

    pub fn version(&self) -> String {
        match self {
            Sources::Git { version, .. } | Sources::Url { version, .. } => version.clone(),
        }
    }
}

impl AsRef<Sources> for Sources {
    fn as_ref(&self) -> &Sources {
        self
    }
}