Skip to content

Commit

Permalink
Merge pull request #160 from yarn-slinger/lints
Browse files Browse the repository at this point in the history
Make Clippy and Rustfmt happy
  • Loading branch information
janhohenheim authored Jan 12, 2024
2 parents 1616b72 + 496b08e commit 72a38d6
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ fn continue_runtime(
let Some(option) = options.into_iter().find(|o| o.id == option) else {
let expected_options = last_options
.values()
.flat_map(|options|
options
.iter()
.map(|option| option.id.to_string()))
.flat_map(|options| options.iter().map(|option| option.id.to_string()))
.collect::<Vec<_>>()
.join(", ");
bail!("Dialogue options does not contain selected option. Expected one of [{expected_options}], but found {option}");
Expand Down
11 changes: 7 additions & 4 deletions crates/bevy_plugin/src/localization/line_id_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ fn handle_yarn_file_events_outside_development(
) {
for event in events.iter() {
let AssetEvent::Modified { handle } = event else {
continue;
};
continue;
};
if !(yarn_files_being_loaded.0.contains(handle) || project.yarn_files.contains(handle)) {
continue;
}
Expand Down Expand Up @@ -90,7 +90,9 @@ fn handle_yarn_file_events(
}
last_recompiled_yarn_file.replace(yarn_file.clone());
for mut dialogue_runner in dialogue_runners.iter_mut() {
dialogue_runner.text_provider.extend_base_string_table(yarn_file.string_table.clone());
dialogue_runner
.text_provider
.extend_base_string_table(yarn_file.string_table.clone());
}
added_tags.remove(handle);
recompilation_needed = true;
Expand Down Expand Up @@ -149,7 +151,8 @@ fn add_tags_to_lines(yarn_file: &YarnFile) -> YarnCompilerResult<Option<String>>
let existing_tags = yarn_file
.string_table
.iter()
.filter_map(|(key, string_info)| (!string_info.is_implicit_tag).then(|| key.clone()))
.filter(|(_, string_info)| !string_info.is_implicit_tag)
.map(|(key, _)| key.clone())
.collect();
YarnCompiler::add_tags_to_lines(yarn_file.file.source.clone(), existing_tags)
}
4 changes: 2 additions & 2 deletions crates/bevy_plugin/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ impl YarnProject {
.get(node_name)?
.headers
.iter()
.fold(HashMap::new(), |mut map, header| {
.fold(HashMap::new(), |mut map: HashMap<_, Vec<_>>, header| {
map.entry(header.key.as_str())
.or_insert_with(Vec::new)
.or_default()
.push(header.value.as_str());
map
})
Expand Down
16 changes: 14 additions & 2 deletions crates/bevy_plugin/src/project/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ fn recompile_loaded_yarn_files(
let Some(mut yarn_project) = yarn_project else {
return Ok(());
};
let Some(compilation) = compile_yarn_files(&yarn_project.yarn_files, &yarn_files, yarn_project.localizations.as_ref(), yarn_project.development_file_generation)? else {
let Some(compilation) = compile_yarn_files(
&yarn_project.yarn_files,
&yarn_files,
yarn_project.localizations.as_ref(),
yarn_project.development_file_generation,
)?
else {
return Ok(());
};
let metadata = compilation
Expand Down Expand Up @@ -175,7 +181,13 @@ fn compile_loaded_yarn_files(
.unwrap()
.as_ref();
let development_file_generation = yarn_project_config_to_load.development_file_generation;
let Some(compilation) = compile_yarn_files(&yarn_files_being_loaded.0, &yarn_files, localizations, development_file_generation)? else {
let Some(compilation) = compile_yarn_files(
&yarn_files_being_loaded.0,
&yarn_files,
localizations,
development_file_generation,
)?
else {
return Ok(());
};
let file_count = yarn_files_being_loaded.0.len();
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_plugin/tests/test_strings_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fn appends_to_pre_existing_strings_file() -> anyhow::Result<()> {
let original_strings_path = project_root_path().join("assets/dialogue/de-CH.strings.csv");
let strings_file_path = dir.path().join("dialogue/de-CH.strings.csv");
fs::create_dir_all(strings_file_path.parent().unwrap())?;
fs::copy(&original_strings_path, &strings_file_path)?;
fs::copy(original_strings_path, &strings_file_path)?;
let original_strings_file_source = fs::read_to_string(&strings_file_path)?;
let original_strings_file_line_ids: Vec<_> = original_strings_file_source
.lines()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub(crate) fn add_initial_value_registrations(
// of the inputs, and create an initial value registration for
// it.
let Ok(compilation) = state.result.as_mut().unwrap().as_mut() else {
return state;
};
return state;
};

let declarations = state
.known_variable_declarations
Expand All @@ -19,11 +19,13 @@ pub(crate) fn add_initial_value_registrations(

for declaration in declarations {
let Some(default_value) = declaration.default_value.clone() else {
state.diagnostics.push(
Diagnostic::from_message(
format!("Variable declaration {} (type {}) has a null default value. This is not allowed.", declaration.name, declaration.r#type.format())));
continue;
};
state.diagnostics.push(Diagnostic::from_message(format!(
"Variable declaration {} (type {}) has a null default value. This is not allowed.",
declaration.name,
declaration.r#type.format()
)));
continue;
};
if let Some(ref mut program) = compilation.program {
let value = match &declaration.r#type {
Type::String => Operand::from(String::try_from(default_value).unwrap()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ pub(crate) fn validate_unique_node_names(
})
});

let nodes_by_name =
nodes_with_names.fold(HashMap::new(), |mut map, (name, header_context, file)| {
map.entry(name)
.or_insert_with(Vec::new)
.push((header_context, file));
let nodes_by_name = nodes_with_names.fold(
HashMap::new(),
|mut map: HashMap<_, Vec<_>>, (name, header_context, file)| {
map.entry(name).or_default().push((header_context, file));
map
});
},
);

// Find groups of nodes with the same name and generate diagnostics
// for each
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Compiler {

/// Extends the Yarn function library with the given [`Library`]. The standard library is only added if this is called with [`Library::standard_library`].
pub fn extend_library(&mut self, library: Library) -> &mut Self {
self.library.extend(library.into_iter());
self.library.extend(library);
self
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl LastLineBeforeOptionsVisitor {
}

let Some(shortcut_option_statement) = statement.shortcut_option_statement() else {
// we aren't an option, keep moving
// we aren't an option, keep moving
continue;
};
// we need to visit the option in case it has embedded statements
Expand Down
12 changes: 5 additions & 7 deletions crates/compiler/src/visitors/type_check_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ impl<'input> YarnSpinnerParserVisitorCompat<'input> for TypeCheckVisitor<'input>
let hint = self.hints.get(ctx).cloned();
let function_type = if let Some(function_declaration) = function_declaration {
let Type::Function(mut function_type) = function_declaration.r#type.clone() else {
unreachable!("Internal error: function declaration is not of type Function. This is a bug. Please report it at https://github.com/yarn-slinger/yarn_slinger/issues/new")
unreachable!("Internal error: function declaration is not of type Function. This is a bug. Please report it at https://github.com/yarn-slinger/yarn_slinger/issues/new")
};

// we have an existing function but its undefined
Expand Down Expand Up @@ -387,12 +387,10 @@ impl<'input> YarnSpinnerParserVisitorCompat<'input> for TypeCheckVisitor<'input>
fn visit_variable(&mut self, ctx: &VariableContext<'input>) -> Self::Return {
// The type of the value depends on the declared type of the
// variable
let Some(var_id) = ctx.get_token(yarnspinnerlexer::VAR_ID, 0) else {
// We don't have a variable name for this Variable context.
// The parser will have generated an error for us in an
// earlier stage; here, we'll bail out.
return None
};
// the parser will have generated an error for us in an
// earlier stage if we don't have a variable name for
// this Variable context; here, we'll bail out.
let var_id = ctx.get_token(yarnspinnerlexer::VAR_ID, 0)?;
let name = var_id.get_text();
if let Some(declaration) = self.declarations().find(|decl| decl.name == name) {
return Some(declaration.r#type.clone());
Expand Down
16 changes: 12 additions & 4 deletions crates/compiler/src/visitors/type_check_visitor/check_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,18 @@ impl<'input> TypeCheckVisitor<'input> {
// annoyingly the function will already have an implicit definition created for it
// we will have to strip that out and add in a new one with the new return type
for term in terms {
let Term::Expression(expression) = term else { continue; };
let ExpressionContextAll::ExpValueContext(value_context) = expression.as_ref() else { continue; };
let Some(value) = value_context.value() else { continue; };
let ValueContextAll::ValueFuncContext(func_context) = value.as_ref() else { continue; };
let Term::Expression(expression) = term else {
continue;
};
let ExpressionContextAll::ExpValueContext(value_context) = expression.as_ref() else {
continue;
};
let Some(value) = value_context.value() else {
continue;
};
let ValueContextAll::ValueFuncContext(func_context) = value.as_ref() else {
continue;
};

let id = func_context
.function_call()
Expand Down
2 changes: 1 addition & 1 deletion crates/example_dialogue_view/src/option_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn select_option(
let mut selection = None;
let key_to_option: HashMap<_, _> = NUMBER_KEYS
.into_iter()
.zip(NUMPAD_KEYS.into_iter())
.zip(NUMPAD_KEYS)
.zip(option_selection.options.iter().map(|option| option.id))
.collect();
for ((num_key, numpad_key), option) in key_to_option {
Expand Down
16 changes: 9 additions & 7 deletions crates/runtime/src/markup/line_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,14 @@ impl LineParser {
}
if let Some(prop) = marker.properties.get(TRIM_WHITESPACE_PROPERTY) {
let MarkupValue::Bool(trim_whitespace) = prop else {
return Err(MarkupParseError::TrimWhitespaceAttributeIsNotBoolean {
input: self.input.clone(),
name: marker.name,
position: self.position,
type_: prop.type_name().to_lowercase(),
});
return Err(
MarkupParseError::TrimWhitespaceAttributeIsNotBoolean {
input: self.input.clone(),
name: marker.name,
position: self.position,
type_: prop.type_name().to_lowercase(),
},
);
};
trim_whitespace_if_able = *trim_whitespace;
}
Expand Down Expand Up @@ -637,7 +639,7 @@ impl LineParser {
fn peek_numeric(&mut self) -> Result<bool> {
self.consume_whitespace()?;
let Some(next) = self.peek_next() else {
return Ok(false)
return Ok(false);
};
Ok(next.is_ascii_digit())
}
Expand Down
4 changes: 1 addition & 3 deletions crates/runtime/src/virtual_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ impl VirtualMachine {
let node_name = node_name.into();
debug!("Loading node \"{node_name}\"");
let Some(current_node) = self.get_node_from_name(&node_name) else {
return Err(DialogueError::InvalidNode{
node_name
});
return Err(DialogueError::InvalidNode { node_name });
};
self.current_node = Some(current_node.clone());

Expand Down
18 changes: 9 additions & 9 deletions crates/yarn_slinger/tests/test_base/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ impl TestBase {
match event {
DialogueEvent::Line(line) => {
println!("Line: {}", line.text);
let Some(test_plan) = self.test_plan.as_mut() else {
continue;
};
let Some(test_plan) = self.test_plan.as_mut() else {
continue;
};
test_plan.next();

assert_eq!(
Expand Down Expand Up @@ -182,8 +182,8 @@ impl TestBase {
println!(" - {} (available: {})", option.line, option.enabled);
}
let Some(test_plan) = self.test_plan.as_mut() else {
continue;
};
continue;
};

test_plan.next();
assert_eq!(
Expand All @@ -210,8 +210,8 @@ impl TestBase {
DialogueEvent::Command(command) => {
println!("Command: {}", command.raw);
let Some(test_plan) = self.test_plan.as_mut() else {
continue;
};
continue;
};
test_plan.next();
assert_eq!(
ExpectedStepType::Command,
Expand All @@ -236,8 +236,8 @@ impl TestBase {
DialogueEvent::LineHints(_) => {}
DialogueEvent::DialogueComplete => {
let Some(test_plan) = self.test_plan.as_mut() else {
continue;
};
continue;
};
test_plan.next();
assert_eq!(
ExpectedStepType::Stop,
Expand Down
2 changes: 1 addition & 1 deletion demo/src/visual_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(crate) fn rotate_sprite(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
for (mut transform, material, mut rotator) in rotators.iter_mut() {
let RotationPhase::ChangingSprite{change, sprite}= rotator.deref_mut() else {
let RotationPhase::ChangingSprite { change, sprite } = rotator.deref_mut() else {
continue;
};
let output = change.elastic(1.3);
Expand Down

0 comments on commit 72a38d6

Please sign in to comment.