Battery packs: Let's talk about crates, baby

MeetingsBrowser2 pts0 comments

Battery packs: Let's talk about crates, baby · baby stepsThis blog post describes an idea I&rsquo;ve been kicking around called battery packs . Battery packs are a curated set of crates arranged around a common theme. For example, there&rsquo;s a CLI battery pack that has everything you need to build a great CLI, an opinionated pack for creating a backend web service, and one for embedded development (based on the Embedded Working Group&rsquo;s Awesome Rust repository). We&rsquo;ve also got some smaller ones, such as the error-handling battery pack that shows how to handle errors in Rust. But this is just the beginning – a key part of the battery pack design is that anybody can create one.<br>Battery packs are meant to address one of the most common things I hear from new Rust adopters. Everyone loves the wealth of high-quality crates available on crates.io. And everyone hates having to spend a bunch of time researching and comparing alternatives. Battery packs can serve as a good set of default choices. And they don&rsquo;t lock you in. At heart, they&rsquo;re basically just a list of recommended crates, so you can always swap something out if you find an alternative.<br>We&rsquo;ve got a prototype of the battery pack tool working today, so you can try it out if you&rsquo;re curious. Just run cargo install cargo-bp and then try a few commands! For example,<br>> cargo bp list

will show you the set of available battery packs, based on a crates.io search (as I&rsquo;ll explain below, a battery pack is itself packaged and distributed as a crate, but not one that you take a direct dependency on). And cargo bp add will add batteries from a battery pack into your crate, so e.g.<br>> cargo bp add cli

would let you select and add common CLI libraries. If you want to see a more involved demo, try out cargo bp add embedded, which is derived from the Awesome Embedded Rust repository.<br>Let&rsquo;s talk about you and me<br>One of the key ideas from battery packs is that anybody can publish one . They are just a crate named X-battery-pack; the dependencies of that crate are your recommendations. Features are designations of common sets of crates frequently used together. The examples are your templates. And so forth.<br>Letting anybody create a battery pack is in contrast to the previous ideas for an &ldquo;extended standard library for Rust&rdquo;1, and it is intended to address some of Rust&rsquo;s unique challenges. For one thing, it lets people publish battery packs that are tailored to specific requirements. For example, the CLI and backend service battery packs are targeting a &ldquo;typical computer&rdquo;. But I could imagine the Rust embedded working group publishing a battery pack with libraries focused on no-std and binary size optimization.<br>Being open-ended also addresses the &ldquo;who decides?&rdquo; question. To my mind, the best people to recommend what libraries you ought to use are other people building systems like yours . This is why I mentioned the Embedded Working Group publishing an Embedded battery pack, for example, as I think they are clearly a set of people who know their space well. But even within the embedded space there are yet smaller groups, and I imagine that sometimes it&rsquo;ll make sense to get narrower. For example, perhaps a battery pack targeted embassy and its associated ecosystem? Unclear.<br>Creating a battery pack<br>If you wanted to create a battery pack, how do you do it? One answer is that you just create a new crate. But a better approach is to use the &ldquo;battery-pack battery pack&rdquo;2, which bundles a template:<br>cargo bp new battery-pack

This will prompt you for the name of the battery pack you want to create and a few other things and make your crate. Then you can just use cargo add dependencies to represent the libraries you want to recommend and publish.<br>&ldquo;Batteries&rdquo; are more than dependencies<br>The &ldquo;batteries&rdquo; that you can add to your project aren&rsquo;t always dependencies. They can also be &ldquo;recipes&rdquo; or templates. For example, the CI battery pack3 can configure your project with the kind of &ldquo;super neat-o&rdquo; github actions you&rsquo;ve always wanted but never wanted to bother configuring. To use it, select one or more of the templates to install:<br>cargo bp add ci

I expect this kind of &ldquo;actions to improve your crate&rdquo; to become a rich source of things. Right now we&rsquo;re using a relatively lightweight template system built on minijinja, but I think we&rsquo;re going to want to expand on this.<br>Giving it some structure<br>Battery Packs also support more than just a flat listing of dependencies/features/templates. You can group dependencies and features into categories and then, for each category, distinguish between &ldquo;pick at most one&rdquo; or &ldquo;pick any number&rdquo;. For a fun example, try cargo bp add embedded, which is derived from the Awesome Embedded Rust repository. If you run it, you&rsquo;ll see something like...

battery pack rsquo packs ldquo rdquo

Related Articles