The error message:
non-primitive cast: `&Option<String>` as `&bool`
indicates that the template is attempting to evaluate an Option<String> (which can be Some(String) or None) as a bool. This likely stems from the {% if let Some(msg) = error_message %} syntax, which isn't supported by the templating engine you're using (such as Tera in Rust).
Solution
Remove the if let syntax and use a simple {% if %} statement to check if error_message exists. Then, display the error_message directly within the block. Here's how you can adjust your template:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Roblox Friendship Checker</title>
<link rel="stylesheet" href="/static/styles.css">
</head>
<body>
<div class="container">
<h1>Roblox Friendship Checker</h1>
{% if error_message %}
<div class="error">{{ error_message }}</div>
{% endif %}
<form action="/check_friendship" method="post">
<label for="user_input">Enter User ID or Username:</label>
<input type="text" id="user_input" name="user_input" required><br>
<label for="target_user_input">Enter Target User ID or Username:</label>
<input type="text" id="target_user_input" name="target_user_input" value="Roblox" required><br>
<input type="submit" value="Check Friendship">
</form>
</div>
</body>
</html>
Explanation
-
Remove if let Syntax: The {% if let Some(msg) = error_message %} syntax is not supported by most templating engines. Instead, use a straightforward {% if %} statement to check for the presence of error_message.
-
Check for Existence: {% if error_message %} checks whether error_message is present (i.e., not None). If it exists, the block inside {% if %} and {% endif %} will be rendered.
-
Display the Message: Within the {% if %} block, use {{ error_message }} to display the actual error message.
Additional Tips
-
Ensure error_message is Passed Correctly: Make sure that when rendering the template from your server-side code, error_message is correctly passed as part of the context. For example, in Rust using Tera:
let mut context = tera::Context::new();
if let Some(err) = some_error_condition {
context.insert("error_message", &err.to_string());
}
-
Validate Templating Syntax: Different templating engines have slightly different syntax rules. Always refer to the documentation of the engine you're using to ensure compatibility.
By making these adjustments, your template should correctly handle and display error messages without type casting issues.