cargo_tangle/create/
source.rs

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 clap::Args;
use std::path::PathBuf;

#[derive(Args, Debug, Clone, Default)]
#[group(id = "source", required = false, multiple = false)]
pub struct Source {
    #[command(flatten)]
    repo: Option<RepoArgs>,

    #[arg(short, long, group = "source")]
    path: Option<PathBuf>,
}

#[derive(Args, Debug, Clone)]
#[group(requires = "repo")]
pub struct RepoArgs {
    #[arg(short, long, env, required = false, group = "source")]
    repo: String,
    #[arg(short, long, env)]
    branch: Option<String>,
    #[arg(short, long, env, conflicts_with = "branch")]
    tag: Option<String>,
}

impl From<Source> for Option<cargo_generate::TemplatePath> {
    fn from(value: Source) -> Self {
        let mut template_path = cargo_generate::TemplatePath::default();

        match value {
            Source {
                repo: Some(repo_args),
                ..
            } => {
                template_path.git = Some(repo_args.repo);
                template_path.branch = repo_args.branch;
                template_path.tag = repo_args.tag;
                Some(template_path)
            }
            Source {
                path: Some(path), ..
            } => {
                template_path.path = Some(path.to_string_lossy().into());
                Some(template_path)
            }
            Source {
                repo: None,
                path: None,
            } => None,
        }
    }
}