|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use clap::ArgMatches; |
| 4 | + |
| 5 | +use super::convert_extra_arg_val; |
| 6 | +use crate::{clang_tools::clang_tidy::CompilationDatabase, common_fs::FileFilter}; |
| 7 | + |
| 8 | +/// An enum to describe `--lines-changed-only` CLI option's behavior. |
| 9 | +#[derive(PartialEq, Clone, Debug)] |
| 10 | +pub enum LinesChangedOnly { |
| 11 | + /// All lines are scanned |
| 12 | + Off, |
| 13 | + /// Only lines in the diff are scanned |
| 14 | + Diff, |
| 15 | + /// Only lines in the diff with additions are scanned. |
| 16 | + On, |
| 17 | +} |
| 18 | + |
| 19 | +impl LinesChangedOnly { |
| 20 | + fn from_string(val: &str) -> LinesChangedOnly { |
| 21 | + match val { |
| 22 | + "true" | "on" | "1" => LinesChangedOnly::On, |
| 23 | + "diff" => LinesChangedOnly::Diff, |
| 24 | + _ => LinesChangedOnly::Off, |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/// A structure to contain parsed CLI options. |
| 30 | +pub struct Cli { |
| 31 | + pub version: String, |
| 32 | + pub verbosity: bool, |
| 33 | + pub extensions: Vec<String>, |
| 34 | + pub repo_root: String, |
| 35 | + pub lines_changed_only: LinesChangedOnly, |
| 36 | + pub files_changed_only: bool, |
| 37 | + pub ignore: Vec<String>, |
| 38 | + pub style: String, |
| 39 | + pub ignore_format: Vec<String>, |
| 40 | + pub ignore_tidy: Vec<String>, |
| 41 | + pub tidy_checks: String, |
| 42 | + pub database: Option<PathBuf>, |
| 43 | + pub extra_arg: Option<Vec<String>>, |
| 44 | + pub thread_comments: ThreadComments, |
| 45 | + pub no_lgtm: bool, |
| 46 | + pub step_summary: bool, |
| 47 | + pub file_annotations: bool, |
| 48 | +} |
| 49 | + |
| 50 | +impl From<&ArgMatches> for Cli { |
| 51 | + /// Construct a [`Cli`] instance from a [`ArgMatches`] instance (after options are parsed from CLI). |
| 52 | + fn from(args: &ArgMatches) -> Self { |
| 53 | + let ignore = args |
| 54 | + .get_many::<String>("ignore") |
| 55 | + .unwrap() |
| 56 | + .map(|s| s.to_owned()) |
| 57 | + .collect::<Vec<_>>(); |
| 58 | + let ignore_tidy = if let Some(val) = args.get_many::<String>("ignore-tidy") { |
| 59 | + val.map(|s| s.to_owned()).collect::<Vec<_>>() |
| 60 | + } else { |
| 61 | + vec![] |
| 62 | + }; |
| 63 | + let ignore_format = if let Some(val) = args.get_many::<String>("ignore-format") { |
| 64 | + val.map(|s| s.to_owned()).collect::<Vec<_>>() |
| 65 | + } else { |
| 66 | + vec![] |
| 67 | + }; |
| 68 | + let extra_arg = convert_extra_arg_val(args); |
| 69 | + |
| 70 | + let lines_changed_only = LinesChangedOnly::from_string( |
| 71 | + args.get_one::<String>("lines-changed-only") |
| 72 | + .unwrap() |
| 73 | + .as_str(), |
| 74 | + ); |
| 75 | + |
| 76 | + let thread_comments = ThreadComments::from_string( |
| 77 | + args.get_one::<String>("thread-comments").unwrap().as_str(), |
| 78 | + ); |
| 79 | + |
| 80 | + let extensions = args |
| 81 | + .get_many::<String>("extensions") |
| 82 | + .unwrap() |
| 83 | + .map(|s| s.to_string()) |
| 84 | + .collect::<Vec<_>>(); |
| 85 | + |
| 86 | + Self { |
| 87 | + version: args.get_one::<String>("version").unwrap().to_owned(), |
| 88 | + verbosity: args.get_one::<String>("verbosity").unwrap().as_str() == "debug", |
| 89 | + extensions, |
| 90 | + repo_root: args.get_one::<String>("repo-root").unwrap().to_owned(), |
| 91 | + lines_changed_only, |
| 92 | + files_changed_only: args.get_flag("files-changed-only"), |
| 93 | + ignore, |
| 94 | + style: args.get_one::<String>("style").unwrap().to_owned(), |
| 95 | + ignore_format, |
| 96 | + ignore_tidy, |
| 97 | + tidy_checks: args.get_one::<String>("tidy-checks").unwrap().to_owned(), |
| 98 | + database: args.get_one::<PathBuf>("database").map(|v| v.to_owned()), |
| 99 | + extra_arg, |
| 100 | + no_lgtm: args.get_flag("no-lgtm"), |
| 101 | + step_summary: args.get_flag("step-summary"), |
| 102 | + thread_comments, |
| 103 | + file_annotations: args.get_flag("file-annotations"), |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/// An enum to describe `--thread-comments` CLI option's behavior. |
| 109 | +#[derive(PartialEq, Clone, Debug)] |
| 110 | +pub enum ThreadComments { |
| 111 | + /// Always post a new comment and delete any outdated ones. |
| 112 | + On, |
| 113 | + /// Do not post thread comments. |
| 114 | + Off, |
| 115 | + /// Only update existing thread comments. |
| 116 | + /// If none exist, then post a new one. |
| 117 | + Update, |
| 118 | +} |
| 119 | + |
| 120 | +impl ThreadComments { |
| 121 | + fn from_string(val: &str) -> ThreadComments { |
| 122 | + match val { |
| 123 | + "true" | "on" | "1" => ThreadComments::On, |
| 124 | + "update" => ThreadComments::Update, |
| 125 | + _ => ThreadComments::Off, |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/// A data structure to contain CLI options that relate to |
| 131 | +/// clang-tidy or clang-format arguments. |
| 132 | +#[derive(Debug, Clone)] |
| 133 | +pub struct ClangParams { |
| 134 | + pub tidy_checks: String, |
| 135 | + pub lines_changed_only: LinesChangedOnly, |
| 136 | + pub database: Option<PathBuf>, |
| 137 | + pub extra_args: Option<Vec<String>>, |
| 138 | + pub database_json: Option<CompilationDatabase>, |
| 139 | + pub style: String, |
| 140 | + pub clang_tidy_command: Option<PathBuf>, |
| 141 | + pub clang_format_command: Option<PathBuf>, |
| 142 | + pub tidy_filter: FileFilter, |
| 143 | + pub format_filter: FileFilter, |
| 144 | +} |
| 145 | + |
| 146 | +impl From<&Cli> for ClangParams { |
| 147 | + /// Construct a [`ClangParams`] instance from a [`Cli`] instance. |
| 148 | + fn from(args: &Cli) -> Self { |
| 149 | + ClangParams { |
| 150 | + tidy_checks: args.tidy_checks.clone(), |
| 151 | + lines_changed_only: args.lines_changed_only.clone(), |
| 152 | + database: args.database.clone(), |
| 153 | + extra_args: args.extra_arg.clone(), |
| 154 | + database_json: None, |
| 155 | + style: args.style.clone(), |
| 156 | + clang_tidy_command: None, |
| 157 | + clang_format_command: None, |
| 158 | + tidy_filter: FileFilter::new(&args.ignore_tidy, args.extensions.clone()), |
| 159 | + format_filter: FileFilter::new(&args.ignore_format, args.extensions.clone()), |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +/// A struct to contain CLI options that relate to |
| 165 | +/// [`ResApiClient.post_feedback()`](fn@crate::rest_api::ResApiClient.post_feedback()). |
| 166 | +pub struct FeedbackInput { |
| 167 | + pub thread_comments: ThreadComments, |
| 168 | + pub no_lgtm: bool, |
| 169 | + pub step_summary: bool, |
| 170 | + pub file_annotations: bool, |
| 171 | + pub style: String, |
| 172 | +} |
| 173 | + |
| 174 | +impl From<&Cli> for FeedbackInput { |
| 175 | + /// Construct a [`FeedbackInput`] instance from a [`Cli`] instance. |
| 176 | + fn from(args: &Cli) -> Self { |
| 177 | + FeedbackInput { |
| 178 | + style: args.style.clone(), |
| 179 | + no_lgtm: args.no_lgtm, |
| 180 | + step_summary: args.step_summary, |
| 181 | + thread_comments: args.thread_comments.clone(), |
| 182 | + file_annotations: args.file_annotations, |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +impl Default for FeedbackInput { |
| 188 | + /// Construct a [`FeedbackInput`] instance with default values. |
| 189 | + fn default() -> Self { |
| 190 | + FeedbackInput { |
| 191 | + thread_comments: ThreadComments::Off, |
| 192 | + no_lgtm: true, |
| 193 | + step_summary: false, |
| 194 | + file_annotations: true, |
| 195 | + style: "llvm".to_string(), |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments