Skip to content
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

Can I pass None as arg to a macro? #864

Closed
KaiyuanMa opened this issue Sep 18, 2023 · 4 comments
Closed

Can I pass None as arg to a macro? #864

KaiyuanMa opened this issue Sep 18, 2023 · 4 comments

Comments

@KaiyuanMa
Copy link

When I pass "None" to input, I get a "type annotations needed cannot infer type" error

{% macro input(input_name, input_type, input_required) %}
    {% let name %}
    {% let local_type %}
    {% let required %}

    {% if input_name.is_some() %}
      {% let name = input_name.unwrap().to_string() %}
    {% else %}
      {% let name = String::from("asd") %}
    {% endif %}

    {% if input_type.is_some() %}
      {% let local_type = input_type.unwrap().to_string() %}
    {% else %}
      {% let local_type = String::from("asd") %}
    {% endif %}

    {% if input_required.is_some() %}
      {% let required = input_required.unwrap() %}
    {% else %}
      {% let required = true %}
    {% endif %}

    <input
        name="{{name}}"
        type="{{local_type}}"
        class="rounded h-[1.8em] px-2 mb-2 mt-1 text-sm"
    />
{% endmacro %}

When I do {% call input(Some("email"), Some("email"), Some(true)) %} It works alright

but when I do {% call input(None, Some("email"), Some(true)) %} It yalled at me :(

@Kijewski
Copy link
Contributor

Could you please copy the exact output of the compiler? In my superficial test the code compiled fine in both the last released version and the current development version.

@KaiyuanMa
Copy link
Author

KaiyuanMa commented Sep 27, 2023

Could you please copy the exact output of the compiler? In my superficial test the code compiled fine in both the last released version and the current development version.

I am using the input component like this,

{%- import "components/input.html" as input -%}
{% extends "base.html" %}


{% block content %}
  {% call input::input(None, None, None) %}
  {% call input::input(None, None, None) %}
{% endblock %}

I get red underline in the Template derive in the struct

#[derive(Default, Template)]
#[template(path = "routes/login.html")]
pub struct Login<'a> {
    pub email_error_msg: &'a str,
    pub form_error_msg: &'a str,
}

type annotations needed
cannot infer typerustc[Click for full compiler diagnostic]
askama_derive
proc_macro Template

When I cargo run i get this error

error[E0282]: type annotations needed
  --> src/templates.rs:22:19
   |
22 | #[derive(Default, Template)]
   |                   ^^^^^^^^ cannot infer type
   |
   = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

but when I change both input back to input(Some("email"), Some("email"), Some(true)) all errors go away
I am using version 0.12.0

@KaiyuanMa
Copy link
Author

Update:
I must have done something wrong, or the original code is just too messy. Anyway here is the updated and working version.

{% macro input(input_name, input_type, input_required) %}
    <input
        name="{{input_name.unwrap_or("")}}"
        type="{{input_type.unwrap_or("")}}"
        required="{{input_required.unwrap_or(false)}}"
        class="rounded h-[1.8em] px-2 mb-2 mt-1 text-sm"
    />
{% endmacro %}

@jh9aea
Copy link

jh9aea commented Nov 7, 2023

I have the same issue

  {% macro all(items) %}
  {% for item in items.unwrap() if items.is_some() %}
    <div>{{ item.name }}</div>
  {% endfor %}
  {% endmacro %}
{% import("items.html") as items %}
{% call  items::all(None) %}
error[E0282]: type annotations needed
  --> main/src/main.rs:28:10
   |
28 | #[derive(Template)]
   |          ^^^^^^^^ cannot infer type
   |
   = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)
// unfortunately i can`t do this
{% import("items.html") as items %}
{% call items::all(None::<crate::Item>) %}
// or this
{% macro all(items: Option<Vec<crate::Item>>) %}...

so i don`t know how to solve this problem
mb you could advise me something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants