mirror of
https://github.com/sagiegurari/duckscript
synced 2024-09-29 20:54:34 +00:00
lint rules
This commit is contained in:
parent
6169a5119b
commit
b8411d0964
|
@ -58,7 +58,7 @@ pub(crate) fn expand_by_wrapper(
|
|||
} else if prefix_index == 0 && (next_char == '$' || next_char == '%') {
|
||||
prefix_index = 1;
|
||||
|
||||
single_type = if next_char == '$' { true } else { false };
|
||||
single_type = next_char == '$';
|
||||
} else if prefix_index == 1 && next_char == '{' {
|
||||
found_prefix = true;
|
||||
prefix_index = 0;
|
||||
|
@ -71,9 +71,8 @@ pub(crate) fn expand_by_wrapper(
|
|||
value_string.push(next_char);
|
||||
}
|
||||
} else if next_char == '}' {
|
||||
match variables.get(&key) {
|
||||
Some(variable_value) => value_string.push_str(&variable_value),
|
||||
_ => (),
|
||||
if let Some(variable_value) = variables.get(&key) {
|
||||
value_string.push_str(variable_value)
|
||||
};
|
||||
|
||||
key.clear();
|
||||
|
@ -95,7 +94,7 @@ pub(crate) fn expand_by_wrapper(
|
|||
|
||||
if force_push {
|
||||
value_string.push('\\');
|
||||
} else if key.len() > 0 {
|
||||
} else if !key.is_empty() {
|
||||
if prefix_index > 0 || found_prefix {
|
||||
push_prefix(&mut value_string, single_type, found_prefix);
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ pub fn parse_text_with_source_file(
|
|||
let mut meta_info = InstructionMetaInfo::new();
|
||||
meta_info.source = Some(source_file.to_string());
|
||||
|
||||
parse_lines(&text, meta_info)
|
||||
parse_lines(text, meta_info)
|
||||
}
|
||||
|
||||
fn parse_lines(
|
||||
|
@ -51,18 +51,17 @@ fn parse_lines(
|
|||
for line in lines.lines() {
|
||||
let mut line_meta_info = meta_info.clone();
|
||||
line_meta_info.line = Some(line_number);
|
||||
line_number = line_number + 1;
|
||||
line_number += 1;
|
||||
|
||||
match parse_line(&line, line_meta_info) {
|
||||
match parse_line(line, line_meta_info) {
|
||||
Ok(instruction) => {
|
||||
instructions.push(instruction.clone());
|
||||
|
||||
match instruction.instruction_type {
|
||||
InstructionType::PreProcess(_) => match preprocessor::run(&instruction) {
|
||||
if let InstructionType::PreProcess(_) = instruction.instruction_type {
|
||||
match preprocessor::run(&instruction) {
|
||||
Ok(mut added_instructions) => instructions.append(&mut added_instructions),
|
||||
Err(error) => return Err(error),
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(error) => return Err(error),
|
||||
|
@ -75,7 +74,7 @@ fn parse_lines(
|
|||
fn parse_line(line_text: &str, meta_info: InstructionMetaInfo) -> Result<Instruction, ScriptError> {
|
||||
let trimmed_text = line_text.trim();
|
||||
|
||||
if trimmed_text.is_empty() || trimmed_text.starts_with(&COMMENT_PREFIX_STR) {
|
||||
if trimmed_text.is_empty() || trimmed_text.starts_with(COMMENT_PREFIX_STR) {
|
||||
Ok(Instruction {
|
||||
meta_info,
|
||||
instruction_type: InstructionType::Empty,
|
||||
|
@ -104,7 +103,7 @@ fn parse_pre_process_line(
|
|||
let end_index = line_text.len();
|
||||
for _i in index..end_index {
|
||||
let character = line_text[index];
|
||||
index = index + 1;
|
||||
index += 1;
|
||||
|
||||
if character == ' ' {
|
||||
if !command.is_empty() {
|
||||
|
@ -118,7 +117,7 @@ fn parse_pre_process_line(
|
|||
if command.is_empty() {
|
||||
Err(ScriptError::PreProcessNoCommandFound(meta_info))
|
||||
} else {
|
||||
match parse_arguments(&meta_info, &line_text, index) {
|
||||
match parse_arguments(&meta_info, line_text, index) {
|
||||
Ok(arguments) => {
|
||||
let mut instruction = PreProcessInstruction::new();
|
||||
instruction.command = Some(command);
|
||||
|
@ -151,7 +150,7 @@ fn parse_command_line(
|
|||
// search for label
|
||||
let mut index = start_index;
|
||||
let mut instruction = ScriptInstruction::new();
|
||||
match find_label(&meta_info, &line_text, index) {
|
||||
match find_label(&meta_info, line_text, index) {
|
||||
Ok(output) => {
|
||||
let (next_index, value) = output;
|
||||
index = next_index;
|
||||
|
@ -164,12 +163,12 @@ fn parse_command_line(
|
|||
};
|
||||
|
||||
// find output variable and command
|
||||
index = match find_output_and_command(&meta_info, &line_text, index, &mut instruction) {
|
||||
index = match find_output_and_command(&meta_info, line_text, index, &mut instruction) {
|
||||
Ok(next_index) => next_index,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
|
||||
match parse_arguments(&meta_info, &line_text, index) {
|
||||
match parse_arguments(&meta_info, line_text, index) {
|
||||
Ok(arguments) => {
|
||||
instruction.arguments = arguments;
|
||||
|
||||
|
@ -218,7 +217,7 @@ fn parse_arguments_with_options(
|
|||
|
||||
let mut index = start_index;
|
||||
loop {
|
||||
match parse_next_argument(&meta_info, &line_text, index, control_as_char) {
|
||||
match parse_next_argument(meta_info, line_text, index, control_as_char) {
|
||||
Ok(output) => {
|
||||
let (next_index, argument) = output;
|
||||
|
||||
|
@ -247,8 +246,8 @@ fn parse_next_argument(
|
|||
control_as_char: bool,
|
||||
) -> Result<(usize, Option<String>), ScriptError> {
|
||||
parse_next_value(
|
||||
&meta_info,
|
||||
&line_text,
|
||||
meta_info,
|
||||
line_text,
|
||||
start_index,
|
||||
true,
|
||||
!control_as_char,
|
||||
|
@ -259,7 +258,7 @@ fn parse_next_argument(
|
|||
|
||||
fn parse_next_value(
|
||||
meta_info: &InstructionMetaInfo,
|
||||
line_text: &Vec<char>,
|
||||
line_text: &[char],
|
||||
start_index: usize,
|
||||
allow_quotes: bool,
|
||||
allow_control: bool,
|
||||
|
@ -280,7 +279,7 @@ fn parse_next_value(
|
|||
let mut found_variable_prefix = false;
|
||||
for _i in index..end_index {
|
||||
let character = line_text[index];
|
||||
index = index + 1;
|
||||
index += 1;
|
||||
|
||||
if in_argument {
|
||||
if in_control {
|
||||
|
@ -327,7 +326,7 @@ fn parse_next_value(
|
|||
|| (stop_on_equals && character == '='))
|
||||
{
|
||||
if character == ' ' || character == '=' {
|
||||
index = index - 1;
|
||||
index -= 1;
|
||||
} else if character == '#' {
|
||||
index = end_index;
|
||||
}
|
||||
|
@ -394,16 +393,15 @@ fn find_label(
|
|||
let mut index = start_index;
|
||||
for _i in index..end_index {
|
||||
let character = line_text[index];
|
||||
index = index + 1;
|
||||
index += 1;
|
||||
|
||||
if character == LABEL_PREFIX {
|
||||
match parse_next_value(&meta_info, &line_text, index, false, false, false, false) {
|
||||
match parse_next_value(meta_info, line_text, index, false, false, false, false) {
|
||||
Ok(output) => {
|
||||
let (next_index, value) = output;
|
||||
index = next_index;
|
||||
|
||||
match value {
|
||||
Some(label_value) => {
|
||||
if let Some(label_value) = value {
|
||||
if label_value.is_empty() {
|
||||
return Err(ScriptError::EmptyLabel(meta_info.clone()));
|
||||
}
|
||||
|
@ -413,8 +411,6 @@ fn find_label(
|
|||
text.push_str(&label_value);
|
||||
|
||||
label = Some(text);
|
||||
}
|
||||
None => (),
|
||||
};
|
||||
|
||||
break;
|
||||
|
@ -422,7 +418,7 @@ fn find_label(
|
|||
Err(error) => return Err(error),
|
||||
};
|
||||
} else if character != ' ' {
|
||||
index = index - 1;
|
||||
index -= 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -437,15 +433,7 @@ fn find_output_and_command(
|
|||
start_index: usize,
|
||||
instruction: &mut ScriptInstruction,
|
||||
) -> Result<usize, ScriptError> {
|
||||
match parse_next_value(
|
||||
&meta_info,
|
||||
&line_text,
|
||||
start_index,
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
) {
|
||||
match parse_next_value(meta_info, line_text, start_index, false, false, true, false) {
|
||||
Ok(output) => {
|
||||
let (next_index, value) = output;
|
||||
|
||||
|
@ -456,7 +444,7 @@ fn find_output_and_command(
|
|||
let end_index = line_text.len();
|
||||
for _i in index..end_index {
|
||||
let character = line_text[index];
|
||||
index = index + 1;
|
||||
index += 1;
|
||||
|
||||
if character != ' ' {
|
||||
if character == '=' {
|
||||
|
@ -468,9 +456,8 @@ fn find_output_and_command(
|
|||
}
|
||||
|
||||
if instruction.output.is_some() {
|
||||
match parse_next_value(
|
||||
&meta_info, &line_text, index, false, false, false, false,
|
||||
) {
|
||||
match parse_next_value(meta_info, line_text, index, false, false, false, false)
|
||||
{
|
||||
Ok(output) => {
|
||||
let (next_index, value) = output;
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ pub(crate) fn run(
|
|||
match path_buffer.parent() {
|
||||
Some(path) => {
|
||||
let mut parent_path_buffer = path.to_path_buf();
|
||||
parent_path_buffer.push(&argument);
|
||||
parent_path_buffer.push(argument);
|
||||
|
||||
let full_path_buffer = match parent_path_buffer.canonicalize() {
|
||||
Ok(new_buffer) => new_buffer,
|
||||
|
|
|
@ -97,20 +97,13 @@ fn create_runtime(instructions: Vec<Instruction>, context: Context) -> Runtime {
|
|||
|
||||
let mut line = 0;
|
||||
for instruction in &instructions {
|
||||
match &instruction.instruction_type {
|
||||
InstructionType::Script(ref value) => {
|
||||
match value.label {
|
||||
Some(ref label) => {
|
||||
if let InstructionType::Script(ref value) = &instruction.instruction_type {
|
||||
if let Some(ref label) = value.label {
|
||||
runtime.label_to_line.insert(label.to_string(), line);
|
||||
()
|
||||
}
|
||||
None => (),
|
||||
};
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
|
||||
line = line + 1;
|
||||
line += 1;
|
||||
}
|
||||
|
||||
runtime.instructions = Some(instructions);
|
||||
|
@ -145,7 +138,7 @@ fn run_instructions(
|
|||
&mut runtime.context.commands,
|
||||
&mut runtime.context.variables,
|
||||
&mut state,
|
||||
&instructions,
|
||||
instructions,
|
||||
instruction,
|
||||
line,
|
||||
);
|
||||
|
@ -185,23 +178,18 @@ fn run_instructions(
|
|||
|
||||
let post_error_line = line + 1;
|
||||
|
||||
match run_on_error_instruction(
|
||||
if let Err(error) = run_on_error_instruction(
|
||||
&mut runtime.context.commands,
|
||||
&mut runtime.context.variables,
|
||||
&mut state,
|
||||
&instructions,
|
||||
instructions,
|
||||
error,
|
||||
meta_info.clone(),
|
||||
) {
|
||||
Err(error) => {
|
||||
return Err(ScriptError::Runtime(error, Some(meta_info.clone())));
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
|
||||
line = post_error_line;
|
||||
|
||||
()
|
||||
}
|
||||
CommandResult::Crash(error) => {
|
||||
let script_error = ScriptError::Runtime(error, Some(meta_info));
|
||||
|
@ -215,9 +203,7 @@ fn run_instructions(
|
|||
CommandResult::Continue(output) => {
|
||||
update_output(&mut runtime.context.variables, output_variable, output);
|
||||
|
||||
line = line + 1;
|
||||
|
||||
()
|
||||
line += 1;
|
||||
}
|
||||
CommandResult::GoTo(output, goto_value) => {
|
||||
update_output(&mut runtime.context.variables, output_variable, output);
|
||||
|
@ -314,26 +300,24 @@ pub fn run_instruction(
|
|||
Some(ref command) => match commands.get_for_use(command) {
|
||||
Some(command_instance) => {
|
||||
let command_arguments = bind_command_arguments(
|
||||
&variables,
|
||||
&script_instruction,
|
||||
variables,
|
||||
script_instruction,
|
||||
&instruction.meta_info,
|
||||
);
|
||||
|
||||
let command_result = if command_instance.requires_context() {
|
||||
if command_instance.requires_context() {
|
||||
command_instance.run_with_context(
|
||||
command_arguments,
|
||||
state,
|
||||
variables,
|
||||
output_variable.clone(),
|
||||
&instructions,
|
||||
instructions,
|
||||
commands,
|
||||
line,
|
||||
)
|
||||
} else {
|
||||
command_instance.run(command_arguments)
|
||||
};
|
||||
|
||||
command_result
|
||||
}
|
||||
}
|
||||
None => CommandResult::Crash(format!("Command: {} not found.", &command)),
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue