24 days of Rust - clap

clap is a fantastic Rust library for Command Line Argument Parsing. It's both easy to use and powerful - in the spirit of Rust philosophy - you get what you pay for. Simple CLI options are simple to define, while complex schemes (think git level of complex) are absolutely doable.

clap is also one of the best examples of what I would call developer marketing in the Rust community. It has a beautiful and informative homepage, an extensive README (including changelog - see note below), a bunch of good examples, even video tutorials! Hats off to Kevin and all clap contributors, you're doing a great job!

Note: Rust crate authors, please, please add changelogs/release notes to your libraries. Coming from Python where it's customary, it struck me that a lot of libraries do not document their changes aside from the commit log. (Oops, I'm guilty of this myself...)

Czytaj dalej »
Napisane 12 grudnia 2016

24 days of Rust - nom, part 2

We learned the basic concepts of nom yesterday when we wrote a parser for HTTP headers. HTTP is by its nature a text protocol. nom however always works on bytes (byte array slices, denoted in Rust with &[u8]). This makes it perfectly suitable for parsing binary data as well. There's already a selection of parsers using nom for binary formats such as Redis dump files, MySQL protocol or tar archives. Today we are going to build a simplified WebSocket frame parser.

Czytaj dalej »
Napisane 11 grudnia 2016

24 days of Rust - nom, part 1

It's entirely possible that you're walking a happy road of a programmer who never had to write a parser by hand. That's not my case unfortunately. I remember incomprehensible indexing of hideous arrays of characters, a maddening cascade of unmaintainable if-else statements, and futile, indescribable attempts to abstract away parts of this unspeakable monstrosity.

If the above paragraph was hard to parse, good! Putting some Lovecraftian adjectives into a description of events can be a way of coping with terrible experiences. Those dark, eldritch (sorry, couldn't resist one more) days are fortunately over. With parser combinators we can write composable and fast parsers. Rust adds another adjective here: safe. nom is a parser combinator library that works by generating parsing code at compile time with a bunch of macros. It also tries to avoid allocation and work through input bytes without copying.

I decided to split nom article into two parts. Today we're focused on parsing text (well, bytes that contain text), while the next article in the series will cover binary parsing.

This is my first hands-on experience with parser combinators - I'm learning nom as I write this. Feel free to let me know if the examples here could be more nom-idiomatic.

Czytaj dalej »
Napisane 9 grudnia 2016