Giter Club home page Giter Club logo

Comments (4)

Geal avatar Geal commented on August 25, 2024

I made it that way to match Parsec's IResult type, although passing a closure into Incomplete proved too difficult for Rust.
I am not sure using Result would be better, since depending on the context, Incomplete can be an error case or a correct case.
If you need more helper functions, though, I could add them, that's not a problem.

from nom.

kgv avatar kgv commented on August 25, 2024

understood.

As for "more helper functions":
In examples i found only one case that needs it (this).

  match keys_and_values_aggregator(input) {
    IResult::Done(i,tuple_vec) => {
      for &(k,v) in tuple_vec.iter() {
        h.insert(k, v);
      }
      IResult::Done(i, h)
    },
    IResult::Incomplete(a)     => IResult::Incomplete(a),
    IResult::Error(a)          => IResult::Error(a)
  }

But in this code we can remove the explicit mapping (like this: IResult::Incomplete(a) => IResult::Incomplete(a),) using map! macro?:

named!(keys_and_values_aggregator<&[u8], Vec<(&str,&str)> >, many0!(key_value));

fn keys_and_values(input: Vec<(&str,&str)>) -> HashMap<&str, &str> {
    let mut h: HashMap<&str, &str> = HashMap::new();
    for &(k,v) in input.iter() {
        h.insert(k, v);
    }
    h
}

named!(keys_and_values_hashmap<&[u8], HashMap<&str, &str> >, map!(keys_and_values_aggregator, keys_and_values));

In short, helper functions enough. Just instead of the standard functionality necessary to use helper functions.

from nom.

Geal avatar Geal commented on August 25, 2024

I see what you mean. I wrote the INI parser a few months ago, and there was not as much tooling (and I did not know I could do if let IResult::Done(i,o) = ...).

The map! and flat_map! macros are useful for those cases. At one point, I had implemented those as a trait on IResult, but the generic types and the lifetimes made the code impossible to maintain.

from nom.

kgv avatar kgv commented on August 25, 2024

Thanks for the answer.

from nom.

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.