Code Monkey home page Code Monkey logo

reformation's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

reformation's Issues

Allow injection of lazily parsed parts for structs

Parsing complex struct could negatively impact performance.

Lets take a look at example:

Assuming we have to parse logs of following format:

[{datetime}:{process_uuid}] {message}

With typical usage: Filter out interesting messages, then process time deltas between then, aggregating by process uuid.

Where are two ways to define struct to parse such a line:

Fully parsing:

#[derive(Reformation)]
#[reformation("[{header}] {message}", no_regex=true)]
struct LogLine {
    header: Header,
    message: Message,
}

#[derive(Reformation)]
#[reformation("{datetime}:{process_uuid}")]
struct Header {
    datetime: CustomDatetime,
    process_uuid: UUID,
}

Or only parsing message at first

#[reformation("[{header}] {message}", no_regex=true)]
struct LogLine {
    #[reformation("[^\[]*")]
    header: String,
    message: Message,
}

Problem with first one is that it is less performant then the second, if we need too look at header only 1 out of 10 lines.

Problem with second one is that types become harder to follow, and code wanting to use header would need to execute Header::parse manually.

Combining the two

The way I see solution to this problem, is to introduce wrapper LazyParse, which will allow to define struct like this:

#[derive(Reformation)]
#[reformation("[{header}] {message}", no_regex=true)]
struct LogLine {
    #[reformation("[^\[]*")]
    header: LazyParse<Header>,
    message: Message,
}

Internally, LazyParse would be implemented as

enum LazyParse<T> {
    Raw(String),
    Parsed(T),
}

And parsing from string would only create raw version, while actual value would be parsed only then first requested.

Drawbacks and extra notes

  • Accessing value could return error
  • To support in place parsing, where should be LazyParse counterpart that contains &'a str

Enum support?

Correct me if I'm wrong, but it doesn't seem that this crate supports enums in any way. Is this planned on being supported?

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.