<metaname="description"content="I listen to a lot of Drum and Bass music, because it's beautiful music. And there's a particular site, Bassdrive.com that hosts a lot of great content. Specifically, the archives section of the ...">
<pclass="header-date"><ahref="https://bspeice.github.io/author/bradlee-speice.html">Bradlee Speice</a>, Sat 22 October 2016, <ahref="https://bspeice.github.io/category/blog.html">Blog</a></p>
<p>I listen to a lot of Drum and Bass music, because it's beautiful music. And
there's a particular site, <ahref="http://bassdrive.com/">Bassdrive.com</a> that hosts
a lot of great content. Specifically, the
<ahref="http://archives.bassdrivearchive.com/">archives</a> section of the site has a
list of the past shows that you can download and listen to. The issue is, it's
just a <ahref="http://archives.bassdrivearchive.com/6%20-%20Saturday/Electronic%20Warfare%20-%20The%20Overfiend/">giant list of links to download</a>. I'd really like
this in a podcast format to take with me on the road, etc.</p>
<p>So I wrote the <ahref="https://github.com/bspeice/elektricity">elektricity</a> web
application to actually accomplish all that. Whenever you request a feed, it
goes out to Bassdrive, processes all the links on a page, and serves up some
fresh, tasty RSS to satisfy your ears. I hosted it on Heroku using the free
tier because it's really not resource-intensive at all.</p>
<p><strong>The issue so far</strong> is that I keep running out of free tier hours during a
month because my podcasting application likes to have a server scan for new
episodes constantly. Not sure why it's doing that, but I don't have a whole
lot of control over it. It's a phenomenal application otherwise.</p>
<p><strong>My (over-engineered) solution</strong>: Re-write the application using the
<ahref="https://www.rust-lang.org/en-US/">Rust</a> programming language. I'd like to run
this on a small hacker board I own, and doing this in Rust would allow me to
easily cross-compile it. Plus, I've been very interested in the Rust language
for a while and this would be a great opportunity to really learn it well.
The code is available <ahref="https://github.com/bspeice/nutone">here</a> as development
progresses.</p>
<h1>The Setup</h1>
<p>We'll be using the <ahref="http://ironframework.io/">iron</a> library to handle the
server, and <ahref="http://hyper.rs/">hyper</a> to fetch the data we need from elsewhere
on the interwebs. <ahref="http://doc.servo.org/html5ever/index.html">HTML5Ever</a> allows
us to ingest the content that will be coming from Bassdrive, and finally,
output is done with <ahref="http://sunng87.github.io/handlebars-rust/handlebars/index.html">handlebars-rust</a>.</p>
<p>It will ultimately be interesting to see how much more work must be done to
actually get this working over another language like Python. Coming from a
dynamic state of mind it's super easy to just chain stuff together, ship it out,
and call it a day. I think I'm going to end up getting much dirtier trying to
write all of this out.</p>
<h1>Issue 1: Strings</h1>
<p>Strings in Rust are hard. I acknowledge Python can get away with some things
that make strings super easy (and Python 3 has gotten better at cracking down
on some bad cases, <code>str <-> bytes</code> specifically), but Rust is hard.</p>
<p>Let's take for example the <code>404</code> error handler I'm trying to write. The result
should be incredibly simple: All I want is to echo back
<code>Didn't find URL: <url></code>. Shouldn't be that hard right? In Python I'd just do
<spanclass="w"></span><spanclass="nb">Ok</span><spanclass="p">(</span><spanclass="n">Response</span><spanclass="o">::</span><spanclass="n">with</span><spanclass="p">((</span><spanclass="n">status</span><spanclass="o">::</span><spanclass="nb">Ok</span><spanclass="p">,</span><spanclass="w"></span><spanclass="s">"You found the server!"</span><spanclass="p">)))</span><spanclass="w"></span>
<p>Doesn't look too bad right? In fact, it's essentially the same as the Python
version! All we need to do is just send back a string of some form. So, we
look up the documentation for <ahref="http://ironframework.io/doc/iron/request/struct.Request.html"><code>Request</code></a> and see a <code>url</code> field that will contain
<spanclass="w"></span><spanclass="nb">Ok</span><spanclass="p">(</span><spanclass="n">Response</span><spanclass="o">::</span><spanclass="n">with</span><spanclass="p">((</span><spanclass="n">status</span><spanclass="o">::</span><spanclass="nb">Ok</span><spanclass="p">,</span><spanclass="w"></span><spanclass="s">"You found the URL: "</span><spanclass="w"></span><spanclass="o">+</span><spanclass="w"></span><spanclass="n">req</span><spanclass="p">.</span><spanclass="n">url</span><spanclass="p">)))</span><spanclass="w"></span>
<p>OK, what's going on here? Time to start Googling for <ahref="https://www.google.com/#q=concatenate+strings+in+rust">"concatenate strings in Rust"</a>. That's what we
want to do right? Concatenate a static string and the URL.</p>
<p>After Googling, we come across a helpful <ahref="https://doc.rust-lang.org/std/macro.concat!.html"><code>concat!</code></a> macro that looks really nice! Let's try that one:</p>
<spanclass="w"></span><spanclass="nb">Ok</span><spanclass="p">(</span><spanclass="n">Response</span><spanclass="o">::</span><spanclass="n">with</span><spanclass="p">((</span><spanclass="n">status</span><spanclass="o">::</span><spanclass="nb">Ok</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">concat</span><spanclass="o">!</span><spanclass="p">(</span><spanclass="s">"You found the URL: "</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">req</span><spanclass="p">.</span><spanclass="n">url</span><spanclass="p">))))</span><spanclass="w"></span>
<spanclass="w"></span><spanclass="nb">Ok</span><spanclass="p">(</span><spanclass="n">Response</span><spanclass="o">::</span><spanclass="n">with</span><spanclass="p">((</span><spanclass="n">status</span><spanclass="o">::</span><spanclass="nb">Ok</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">format</span><spanclass="o">!</span><spanclass="p">(</span><spanclass="s">"You found the URL: {}"</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">req</span><spanclass="p">.</span><spanclass="n">url</span><spanclass="p">))))</span><spanclass="w"></span>
<h1>Issue 2: Fighting with the borrow checker</h1>
<p>Rust's single coolest feature is how the compiler can guarantee safety in your
program. As long as you don't use <code>unsafe</code> pointers in Rust, you're guaranteed
safety. And not having truly manual memory management is really cool; I'm
totally OK with never having to write <code>malloc()</code> again.</p>
<p>That said, even <ahref="https://doc.rust-lang.org/book/ownership.html">the Rust documentation</a> makes a specific note:</p>
<blockquote>
<p>Many new users to Rust experience something we like to call
‘fighting with the borrow checker’, where the Rust compiler refuses to
compile a program that the author thinks is valid.</p>
</blockquote>
<p>If you have to put it in the documentation, it's not a helpful note:
it's hazing.</p>
<p>So now that we have a handler which works with information from the request, we
want to start making something that looks like an actual web application.
The router provided by <code>iron</code> isn't terribly difficult so I won't cover it.
Instead, the thing that had me stumped for a couple hours was trying to
dynamically create routes.</p>
<p>The unfortunate thing with Rust (in my limited experience at the moment) is that
there is a severe lack of non-trivial examples. Using the router is easy when
you want to give an example of a static function. But how do you you start
working on things that are a bit more complex?</p>
<p>We're going to cover that here. Our first try: creating a function which returns
other functions. This is a principle called <ahref="http://stackoverflow.com/a/36321/1454178">currying</a>. We set up a function that allows us to keep some data in scope
<p>...oookay. I for one, am not going to spend time trying to figure out what's
going on there.</p>
<p>And it is here that I will save the audience many hours of frustrated effort.
At this point, I decided to switch from <code>iron</code> to pure <code>hyper</code> since using
<code>hyper</code> would give me a much simpler API. All I would have to do is build a
function that took two parameters as input, and we're done. That said, it
ultimately posed many more issues because I started getting into a weird fight
with the <code>'static</code><ahref="https://doc.rust-lang.org/book/lifetimes.html">lifetime</a>
and being a Rust newbie I just gave up on trying to understand it.</p>
<p>Instead, we will abandon (mostly) the curried function attempt, and instead
take advantage of something Rust actually intends us to use: <code>struct</code> and
<code>trait</code>.</p>
<p>Remember when I talked about a lack of non-trivial examples on the Internet?
This is what I was talking about. I could only find <em>one</em> example of this
available online, and it was incredibly complex and contained code we honestly
don't need or care about. There was no documentation of how to build routes that
didn't use static functions, etc. But, I'm assuming you don't really care about
my whining, so let's get to it.</p>
<p>The <code>iron</code> documentation mentions the <ahref="http://ironframework.io/doc/iron/middleware/trait.Handler.html"><code>Handler</code></a> trait as being something we can implement.
Does the function signature for that <code>handle()</code> method look familiar? It's what
we've been working with so far.</p>
<p>The principle is that we need to define a new <code>struct</code> to hold our data, then
implement that <code>handle()</code> method to return the result. Something that looks
<spanclass="w"></span><spanclass="n">message</span><spanclass="o">:</span><spanclass="w"></span><spanclass="s">"Is it working yet?"</span><spanclass="w"></span>