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

fix not being able to use Html<T: Display> in templates #108

Closed
wants to merge 1 commit into from
Closed

fix not being able to use Html<T: Display> in templates #108

wants to merge 1 commit into from

Conversation

JojiiOfficial
Copy link

Using Html<T: Display> (theoretically) allows printing raw strings without encoding into a template, so it could be used to work around #1. During trying this out, I encountered following error:

   |
10 | helper().to_html(&mut _ructe_out_)?;
   |          ^^^^^^^ method cannot be called on `ructe::templates::Html<String>` due to unsatisfied trait bounds
   |
  ::: /home/jojii/.cargo/registry/src/github.com-1ecc6299db9ec823/ructe-0.13.4/src/templates/utils.rs:78:1
   |
78 | pub struct Html<T>(pub T);
   | --------------------------
   | |
   | doesn't satisfy `ructe::templates::Html<String>: _utils::ToHtml`
   | doesn't satisfy `ructe::templates::Html<String>: std::fmt::Display`
   |
   = note: the following trait bounds were not satisfied:
           `ructe::templates::Html<String>: std::fmt::Display`
           which is required by `ructe::templates::Html<String>: _utils::ToHtml`
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
1  | use ructe::templates::ToHtml;
   |

By replacing the relative import path in the templates preamble with the absolute module path fixes this issue and one can successfully work around #1


Note: I haven't looked through all of ructes code so I can't assure they are semantically equivalent

@kaj
Copy link
Owner

kaj commented Dec 12, 2021

That should indeed work as is. How does your template look? Do you use the latest release of ructe? How does your building and including of templates look?

Note that ructe should be a build-dependency and not a regular dependency, so I don't think your proposed fix should work.

@JojiiOfficial
Copy link
Author

I don't quite get the reason of the error as well. I'm using the latest version. I've made a small example project causing this error:
reproduce.tar.gz

@kaj
Copy link
Owner

kaj commented Dec 12, 2021

Ah, you have conflicting ToHtml types. Remove the use ructe::templates::Html from line two and it should work. Everything you need at runtime from ructe is included in the generated templates module. As it is, you have a type crate::templates::Html and one type ructe::templates::Html, which the compiler seems as two different types, even if they are line-for-line identical in the source code.

You can (should) also remove ructe from the [dependencies] in Cargo.toml, it is enough to have it in [build-dependencies].

@JojiiOfficial
Copy link
Author

This explains the error. Thanks for the tip 👍🏻

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

Successfully merging this pull request may close these issues.

None yet

2 participants