XAMPPRocky / fluent-templates

Easily add Fluent to your Rust project.
Apache License 2.0
136 stars 28 forks source link

Seems like impl for tera::Function is not brought into the scope #38

Open bodqhrohro opened 2 years ago

bodqhrohro commented 2 years ago

I try to use the loader with

rocket = { version = "0.5.0-rc.1" }
rocket_dyn_templates = { version = "0.1.0-rc.2", features = ["tera"] }
static_loader! {
  static LOCALES = {
    locales: "./locales",
    fallback_language: "en",
  };
}
...
    .attach(Template::custom(|engines| {
      engines.tera.register_function("fluent", FluentLoader::new(&*LOCALES));
    }))

and get:

error[E0277]: expected a `Fn<(&HashMap<std::string::String, rocket_dyn_templates::tera::Value>,)>` closure, found `FluentLoader<&StaticLoader>`
   --> src/main.rs:43:48
    |
43  |       engines.tera.register_function("fluent", FluentLoader::new(&*LOCALES));
    |                    -----------------           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `Fn<(&HashMap<std::string::String, rocket_dyn_templates::tera::Value>,)>` closure, found `FluentLoader<&StaticLoader>`
    |                    |
    |                    required by a bound introduced by this call
    |
    = help: the trait `for<'r> Fn<(&'r HashMap<std::string::String, rocket_dyn_templates::tera::Value>,)>` is not implemented for `FluentLoader<&StaticLoader>`
    = note: required because of the requirements on the impl of `rocket_dyn_templates::tera::Function` for `FluentLoader<&StaticLoader>`
note: required by a bound in `Tera::register_function`
   --> /home/bodqhrohro/.cargo/registry/src/github.com-1ecc6299db9ec823/tera-1.17.0/src/tera.rs:563:33
    |
563 |     pub fn register_function<F: Function + 'static>(&mut self, name: &str, function: F) {
    |                                 ^^^^^^^^ required by this bound in `Tera::register_function`

tera.rs does not have anything public, is it even imported? Or maybe there is something wrong with the signature?

bodqhrohro commented 2 years ago

Same if I try to invoke it from a custom function:

use fluent_templates::{static_loader, FluentLoader, StaticLoader};
use unic_langid::langid;
use lazy_static::lazy_static;
use std::collections::HashMap;  
use rocket_dyn_templates::tera::{Value, Error, Function};
...
static_loader! {
  static LOCALES = {
    locales: "./locales",
    fallback_language: "en",
  };
}
lazy_static! {
  static ref FLUENT_LOADER: FluentLoader<&'static StaticLoader> = FluentLoader::new(&*LOCALES);
}
pub fn tera_function(args: &HashMap<String, Value>) -> Result<Value, Error> {
  (*FLUENT_LOADER).with_default_lang(langid!("en")).call(args)
}
error[E0599]: the method `call` exists for struct `FluentLoader<&StaticLoader>`, but its trait bounds were not satisfied
  --> src/l10n.rs:39:53
   |
39 |   (*FLUENT_LOADER).with_default_lang(langid!("en")).call(args)
   |                                                     ^^^^ method cannot be called on `FluentLoader<&StaticLoader>` due to unsatisfied trait bounds
   |
  ::: /home/bodqhrohro/.cargo/registry/src/github.com-1ecc6299db9ec823/fluent-templates-0.8.0/src/loader.rs:94:1
   |
94 | pub struct FluentLoader<L> {
   | --------------------------
   | |
   | doesn't satisfy `<_ as FnOnce<(&HashMap<std::string::String, rocket_dyn_templates::tera::Value>,)>>::Output = Result<rocket_dyn_templates::tera::Value, rocket_dyn_templates::tera::Error>`
   | doesn't satisfy `_: Fn<(&HashMap<std::string::String, rocket_dyn_templates::tera::Value>,)>`
   | doesn't satisfy `_: rocket_dyn_templates::tera::Function`
   |
   = note: the following trait bounds were not satisfied:
           `<FluentLoader<&StaticLoader> as FnOnce<(&HashMap<std::string::String, rocket_dyn_templates::tera::Value>,)>>::Output = Result<rocket_dyn_templates::tera::Value, rocket_dyn_templates::tera::Error>`
           which is required by `FluentLoader<&StaticLoader>: rocket_dyn_templates::tera::Function`
           `FluentLoader<&StaticLoader>: Fn<(&HashMap<std::string::String, rocket_dyn_templates::tera::Value>,)>`
           which is required by `FluentLoader<&StaticLoader>: rocket_dyn_templates::tera::Function`
XAMPPRocky commented 2 years ago

Thank you for your issue! You just need to remove the FluentLoader::new part, the loader should be able to be passed directly to the register_function. We should also add a fluent loader impl for &T as well as that would make your code work as is

bodqhrohro commented 2 years ago

the loader should be able to be passed directly to the register_function

Hmm, what do you mean? I tried

    .attach(Template::custom(|engines| {
      engines.tera.register_function("fluent", FluentLoader {
        loader: &*LOCALES,
        default_lang: langid!("en"),
      });
    }))
engines.tera.register_function("fluent", &*LOCALES);
engines.tera.register_function("fluent", *LOCALES);

, and neither of them works, as well as I can't find trait implementations for StaticLoader itself, so I don't get how is that supposed to work.