Znippets: a collection of zig code snippets tested across multiple versions!

Znippets, Github repo

Znippets, website

video presentation

A few weeks ago I created “Znippets”, the idea is to create a collection of useful zig code snippets that are tested across multiple zig versions, to be able to know if the current snippet you are looking at is working for the specific version of zig you are using! Because zig is still pretty unstable from version to version, you will find a lot of zig resources that are outdated, but it’s difficult to tell right away. That’s why I created znippets, so we can have a simple website with a bunch of code snippets, and for each snippet you can see a list of working and failing versions! And I also made a page for each version so that you can just look at the snippets that work for the desired version!
All of these pages are automatically generated, and minified! really bare bone!

This is really more a proof of concept than a solid and reliable project.
When designing it, my main concern was to build something with really low-maintenance. So it limits what’s possible, and I used the simplest ideas I could come up with, but if you have better ideas, please tell me about them :slight_smile:

To quickly tell you how it works (you can find more information in the README):

  • the code snippets are inside the snippets/ directory
  • each snippet is inside a test
  • I get the latest available versions by fetching and parsing this json file
  • I use zigup to switch between versions
  • I spawn a new process to call zig test <snippet-filename>.zig
    • if it compiles and test(s) succeed → marked as working
    • otherwise, marked as failing
  • I generate html files using a realllllly basic templating system (it looks for a keyword and replace it)
  • I store information inside 2 files (VERSIONS, SNIPPETS) to avoid repeating work
  • I’ve set up a VPS to automatically run this script once a day

Few notes:

  • Code is a beautiful mess, but I will (probably) rewrite it once 0.16.0 is released.
  • I made it using zig 0.16.0-dev.1326+2e6f7d36b
  • There’s an odd bug, if code snippet contains an arrow (<--), the following lines are mixed together, I didn’t investigate that much, but it could possibly come from std.Io.Writer.sendFileAll. (but too lazy to verify)
    See this, and this
  • I have a document that I didn’t share with a bunch of ideas, like a way to mark a snippet as outdated, deprecated, or replaced by another snippet…
  • it’s been a few weeks already, and I already forgot what I did/thought about!

Anyway, I will be happy to hear what you think, I am open to suggestions!
Happy new year btw!

12 Likes

That’s cool!

I like that you have ArrayList for 15 and 16, I am looking at that now.

I usually keep a repo called “learn” for each new language I learn.

My zig on is:

(edit: This is to show that I appreciate your very polished and
minimized examples.)

1 Like