Giter VIP home page Giter VIP logo

Comments (4)

zbraniecki avatar zbraniecki commented on May 18, 2024 1

I asked on rust user group - https://users.rust-lang.org/t/l10n-library-api-question/19967

from fluent-rs.

stasm avatar stasm commented on May 18, 2024

I agree with the general direction. Thanks for writing this down.

In our conversation, you mentioned using Option<Result<String, (String, Vec<FluentError>)>> for the return type of format. I have an open question about using Result in our APIs like that. Its purpose is to provide a branching mechanism. But format doesn't really branch on errors, it always returns something which is good to be used.

Using Result can lead to duplicated code:

if let Some(resolved) = bundle.format("key") {
    resolved
         // Handle value.
        .and_then(|value| ...)
        // Handle value again, and possibly errors.
        .or_else(|(value, errs)| ...)
}

If instead format returned Option<(String, Option<Vec<FluentError>>)>>, this code could be simplified to:

if let Some((value, errs)) = bundle.format("key") {
    // Handle errors.
    errs.and_then(|errs| ...);
    // Handle value.
    value
}

I don't know what the established pattern for such scenarios is in Rust. Is it better to have a dedicated branch for the error scenario with fallback (or_else) or to handle errors separately if there are Some?

from fluent-rs.

stasm avatar stasm commented on May 18, 2024

I also realized that using Option might prove to be limiting when we want to differentiate between bundle-level errors. E.g. there could be a different error for when a message is missing, a different one for when the id starts with a - (because terms are private), and yet another one when the message exists but doesn't have a value!

Taking this into account, maybe we should return one of the following types?

  1. Result<Result<String, (String, Vec<ResolveError>)>, BundleError>, or
  2. Result<(String, Vec<ResolveError>), BundleError>, or
  3. Result<(String, Option<Vec<ResolveError>>), BundleError>

from fluent-rs.

zbraniecki avatar zbraniecki commented on May 18, 2024

This has been fixed by 21f3a31

from fluent-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.