speice.io

Home Projects AboutRSS

What I Learned: Porting Dateutil Parser to Rust


  • dtparse
  • rust

Hi. I’m Bradlee.

I’ve mostly been a lurker in Rust for a while, making a couple small contributions here and there. So launching dtparse feels like nice step towards becoming a functioning member of society. But not too much, because then you know people start asking you to pay bills, and ain’t nobody got time for that.

But I built dtparse, and you can read about my thoughts on the process. Or don’t. I won’t tell you what to do with your life (but you should totally keep reading).

Slow down, what?

OK, fine, I guess I should start with why someone would do this.

Dateutil is a Python library for handling dates. The standard library support for time in Python is kinda dope, but there are a lot of extras that go into making it useful beyond just the datetime module. dateutil.parser specifically is code to take all the super-weird time formats people come up with and turn them into something actually useful.

Date/time parsing, it turns out, is just like everything else involving computers and time: it feels like it shouldn’t be that difficult to do, until you try to do it, and you realize that people suck and this is why we can’t we have nice things. But alas, we’ll try and make contemporary art out of the rubble and give it a pretentious name like Time.

A gravel mound

Time

What makes dateutil.parser great is that there’s single function with a single argument that drives what programmers interact with: parse(timestr). It takes in the time as a string, and gives you back a reasonable “look, this is the best anyone can possibly do to make sense of your input” value. It doesn’t expect much of you.

And now it’s in Rust.

Lost in Translation

Having worked at a bulge-bracket bank watching Java programmers try to be Python programmers, I’m admittedly hesitant to publish Python code that’s trying to be Rust. Interestingly, Rust code can actually do a great job of mimicking Python. It’s certainly not idiomatic Rust, but I’ve had better experiences than this guy who attempted the same thing for D. These are the actual take-aways:

When transcribing code, stay as close to the original library as possible. I’m talking about using the same variable names, same access patterns, the whole shebang. It’s way too easy to make a couple of typos, and all of a sudden your code blows up in new and exciting ways. Having a reference manual for verbatim what your code should be means that you don’t spend that long debugging complicated logic, you’re more looking for typos.

Also, don’t use nice Rust things like enums. While one time it worked out OK for me, I also managed to shoot myself in the foot a couple times because dateutil stores AM/PM as a boolean and I mixed up which was true, and which was false (side note: AM is false, PM is true). In general, writing nice code should not be a first-pass priority when you’re just trying to recreate the same functionality.

Exceptions are a pain. Make peace with it. Python code is just allowed to skip stack frames. So when a co-worker told me “Rust is getting try-catch syntax” I properly freaked out. Turns out he’s not quite right, and I’m OK with that. And while dateutil is pretty well-behaved about not skipping multiple stack frames, 130-line try-catch blocks take a while to verify.

As another Python quirk, be very careful about long nested if-elif-else blocks. I used to think that Python’s whitespace was just there to get you to format your code correctly. I think that no longer. It’s way too easy to close a block too early and have incredibly weird issues in the logic. Make sure you use an editor that displays indentation levels so you can keep things straight.

Rust macros are not free. I originally had the main test body wrapped up in a macro using pyo3. It took two minutes to compile. After moving things to a function compile times dropped down to ~5 seconds. Turns out 150 lines * 100 tests = a lot of redundant code to be compiled. My new rule of thumb is that any macros longer than 10-15 lines are actually functions that need to be liberated, man.

Finally, I really miss list comprehensions and dictionary comprehensions. As a quick comparison, see this dateutil code and the implementation in Rust. I probably wrote it wrong, and I’m sorry. Ultimately though, I hope that these comprehensions can be added through macros or syntax extensions. Either way, they’re expressive, save typing, and are super-readable. Let’s get more of that.

Using a young language

Now, Rust is exciting and new, which means that there’s opportunity to make a substantive impact. On more than one occasion though, I’ve had issues navigating the Rust ecosystem.

What I’ll call the “canonical library” is still being built. In Python, if you need datetime parsing, you use dateutil. If you want decimal types, it’s already in the standard library. While I might’ve gotten away with f64, dateutil uses decimals, and I wanted to follow the principle of staying as close to the original library as possible. Thus began my quest to find a decimal library in Rust. What I quickly found was summarized in a comment:

Writing a BigDecimal is easy. Writing a good BigDecimal is hard.

-cmr

In practice, this means that there are at least 4 different implementations available. And that’s a lot of decisions to worry about when all I’m thinking is “why can’t calendar reform be a thing” and I’m forced to dig through a couple different threads to figure out if the library I’m look at is dead or just stable.

And even when the “canonical library” exists, there’s no guarantees that it will be well-maintained. Chrono is the de facto date/time library in Rust, and just released version 0.4.4 like two days ago. Meanwhile, chrono-tz appears to be dead in the water even though there are people happy to help maintain it. I know relatively little about it, but it appears that most of the release process is automated; keeping that up to date should be a no-brainer.

Trial Maintenance Policy

Specifically given “maintenance” being an oft-discussed issue, I’m going to try out the following policy to keep things moving on dtparse:

  1. Issues/PRs needing maintainer feedback will be updated at least weekly. I want to make sure nobody’s blocking on me.

  2. To keep issues/PRs needing contributor feedback moving, I’m going to (kindly) ask the contributor to check in after two weeks, and close the issue without resolution if I hear nothing back after a month.

The second point I think has the potential to be a bit controversial, so I’m happy to receive feedback on that. And if a contributor responds with “hey, still working on it, had a kid and I’m running on 30 seconds of sleep a night,” then first: congratulations on sustaining human life. And second: I don’t mind keeping those requests going indefinitely. I just want to try and balance keeping things moving with giving people the necessary time they need.

I should also note that I’m still getting some best practices in place - CONTRIBUTING and CONTRIBUTORS files need to be added, as well as issue/PR templates. In progress. None of us are perfect.

Roadmap and Conclusion

So if I’ve now built a dateutil-compatible parser, we’re done, right? Of course not! That’s not nearly ambitious enough.

Ultimately, I’d love to have a library that’s capable of parsing everything the Linux date command can do (and not date on OSX, because seriously, BSD coreutils are the worst). I know Rust has a coreutils rewrite going on, and dtparse would potentially be an interesting candidate since it doesn’t bring in a lot of extra dependencies. humantime could help pick up some of the (current) slack in dtparse, so maybe we can share and care with each other?

All in all, I’m mostly hoping that nobody’s already done this and I haven’t spent a bit over a month on redundant code. So if it exists, tell me. I need to know, but be nice about it, because I’m going to take it hard.

And in the mean time, I’m looking forward to building more. Onwards.