git_helpe_rs/cli/
map_to_operation.rs

1use std::path::PathBuf;
2
3use anyhow::Ok;
4use clap::ArgMatches;
5
6use crate::file_utils::config_file::get_path_to_config;
7
8use super::{
9    CheckoutToPrefix, CommitOperationArguments, CommitSubcommandFlags, DryRunAndCopyFlag,
10    OperationWithArguments, ParsedArguments, SetFormat, UseTemplate,
11};
12
13impl TryFrom<ArgMatches> for ParsedArguments {
14    type Error = anyhow::Error;
15    fn try_from(value: ArgMatches) -> Result<Self, Self::Error> {
16        let operation_with_arguments = match value.subcommand() {
17            Some(("set-branch-prefix", args)) => {
18                let format_vals = get_key_val_from_arg_matches(args, "prefix").unwrap();
19
20                Ok(OperationWithArguments::SetBranchPrefix(format_vals))
21            }
22            Some(("set-branch-template", args)) => {
23                let format_vals = get_key_val_from_arg_matches(args, "template").unwrap();
24
25                Ok(OperationWithArguments::SetBranchFormat(format_vals))
26            }
27            Some(("set-commit", args)) => {
28                let format_vals = get_key_val_from_arg_matches(args, "template").unwrap();
29
30                Ok(OperationWithArguments::SetCommitFormat(format_vals))
31            }
32
33            Some(("c", args)) => {
34                let use_template = get_use_template_from_arg_matches(args);
35
36                let should_use_number_in_branch = args
37                    .get_one::<bool>("infer-number-from-branch")
38                    .unwrap_or(&false);
39
40                let dry_run_and_copy_flags = get_dry_run_and_copy_flags(args);
41                let commit_flags = CommitSubcommandFlags {
42                    use_branch_number: should_use_number_in_branch.to_owned(),
43                    dry_run: dry_run_and_copy_flags.dry_run,
44                    copy: dry_run_and_copy_flags.copy,
45                };
46
47                let args = CommitOperationArguments {
48                    flags: commit_flags,
49                    use_template: use_template,
50                };
51                let commit_operation_with_arguments = OperationWithArguments::Commit(args);
52
53                Ok(commit_operation_with_arguments)
54            }
55
56            Some(("bt", args)) => {
57                let use_template = get_use_template_from_arg_matches(args);
58
59                Ok(OperationWithArguments::BranchFromTemplate(use_template))
60            }
61            Some(("bp", args)) => {
62                let prefix_key = args.get_one::<String>("prefix").unwrap();
63                let dry_run_and_copy_flags = get_dry_run_and_copy_flags(args);
64                let checkout_to_prefix = CheckoutToPrefix {
65                    prefix_key: prefix_key.to_owned(),
66                    dry_run: dry_run_and_copy_flags.dry_run,
67                    copy: dry_run_and_copy_flags.copy,
68                };
69
70                Ok(OperationWithArguments::BranchFromClipboard(
71                    checkout_to_prefix,
72                ))
73            }
74            Some(("set-clipboard-command", args)) => {
75                let mut args = args.clone();
76                let clipboard_command: Vec<String> =
77                    args.remove_many("copy-paste-pair").unwrap().collect();
78
79                let copy = clipboard_command.get(0).unwrap();
80                let paste = clipboard_command.get(1).unwrap();
81
82                Ok(OperationWithArguments::SetClipboardCommands(
83                    super::SetClipboardCommands {
84                        copy: copy.to_owned(),
85                        paste: paste.to_owned(),
86                    },
87                ))
88            }
89            Some(("generate-autocompletion-script", args)) => {
90                let path: PathBuf = args.get_one::<String>("output-directory").unwrap().into();
91
92                Ok(OperationWithArguments::GenerateAutocompletionScript(
93                    path.to_owned(),
94                ))
95            }
96            Some(("show", _args)) => Ok(OperationWithArguments::Show),
97            _ => Err(anyhow::anyhow!("Unknown command")),
98        };
99
100        let path_to_config_from_args = value.get_one::<PathBuf>("config");
101        let path_to_config = get_path_to_config(path_to_config_from_args.cloned());
102
103        Ok(ParsedArguments {
104            operation_with_arguments: operation_with_arguments.unwrap(),
105            path_to_config: path_to_config.to_owned(),
106        })
107    }
108}
109
110fn get_key_val_from_arg_matches(
111    args: &ArgMatches,
112    value_id: &str,
113) -> Result<SetFormat, anyhow::Error> {
114    let default_key = "default".to_string();
115    let key = args.get_one::<String>("key").unwrap_or(&default_key);
116    let value = args.get_one::<String>(value_id).unwrap();
117
118    Ok(SetFormat {
119        key: key.to_owned(),
120        value: value.to_owned(),
121    })
122}
123
124fn get_dry_run_and_copy_flags(args: &ArgMatches) -> DryRunAndCopyFlag {
125    let copy = args
126        .get_one::<bool>("copy-flag")
127        .unwrap_or(&false)
128        .to_owned();
129    let dry_run = args.get_one::<bool>("dry-run").unwrap_or(&false).to_owned();
130
131    DryRunAndCopyFlag { copy, dry_run }
132}
133
134fn get_use_template_from_arg_matches(args: &ArgMatches) -> UseTemplate {
135    let key = if let Some(key) = args.get_one::<String>("key") {
136        key.to_owned()
137    } else {
138        "default".to_owned()
139    };
140
141    let mut args = args.clone();
142
143    let interpolate_values: Vec<String> = args.remove_many("interpolate-values").unwrap().collect();
144
145    let use_autocomplete = match args.try_contains_id("auto-complete") {
146        Err(_) => &false,
147        _ => args.get_one::<bool>("auto-complete").unwrap_or(&false),
148        // Err(_) => &false,
149    };
150
151    let dry_run_and_copy_flags = get_dry_run_and_copy_flags(&args);
152
153    UseTemplate {
154        interpolate_values: interpolate_values,
155        key: key,
156        use_autocomplete: use_autocomplete.to_owned(),
157        dry_run: dry_run_and_copy_flags.dry_run,
158        copy: dry_run_and_copy_flags.copy,
159    }
160}