XAMPPRocky/fluent-templates

Better error handling semanitcs

Open

#34 aperta il 19 mar 2022

Vedi su GitHub
 (1 commento) (0 reazioni) (0 assegnatari)Rust (38 fork)auto 404
enhancementgood first issuehelp wanted

Metriche repository

Star
 (167 star)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

I will say https://github.com/XAMPPRocky/fluent-templates/pull/33 leaves a little ugly way of handling any errors given off by FluentLoader::call as this is my current solution

let fl = FluentLoader::new(loader).with_default_lang(langid!("en-US"));
tera.register_function(
  "fluent",
  move |args: &HashMap<String, serde_json::Value>| match fl.call(&args) {
    Ok(r) => Ok(r),
    Err(e) => match args.get("key").and_then(serde_json::Value::as_str) {
      Some(key) => Ok(key.into()),
      None => Err(e),
    },
  },
);

a solution to this might be to have something like

pub fn call_wrapper(...) -> Result<Json, loader::tera::Error> {
}

pub fn call(...) -> Result<Json, tera::Error> {
  call_wrapper(...)
}

which would change

let fl = FluentLoader::new(loader).with_default_lang(langid!("en-US"));
tera.register_function(
  "fluent",
  move |args: &HashMap<String, serde_json::Value>| match fl.call_wrapper(&args) {
    Ok(s) => Ok(s),
    Err(e) {
      match e {
        NoLangArgument => ...,
        LangArgumentInvalid => ...,
        NoFluentArgument => ...,
        JsonToFluentFail => ...,
        /* new */
        UnknownKey(key) => ...,
      }
    }
  },
);

thoughts?

Guida contributor