cup_cli/config/
dependency.rs1use std::path::PathBuf;
2
3use anyhow::anyhow;
4
5use crate::config::Shelling;
6
7#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
8pub struct DependencyConfig {
9 pub path: Option<String>,
10 pub git: Option<String>,
11 pub features: Option<Vec<String>>,
12 #[serde(rename = "default-features")]
13 pub defaut_features: Option<bool>,
14 pub optional: Option<bool>,
15}
16
17impl Shelling<anyhow::Result<Dependency>> for DependencyConfig {
18 fn shelling(self) -> anyhow::Result<Dependency> {
19 let src = if let Some(path) = self.path {
20 DependencySource::Local(PathBuf::from(path))
21 } else if let Some(git) = self.git {
22 let mut parts = git.split('/').into_iter().collect::<Vec<_>>();
23 if parts.len() != 3 {
24 return Err(anyhow!(
25 "Invalid git mark format, should be owner/repo/tag."
26 ));
27 }
28 let owner = parts.remove(0);
29 let repo = parts.remove(0);
30 let tag = parts.remove(0);
31 DependencySource::Git {
32 owner: owner.to_string(),
33 repo: repo.to_string(),
34 tag: tag.to_string(),
35 }
36 } else {
37 return Err(anyhow!(
38 "Invalid dependency config, must specify either git or path."
39 ));
40 };
41 Ok(Dependency {
42 src,
43 features: self.features.unwrap_or_default(),
44 defaut_features: self.defaut_features.unwrap_or(true),
45 optional: self.optional.unwrap_or(false),
46 })
47 }
48}
49
50#[derive(Debug, Clone)]
51pub enum DependencySource {
52 Local(PathBuf),
53 Git {
54 owner: String,
55 repo: String,
56 tag: String,
57 },
58}
59
60#[derive(Debug, Clone)]
61pub struct Dependency {
62 pub src: DependencySource,
63 pub features: Vec<String>,
64 pub defaut_features: bool,
65 pub optional: bool,
66}