Rust | supernova [1]

Dec 19, 2018

Adding display of Main Language of Starred repo

Rust’s syntax is definitely something that takes time getting used to. I’ve worked with C and C++ for roughly 4-ish years and descendants of C (like Java), for even longer, around 6 years. Rust is pretty rough to look at and decipher easily - I’ve recently been playing with Elixir and I picked up Ruby a while back and both are easy to read at a glance.

I resolved to do something simpler in supernova this time but more useful than outputting starred repositories into a file - displaying more information through the API. My ultimate aim was to allow the user to grab information on programming languages of their starred repos so that if they wanted to work on or practise a new language, they could look up the repos that use those languages.

Issue:

Solution:

The structure of Rust is something interesting. It takes a lot from functional programming, which besides Elixir, I have not experienced in depth. Rust is structured to avoid as much mutability as possible. Instead of objects, you have definitions for data and an implementation that “creates” it.

The goal of this simpler issue was to move away from tutorials and code snippets online (which turns out to be just as quickly depreciated as Ethereum tutorials actually). Rather, I wanted to learn from my maintainer who picked up on best practises quickly through meticulously researching every piece of code we submitted to the project, and rejecting anything that wasn’t up to best standards.

So this issue was mostly getting used to the Github API and how Rust and Serde, a (de)serialization framework can work together.

The API has quite a bit of options to play with, and I think supernova can really grow into a powerful and useful tool.

Steps I took:

Simply add the value name (in this case it was “language”) from the JSON you get from the Github API to the Repository struct

The data type pulled was an Option<String>, meaning that unlike String, Rust would not be able to cleanly output it via {} tags in an output macro

To explain the above for beginners, it’s similar to a placeholder in C, where if you wish to output a variable, within the printf you would have to make a %datatype placeholder to output the variable.

Observing the surrounding code, I pulled how another Option<String> value (description in this case) and copied how that was handled.

Because that value only relies on Repository, only the implementation of the Repository struct had to be changed to handle it.