Skip to content

Fix change in clippy rule from &Option<Type> to Option<&Type> #603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dsc/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub enum SubCommand {
#[clap(name = "type", short, long, help = "The type of DSC schema to get")]
dsc_type: DscType,
#[clap(short = 'o', long, help = "The output format to use")]
format: Option<OutputFormat>,
output_format: Option<OutputFormat>,
},
}

Expand Down
10 changes: 5 additions & 5 deletions dsc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() {

let args = Args::parse();

util::enable_tracing(&args.trace_level, &args.trace_format);
util::enable_tracing(args.trace_level.as_ref(), args.trace_format.as_ref());

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

Expand All @@ -47,21 +47,21 @@ fn main() {
if let Some(file_name) = parameters_file {
info!("Reading parameters from file {file_name}");
match std::fs::read_to_string(&file_name) {
Ok(parameters) => subcommand::config(&subcommand, &Some(parameters), &system_root, &as_group, &as_include),
Ok(parameters) => subcommand::config(&subcommand, &Some(parameters), system_root.as_ref(), &as_group, &as_include),
Err(err) => {
error!("Error: Failed to read parameters file '{file_name}': {err}");
exit(util::EXIT_INVALID_INPUT);
}
}
}
else {
subcommand::config(&subcommand, &parameters, &system_root, &as_group, &as_include);
subcommand::config(&subcommand, &parameters, system_root.as_ref(), &as_group, &as_include);
}
},
SubCommand::Resource { subcommand } => {
subcommand::resource(&subcommand);
},
SubCommand::Schema { dsc_type , format } => {
SubCommand::Schema { dsc_type , output_format } => {
let schema = util::get_schema(dsc_type);
let json = match serde_json::to_string(&schema) {
Ok(json) => json,
Expand All @@ -70,7 +70,7 @@ fn main() {
exit(util::EXIT_JSON_ERROR);
}
};
util::write_output(&json, &format);
util::write_output(&json, output_format.as_ref());
},
}

Expand Down
12 changes: 6 additions & 6 deletions dsc/src/resource_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use dsc_lib::{
};
use std::process::exit;

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

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

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

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

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

pub fn export(dsc: &mut DscManager, resource_type: &str, format: &Option<OutputFormat>) {
pub fn export(dsc: &mut DscManager, resource_type: &str, format: Option<&OutputFormat>) {
let mut input = String::new();
let Some(dsc_resource) = get_resource(dsc, resource_type) else {
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
Expand Down
90 changes: 45 additions & 45 deletions dsc/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use std::{
};
use tracing::{debug, error, trace};

pub fn config_get(configurator: &mut Configurator, format: &Option<OutputFormat>, as_group: &bool)
pub fn config_get(configurator: &mut Configurator, format: Option<&OutputFormat>, as_group: &bool)
{
match configurator.invoke_get() {
Ok(result) => {
Expand Down Expand Up @@ -69,7 +69,7 @@ pub fn config_get(configurator: &mut Configurator, format: &Option<OutputFormat>
}
}

pub fn config_set(configurator: &mut Configurator, format: &Option<OutputFormat>, as_group: &bool)
pub fn config_set(configurator: &mut Configurator, format: Option<&OutputFormat>, as_group: &bool)
{
match configurator.invoke_set(false) {
Ok(result) => {
Expand Down Expand Up @@ -104,7 +104,7 @@ pub fn config_set(configurator: &mut Configurator, format: &Option<OutputFormat>
}
}

pub fn config_test(configurator: &mut Configurator, format: &Option<OutputFormat>, as_group: &bool, as_get: &bool, as_config: &bool)
pub fn config_test(configurator: &mut Configurator, format: Option<&OutputFormat>, as_group: &bool, as_get: &bool, as_config: &bool)
{
match configurator.invoke_test() {
Ok(result) => {
Expand Down Expand Up @@ -190,7 +190,7 @@ pub fn config_test(configurator: &mut Configurator, format: &Option<OutputFormat
}
}

pub fn config_export(configurator: &mut Configurator, format: &Option<OutputFormat>)
pub fn config_export(configurator: &mut Configurator, format: Option<&OutputFormat>)
{
match configurator.invoke_export() {
Ok(result) => {
Expand Down Expand Up @@ -219,7 +219,7 @@ pub fn config_export(configurator: &mut Configurator, format: &Option<OutputForm
}
}

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

#[allow(clippy::too_many_lines)]
pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounted_path: &Option<String>, as_group: &bool, as_include: &bool) {
pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounted_path: Option<&String>, as_group: &bool, as_include: &bool) {
let (new_parameters, json_string) = match subcommand {
ConfigSubCommand::Get { input, file, .. } |
ConfigSubCommand::Set { input, file, .. } |
ConfigSubCommand::Test { input, file, .. } |
ConfigSubCommand::Validate { input, file, .. } |
ConfigSubCommand::Export { input, file, .. } => {
let new_path = initialize_config_root(file);
let document = get_input(input, &new_path);
let new_path = initialize_config_root(file.as_ref());
let document = get_input(input.as_ref(), new_path.as_ref());
if *as_include {
let (new_parameters, config_json) = match get_contents(&document) {
Ok((parameters, config_json)) => (parameters, config_json),
Expand All @@ -273,8 +273,8 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
}
},
ConfigSubCommand::Resolve { input, file, .. } => {
let new_path = initialize_config_root(file);
let document = get_input(input, &new_path);
let new_path = initialize_config_root(file.as_ref());
let document = get_input(input.as_ref(), new_path.as_ref());
let (new_parameters, config_json) = match get_contents(&document) {
Ok((parameters, config_json)) => (parameters, config_json),
Err(err) => {
Expand Down Expand Up @@ -343,29 +343,29 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
configurator.set_system_root(path);
}

if let Err(err) = configurator.set_context(&parameters) {
if let Err(err) = configurator.set_context(parameters.as_ref()) {
error!("Error: Parameter input failure: {err}");
exit(EXIT_INVALID_INPUT);
}

match subcommand {
ConfigSubCommand::Get { output_format: format, .. } => {
config_get(&mut configurator, format, as_group);
ConfigSubCommand::Get { output_format, .. } => {
config_get(&mut configurator, output_format.as_ref(), as_group);
},
ConfigSubCommand::Set { output_format: format, .. } => {
config_set(&mut configurator, format, as_group);
ConfigSubCommand::Set { output_format, .. } => {
config_set(&mut configurator, output_format.as_ref(), as_group);
},
ConfigSubCommand::Test { output_format: format, as_get, as_config, .. } => {
config_test(&mut configurator, format, as_group, as_get, as_config);
ConfigSubCommand::Test { output_format, as_get, as_config, .. } => {
config_test(&mut configurator, output_format.as_ref(), as_group, as_get, as_config);
},
ConfigSubCommand::Validate { input, file, output_format: format} => {
ConfigSubCommand::Validate { input, file, output_format} => {
let mut result = ValidateResult {
valid: true,
reason: None,
};
if *as_include {
let new_path = initialize_config_root(file);
let input = get_input(input, &new_path);
let new_path = initialize_config_root(file.as_ref());
let input = get_input(input.as_ref(), new_path.as_ref());
match serde_json::from_str::<Include>(&input) {
Ok(_) => {
// valid, so do nothing
Expand All @@ -392,12 +392,12 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
exit(EXIT_JSON_ERROR);
};

write_output(&json, format);
write_output(&json, output_format.as_ref());
},
ConfigSubCommand::Export { output_format: format, .. } => {
config_export(&mut configurator, format);
ConfigSubCommand::Export { output_format, .. } => {
config_export(&mut configurator, output_format.as_ref());
},
ConfigSubCommand::Resolve { output_format: format, .. } => {
ConfigSubCommand::Resolve { output_format, .. } => {
let configuration = match serde_json::from_str(&json_string) {
Ok(json) => json,
Err(err) => {
Expand Down Expand Up @@ -426,7 +426,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
exit(EXIT_JSON_ERROR);
}
};
write_output(&json_string, format);
write_output(&json_string, output_format.as_ref());
},
}
}
Expand Down Expand Up @@ -531,51 +531,51 @@ pub fn resource(subcommand: &ResourceSubCommand) {
};

match subcommand {
ResourceSubCommand::List { resource_name, adapter_name, description, tags, output_format: format } => {
list_resources(&mut dsc, resource_name, adapter_name, description, tags, format);
ResourceSubCommand::List { resource_name, adapter_name, description, tags, output_format } => {
list_resources(&mut dsc, resource_name.as_ref(), adapter_name.as_ref(), description.as_ref(), tags.as_ref(), output_format.as_ref());
},
ResourceSubCommand::Schema { resource , output_format: format } => {
ResourceSubCommand::Schema { resource , output_format } => {
dsc.find_resources(&[resource.to_string()]);
resource_command::schema(&dsc, resource, format);
resource_command::schema(&dsc, resource, output_format.as_ref());
},
ResourceSubCommand::Export { resource, output_format: format } => {
ResourceSubCommand::Export { resource, output_format } => {
dsc.find_resources(&[resource.to_string()]);
resource_command::export(&mut dsc, resource, format);
resource_command::export(&mut dsc, resource, output_format.as_ref());
},
ResourceSubCommand::Get { resource, input, file: path, all, output_format: format } => {
ResourceSubCommand::Get { resource, input, file: path, all, output_format } => {
dsc.find_resources(&[resource.to_string()]);
if *all { resource_command::get_all(&dsc, resource, format); }
if *all { resource_command::get_all(&dsc, resource, output_format.as_ref()); }
else {
let parsed_input = get_input(input, path);
resource_command::get(&dsc, resource, parsed_input, format);
let parsed_input = get_input(input.as_ref(), path.as_ref());
resource_command::get(&dsc, resource, parsed_input, output_format.as_ref());
}
},
ResourceSubCommand::Set { resource, input, file: path, output_format: format } => {
ResourceSubCommand::Set { resource, input, file: path, output_format } => {
dsc.find_resources(&[resource.to_string()]);
let parsed_input = get_input(input, path);
resource_command::set(&dsc, resource, parsed_input, format);
let parsed_input = get_input(input.as_ref(), path.as_ref());
resource_command::set(&dsc, resource, parsed_input, output_format.as_ref());
},
ResourceSubCommand::Test { resource, input, file: path, output_format: format } => {
ResourceSubCommand::Test { resource, input, file: path, output_format } => {
dsc.find_resources(&[resource.to_string()]);
let parsed_input = get_input(input, path);
resource_command::test(&dsc, resource, parsed_input, format);
let parsed_input = get_input(input.as_ref(), path.as_ref());
resource_command::test(&dsc, resource, parsed_input, output_format.as_ref());
},
ResourceSubCommand::Delete { resource, input, file: path } => {
dsc.find_resources(&[resource.to_string()]);
let parsed_input = get_input(input, path);
let parsed_input = get_input(input.as_ref(), path.as_ref());
resource_command::delete(&dsc, resource, parsed_input);
},
}
}

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

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

Expand Down
12 changes: 6 additions & 6 deletions dsc/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,18 @@ pub fn get_schema(dsc_type: DscType) -> RootSchema {
///
/// * `json` - The JSON to write
/// * `format` - The format to use
pub fn write_output(json: &str, format: &Option<OutputFormat>) {
pub fn write_output(json: &str, format: Option<&OutputFormat>) {
let mut is_json = true;
let mut output_format = format.clone();
let mut output_format = format;
let mut syntax_color = false;
if std::io::stdout().is_terminal() {
syntax_color = true;
if output_format.is_none() {
output_format = Some(OutputFormat::Yaml);
output_format = Some(&OutputFormat::Yaml);
}
}
else if output_format.is_none() {
output_format = Some(OutputFormat::Json);
output_format = Some(&OutputFormat::Json);
}

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

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

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

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