Skip to content

Commit 47f2245

Browse files
authored
Merge pull request #603 from SteveL-MSFT/clippy-option
Fix change in clippy rule from `&Option<Type>` to `Option<&Type>`
2 parents a6db74b + 638333b commit 47f2245

File tree

9 files changed

+106
-106
lines changed

9 files changed

+106
-106
lines changed

dsc/src/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub enum SubCommand {
6868
#[clap(name = "type", short, long, help = "The type of DSC schema to get")]
6969
dsc_type: DscType,
7070
#[clap(short = 'o', long, help = "The output format to use")]
71-
format: Option<OutputFormat>,
71+
output_format: Option<OutputFormat>,
7272
},
7373
}
7474

dsc/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn main() {
3333

3434
let args = Args::parse();
3535

36-
util::enable_tracing(&args.trace_level, &args.trace_format);
36+
util::enable_tracing(args.trace_level.as_ref(), args.trace_format.as_ref());
3737

3838
debug!("Running dsc {}", env!("CARGO_PKG_VERSION"));
3939

@@ -47,21 +47,21 @@ fn main() {
4747
if let Some(file_name) = parameters_file {
4848
info!("Reading parameters from file {file_name}");
4949
match std::fs::read_to_string(&file_name) {
50-
Ok(parameters) => subcommand::config(&subcommand, &Some(parameters), &system_root, &as_group, &as_include),
50+
Ok(parameters) => subcommand::config(&subcommand, &Some(parameters), system_root.as_ref(), &as_group, &as_include),
5151
Err(err) => {
5252
error!("Error: Failed to read parameters file '{file_name}': {err}");
5353
exit(util::EXIT_INVALID_INPUT);
5454
}
5555
}
5656
}
5757
else {
58-
subcommand::config(&subcommand, &parameters, &system_root, &as_group, &as_include);
58+
subcommand::config(&subcommand, &parameters, system_root.as_ref(), &as_group, &as_include);
5959
}
6060
},
6161
SubCommand::Resource { subcommand } => {
6262
subcommand::resource(&subcommand);
6363
},
64-
SubCommand::Schema { dsc_type , format } => {
64+
SubCommand::Schema { dsc_type , output_format } => {
6565
let schema = util::get_schema(dsc_type);
6666
let json = match serde_json::to_string(&schema) {
6767
Ok(json) => json,
@@ -70,7 +70,7 @@ fn main() {
7070
exit(util::EXIT_JSON_ERROR);
7171
}
7272
};
73-
util::write_output(&json, &format);
73+
util::write_output(&json, output_format.as_ref());
7474
},
7575
}
7676

dsc/src/resource_command.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use dsc_lib::{
1515
};
1616
use std::process::exit;
1717

18-
pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: &Option<OutputFormat>) {
18+
pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: Option<&OutputFormat>) {
1919
let Some(mut resource) = get_resource(dsc, resource_type) else {
2020
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
2121
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
@@ -56,7 +56,7 @@ pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: &Op
5656
}
5757
}
5858

59-
pub fn get_all(dsc: &DscManager, resource_type: &str, format: &Option<OutputFormat>) {
59+
pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputFormat>) {
6060
let mut input = String::new();
6161
let Some(mut resource) = get_resource(dsc, resource_type) else {
6262
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
@@ -104,7 +104,7 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: &Option<OutputForm
104104
}
105105
}
106106

107-
pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: &Option<OutputFormat>) {
107+
pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: Option<&OutputFormat>) {
108108
if input.is_empty() {
109109
error!("Error: Desired input is empty");
110110
exit(EXIT_INVALID_ARGS);
@@ -150,7 +150,7 @@ pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: &Op
150150
}
151151
}
152152

153-
pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: &Option<OutputFormat>) {
153+
pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: Option<&OutputFormat>) {
154154
if input.is_empty() {
155155
error!("Error: Expected input is required");
156156
exit(EXIT_INVALID_ARGS);
@@ -227,7 +227,7 @@ pub fn delete(dsc: &DscManager, resource_type: &str, mut input: String) {
227227
}
228228
}
229229

230-
pub fn schema(dsc: &DscManager, resource_type: &str, format: &Option<OutputFormat>) {
230+
pub fn schema(dsc: &DscManager, resource_type: &str, format: Option<&OutputFormat>) {
231231
let Some(resource) = get_resource(dsc, resource_type) else {
232232
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
233233
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
@@ -256,7 +256,7 @@ pub fn schema(dsc: &DscManager, resource_type: &str, format: &Option<OutputForma
256256
}
257257
}
258258

259-
pub fn export(dsc: &mut DscManager, resource_type: &str, format: &Option<OutputFormat>) {
259+
pub fn export(dsc: &mut DscManager, resource_type: &str, format: Option<&OutputFormat>) {
260260
let mut input = String::new();
261261
let Some(dsc_resource) = get_resource(dsc, resource_type) else {
262262
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());

dsc/src/subcommand.rs

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use std::{
3434
};
3535
use tracing::{debug, error, trace};
3636

37-
pub fn config_get(configurator: &mut Configurator, format: &Option<OutputFormat>, as_group: &bool)
37+
pub fn config_get(configurator: &mut Configurator, format: Option<&OutputFormat>, as_group: &bool)
3838
{
3939
match configurator.invoke_get() {
4040
Ok(result) => {
@@ -69,7 +69,7 @@ pub fn config_get(configurator: &mut Configurator, format: &Option<OutputFormat>
6969
}
7070
}
7171

72-
pub fn config_set(configurator: &mut Configurator, format: &Option<OutputFormat>, as_group: &bool)
72+
pub fn config_set(configurator: &mut Configurator, format: Option<&OutputFormat>, as_group: &bool)
7373
{
7474
match configurator.invoke_set(false) {
7575
Ok(result) => {
@@ -104,7 +104,7 @@ pub fn config_set(configurator: &mut Configurator, format: &Option<OutputFormat>
104104
}
105105
}
106106

107-
pub fn config_test(configurator: &mut Configurator, format: &Option<OutputFormat>, as_group: &bool, as_get: &bool, as_config: &bool)
107+
pub fn config_test(configurator: &mut Configurator, format: Option<&OutputFormat>, as_group: &bool, as_get: &bool, as_config: &bool)
108108
{
109109
match configurator.invoke_test() {
110110
Ok(result) => {
@@ -190,7 +190,7 @@ pub fn config_test(configurator: &mut Configurator, format: &Option<OutputFormat
190190
}
191191
}
192192

193-
pub fn config_export(configurator: &mut Configurator, format: &Option<OutputFormat>)
193+
pub fn config_export(configurator: &mut Configurator, format: Option<&OutputFormat>)
194194
{
195195
match configurator.invoke_export() {
196196
Ok(result) => {
@@ -219,7 +219,7 @@ pub fn config_export(configurator: &mut Configurator, format: &Option<OutputForm
219219
}
220220
}
221221

222-
fn initialize_config_root(path: &Option<String>) -> Option<String> {
222+
fn initialize_config_root(path: Option<&String>) -> Option<String> {
223223
// code that calls this pass in either None, Some("-"), or Some(path)
224224
// in the case of `-` we treat it as None, but need to pass it back as subsequent processing needs to handle it
225225
let use_stdin = if let Some(specified_path) = path {
@@ -250,15 +250,15 @@ fn initialize_config_root(path: &Option<String>) -> Option<String> {
250250
}
251251

252252
#[allow(clippy::too_many_lines)]
253-
pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounted_path: &Option<String>, as_group: &bool, as_include: &bool) {
253+
pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounted_path: Option<&String>, as_group: &bool, as_include: &bool) {
254254
let (new_parameters, json_string) = match subcommand {
255255
ConfigSubCommand::Get { input, file, .. } |
256256
ConfigSubCommand::Set { input, file, .. } |
257257
ConfigSubCommand::Test { input, file, .. } |
258258
ConfigSubCommand::Validate { input, file, .. } |
259259
ConfigSubCommand::Export { input, file, .. } => {
260-
let new_path = initialize_config_root(file);
261-
let document = get_input(input, &new_path);
260+
let new_path = initialize_config_root(file.as_ref());
261+
let document = get_input(input.as_ref(), new_path.as_ref());
262262
if *as_include {
263263
let (new_parameters, config_json) = match get_contents(&document) {
264264
Ok((parameters, config_json)) => (parameters, config_json),
@@ -273,8 +273,8 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
273273
}
274274
},
275275
ConfigSubCommand::Resolve { input, file, .. } => {
276-
let new_path = initialize_config_root(file);
277-
let document = get_input(input, &new_path);
276+
let new_path = initialize_config_root(file.as_ref());
277+
let document = get_input(input.as_ref(), new_path.as_ref());
278278
let (new_parameters, config_json) = match get_contents(&document) {
279279
Ok((parameters, config_json)) => (parameters, config_json),
280280
Err(err) => {
@@ -343,29 +343,29 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
343343
configurator.set_system_root(path);
344344
}
345345

346-
if let Err(err) = configurator.set_context(&parameters) {
346+
if let Err(err) = configurator.set_context(parameters.as_ref()) {
347347
error!("Error: Parameter input failure: {err}");
348348
exit(EXIT_INVALID_INPUT);
349349
}
350350

351351
match subcommand {
352-
ConfigSubCommand::Get { output_format: format, .. } => {
353-
config_get(&mut configurator, format, as_group);
352+
ConfigSubCommand::Get { output_format, .. } => {
353+
config_get(&mut configurator, output_format.as_ref(), as_group);
354354
},
355-
ConfigSubCommand::Set { output_format: format, .. } => {
356-
config_set(&mut configurator, format, as_group);
355+
ConfigSubCommand::Set { output_format, .. } => {
356+
config_set(&mut configurator, output_format.as_ref(), as_group);
357357
},
358-
ConfigSubCommand::Test { output_format: format, as_get, as_config, .. } => {
359-
config_test(&mut configurator, format, as_group, as_get, as_config);
358+
ConfigSubCommand::Test { output_format, as_get, as_config, .. } => {
359+
config_test(&mut configurator, output_format.as_ref(), as_group, as_get, as_config);
360360
},
361-
ConfigSubCommand::Validate { input, file, output_format: format} => {
361+
ConfigSubCommand::Validate { input, file, output_format} => {
362362
let mut result = ValidateResult {
363363
valid: true,
364364
reason: None,
365365
};
366366
if *as_include {
367-
let new_path = initialize_config_root(file);
368-
let input = get_input(input, &new_path);
367+
let new_path = initialize_config_root(file.as_ref());
368+
let input = get_input(input.as_ref(), new_path.as_ref());
369369
match serde_json::from_str::<Include>(&input) {
370370
Ok(_) => {
371371
// valid, so do nothing
@@ -392,12 +392,12 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
392392
exit(EXIT_JSON_ERROR);
393393
};
394394

395-
write_output(&json, format);
395+
write_output(&json, output_format.as_ref());
396396
},
397-
ConfigSubCommand::Export { output_format: format, .. } => {
398-
config_export(&mut configurator, format);
397+
ConfigSubCommand::Export { output_format, .. } => {
398+
config_export(&mut configurator, output_format.as_ref());
399399
},
400-
ConfigSubCommand::Resolve { output_format: format, .. } => {
400+
ConfigSubCommand::Resolve { output_format, .. } => {
401401
let configuration = match serde_json::from_str(&json_string) {
402402
Ok(json) => json,
403403
Err(err) => {
@@ -426,7 +426,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
426426
exit(EXIT_JSON_ERROR);
427427
}
428428
};
429-
write_output(&json_string, format);
429+
write_output(&json_string, output_format.as_ref());
430430
},
431431
}
432432
}
@@ -531,51 +531,51 @@ pub fn resource(subcommand: &ResourceSubCommand) {
531531
};
532532

533533
match subcommand {
534-
ResourceSubCommand::List { resource_name, adapter_name, description, tags, output_format: format } => {
535-
list_resources(&mut dsc, resource_name, adapter_name, description, tags, format);
534+
ResourceSubCommand::List { resource_name, adapter_name, description, tags, output_format } => {
535+
list_resources(&mut dsc, resource_name.as_ref(), adapter_name.as_ref(), description.as_ref(), tags.as_ref(), output_format.as_ref());
536536
},
537-
ResourceSubCommand::Schema { resource , output_format: format } => {
537+
ResourceSubCommand::Schema { resource , output_format } => {
538538
dsc.find_resources(&[resource.to_string()]);
539-
resource_command::schema(&dsc, resource, format);
539+
resource_command::schema(&dsc, resource, output_format.as_ref());
540540
},
541-
ResourceSubCommand::Export { resource, output_format: format } => {
541+
ResourceSubCommand::Export { resource, output_format } => {
542542
dsc.find_resources(&[resource.to_string()]);
543-
resource_command::export(&mut dsc, resource, format);
543+
resource_command::export(&mut dsc, resource, output_format.as_ref());
544544
},
545-
ResourceSubCommand::Get { resource, input, file: path, all, output_format: format } => {
545+
ResourceSubCommand::Get { resource, input, file: path, all, output_format } => {
546546
dsc.find_resources(&[resource.to_string()]);
547-
if *all { resource_command::get_all(&dsc, resource, format); }
547+
if *all { resource_command::get_all(&dsc, resource, output_format.as_ref()); }
548548
else {
549-
let parsed_input = get_input(input, path);
550-
resource_command::get(&dsc, resource, parsed_input, format);
549+
let parsed_input = get_input(input.as_ref(), path.as_ref());
550+
resource_command::get(&dsc, resource, parsed_input, output_format.as_ref());
551551
}
552552
},
553-
ResourceSubCommand::Set { resource, input, file: path, output_format: format } => {
553+
ResourceSubCommand::Set { resource, input, file: path, output_format } => {
554554
dsc.find_resources(&[resource.to_string()]);
555-
let parsed_input = get_input(input, path);
556-
resource_command::set(&dsc, resource, parsed_input, format);
555+
let parsed_input = get_input(input.as_ref(), path.as_ref());
556+
resource_command::set(&dsc, resource, parsed_input, output_format.as_ref());
557557
},
558-
ResourceSubCommand::Test { resource, input, file: path, output_format: format } => {
558+
ResourceSubCommand::Test { resource, input, file: path, output_format } => {
559559
dsc.find_resources(&[resource.to_string()]);
560-
let parsed_input = get_input(input, path);
561-
resource_command::test(&dsc, resource, parsed_input, format);
560+
let parsed_input = get_input(input.as_ref(), path.as_ref());
561+
resource_command::test(&dsc, resource, parsed_input, output_format.as_ref());
562562
},
563563
ResourceSubCommand::Delete { resource, input, file: path } => {
564564
dsc.find_resources(&[resource.to_string()]);
565-
let parsed_input = get_input(input, path);
565+
let parsed_input = get_input(input.as_ref(), path.as_ref());
566566
resource_command::delete(&dsc, resource, parsed_input);
567567
},
568568
}
569569
}
570570

571-
fn list_resources(dsc: &mut DscManager, resource_name: &Option<String>, adapter_name: &Option<String>, description: &Option<String>, tags: &Option<Vec<String>>, format: &Option<OutputFormat>) {
571+
fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_name: Option<&String>, description: Option<&String>, tags: Option<&Vec<String>>, format: Option<&OutputFormat>) {
572572
let mut write_table = false;
573573
let mut table = Table::new(&["Type", "Kind", "Version", "Caps", "RequireAdapter", "Description"]);
574574
if format.is_none() && io::stdout().is_terminal() {
575575
// write as table if format is not specified and interactive
576576
write_table = true;
577577
}
578-
for resource in dsc.list_available_resources(&resource_name.clone().unwrap_or("*".to_string()), &adapter_name.clone().unwrap_or_default()) {
578+
for resource in dsc.list_available_resources(resource_name.unwrap_or(&String::from("*")), adapter_name.unwrap_or(&String::new())) {
579579
let mut capabilities = "--------".to_string();
580580
let capability_types = [
581581
(Capability::Get, "g"),
@@ -606,7 +606,7 @@ fn list_resources(dsc: &mut DscManager, resource_name: &Option<String>, adapter_
606606

607607
// if description is specified, skip if resource description does not contain it
608608
if description.is_some() &&
609-
(manifest.description.is_none() | !manifest.description.unwrap_or_default().to_lowercase().contains(&description.as_ref().unwrap_or(&String::new()).to_lowercase())) {
609+
(manifest.description.is_none() | !manifest.description.unwrap_or_default().to_lowercase().contains(&description.unwrap_or(&String::new()).to_lowercase())) {
610610
continue;
611611
}
612612

dsc/src/util.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,18 @@ pub fn get_schema(dsc_type: DscType) -> RootSchema {
213213
///
214214
/// * `json` - The JSON to write
215215
/// * `format` - The format to use
216-
pub fn write_output(json: &str, format: &Option<OutputFormat>) {
216+
pub fn write_output(json: &str, format: Option<&OutputFormat>) {
217217
let mut is_json = true;
218-
let mut output_format = format.clone();
218+
let mut output_format = format;
219219
let mut syntax_color = false;
220220
if std::io::stdout().is_terminal() {
221221
syntax_color = true;
222222
if output_format.is_none() {
223-
output_format = Some(OutputFormat::Yaml);
223+
output_format = Some(&OutputFormat::Yaml);
224224
}
225225
}
226226
else if output_format.is_none() {
227-
output_format = Some(OutputFormat::Json);
227+
output_format = Some(&OutputFormat::Json);
228228
}
229229

230230
let output = match output_format {
@@ -292,7 +292,7 @@ pub fn write_output(json: &str, format: &Option<OutputFormat>) {
292292
}
293293

294294
#[allow(clippy::too_many_lines)]
295-
pub fn enable_tracing(trace_level_arg: &Option<TraceLevel>, trace_format_arg: &Option<TraceFormat>) {
295+
pub fn enable_tracing(trace_level_arg: Option<&TraceLevel>, trace_format_arg: Option<&TraceFormat>) {
296296

297297
let mut policy_is_used = false;
298298
let mut tracing_setting = TracingSetting::default();
@@ -453,7 +453,7 @@ pub fn validate_json(source: &str, schema: &Value, json: &Value) -> Result<(), D
453453
Ok(())
454454
}
455455

456-
pub fn get_input(input: &Option<String>, file: &Option<String>) -> String {
456+
pub fn get_input(input: Option<&String>, file: Option<&String>) -> String {
457457
trace!("Input: {input:?}, File: {file:?}");
458458
let value = if let Some(input) = input {
459459
debug!("Reading input from command line parameter");

0 commit comments

Comments
 (0)