<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>🐥 YellowDuck.be</title>
  <id>https://www.yellowduck.be/posts/feed</id>
  <updated>2026-11-30T18:00:00Z</updated>
  <author>
    <name>Pieter Claerhout</name>
    <email>pieter@yellowduck.be</email>
  </author>
  <link rel="self" href="https://www.yellowduck.be/posts/feed"/>
  <link rel="alternate" href="https://www.yellowduck.be"/>
  <entry>
    <id>https://www.yellowduck.be/posts/auto-assign-new-issues-and-pull-requests-with-github-actions</id>
    <title>🐥 Auto-assign new issues and pull requests with GitHub Actions</title>
    <updated>2026-11-30T18:00:00Z</updated>
    <published>2026-11-30T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/auto-assign-new-issues-and-pull-requests-with-github-actions"/>
    <content type="html">&lt;p&gt;When you’re the only maintainer of a repository, every new issue and pull request is yours anyway. Still, I like having the assignee field filled in. It makes GitHub’s “assigned to me” filters useful and keeps things working if more people join later. Doing this by hand is the kind of chore I forget, so I automated it with a small GitHub Actions workflow.&lt;/p&gt;
&lt;h1&gt;The workflow&lt;/h1&gt;
&lt;p&gt;I used &lt;a href=&quot;https://github.com/gustavofreze/auto-assign&quot;&gt;&lt;code&gt;gustavofreze/auto-assign&lt;/code&gt;&lt;/a&gt;. It assigns the issue or pull request that triggered the workflow to the logins you specify. Create &lt;code&gt;.github/workflows/auto-assign.yml&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-yaml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Auto assign&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;issues&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;types&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;      &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;opened&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;pull_request&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;types&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;      &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;opened&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;jobs&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;runs-on&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;ubuntu-latest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;permissions&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;      &lt;span class=&quot;l-property&quot;&gt;issues&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;write&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;      &lt;span class=&quot;l-property&quot;&gt;pull-requests&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;write&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;      &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Auto assigns assignees&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;gustavofreze/auto-assign@3.0.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;          &lt;span class=&quot;l-property&quot;&gt;assignees&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;your-github-login&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;          &lt;span class=&quot;l-property&quot;&gt;assignment_options&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;ISSUE,PULL_REQUEST&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;How it works&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Triggers:&lt;/strong&gt; The workflow runs when an issue or pull request is opened. For these events, the action only touches the item that triggered the run, and only if it has no assignee yet. A pull request event never assigns issues, and an issue event never assigns pull requests.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;assignment_options&lt;/code&gt;:&lt;/strong&gt; The default is &lt;code&gt;ISSUE&lt;/code&gt;. Set it to &lt;code&gt;ISSUE,PULL_REQUEST&lt;/code&gt; to cover both.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Permissions:&lt;/strong&gt; The job needs &lt;code&gt;issues: write&lt;/code&gt; and &lt;code&gt;pull-requests: write&lt;/code&gt;. The default workflow token is enough, so there’s no secret to create or rotate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pinning:&lt;/strong&gt; I pinned the action to the &lt;code&gt;3.0.0&lt;/code&gt; tag. For stricter supply-chain hygiene, you can pin it to a commit SHA instead.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Options worth knowing about&lt;/h1&gt;
&lt;p&gt;The action has a few inputs I didn’t need but you might:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th align=&quot;left&quot;&gt;Input&lt;/th&gt;
&lt;th align=&quot;left&quot;&gt;Default&lt;/th&gt;
&lt;th align=&quot;left&quot;&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;&lt;code&gt;skip_bots&lt;/code&gt;&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;Skips items opened by bots such as &lt;code&gt;dependabot[bot]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;&lt;code&gt;skip_drafts&lt;/code&gt;&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;Skips draft pull requests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;&lt;code&gt;assign_author&lt;/code&gt;&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;Also assigns the author of the item&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;&lt;code&gt;allow_no_assignees&lt;/code&gt;&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;Lets the run succeed when nobody can be assigned&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h1&gt;Caveats&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Forks:&lt;/strong&gt; On the &lt;code&gt;pull_request&lt;/code&gt; event, pull requests from forks get a read-only token, so assignment will fail for them. That doesn’t matter for a personal repo. If you accept outside contributions, you would need &lt;code&gt;pull_request_target&lt;/code&gt;, which has its own security trade-offs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Default branch:&lt;/strong&gt; The &lt;code&gt;issues&lt;/code&gt; trigger only fires once the workflow file is on the default branch. The &lt;code&gt;pull_request&lt;/code&gt; trigger uses the workflow file from the pull request itself, so the PR that introduces the workflow is its first test.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It’s a 23-line file that removes one small manual step. I opened the pull request that adds it on a branch of its own so I could see whether it assigned itself.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/devops&quot;&gt;#devops&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/github&quot;&gt;#github&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/building-a-safe-image-proxy-in-phoenix</id>
    <title>🐥 Building a Safe Image Proxy in Phoenix</title>
    <updated>2026-11-28T18:00:00Z</updated>
    <published>2026-11-28T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/building-a-safe-image-proxy-in-phoenix"/>
    <content type="html">&lt;p&gt;If you render content from other websites, you’ll eventually run into broken images. Some hosts refuse hotlinking, some check the &lt;code&gt;Referer&lt;/code&gt; header, and your own Content Security Policy may only allow images from a short list of origins. The result is the same: a page full of broken image icons.&lt;/p&gt;
&lt;p&gt;One fix is to stop showing images. Another is to serve them yourself through a small proxy. This post walks through the second option and, more importantly, the ways it can go wrong.&lt;/p&gt;
&lt;h1&gt;The idea&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;Rewrite every remote image URL in your content to point to your own server.&lt;/li&gt;
&lt;li&gt;When the browser requests it, fetch the original image server-side and stream it back.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Because the image now comes from your own origin, your CSP is happy. Because your server makes the request without a &lt;code&gt;Referer&lt;/code&gt;, most hotlink protection doesn’t trigger.&lt;/p&gt;
&lt;p&gt;The catch: you’ve just built a service that makes HTTP requests to URLs chosen by someone else. That is exactly the shape of an SSRF vulnerability, so most of the work is making it safe.&lt;/p&gt;
&lt;h1&gt;Step 1: sign the URLs&lt;/h1&gt;
&lt;p&gt;An unauthenticated &lt;code&gt;/proxy?url=...&lt;/code&gt; endpoint is an open proxy. Anyone can use it to hide their traffic, hammer a third party, or probe your internal network.&lt;/p&gt;
&lt;p&gt;Instead, only proxy URLs that your own code generated. Sign the source URL and put the signature in the path:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;/proxy/image/&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;sign&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;verify&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;Plug.Crypto.MessageVerifier&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;verify&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;sign&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;Plug.Crypto.MessageVerifier&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;sign&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;secret&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;  &lt;span class=&quot;l-variable&quot;&gt;base&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyAppWeb.Endpoint&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:secret_key_base&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;  &lt;span class=&quot;l-module&quot;&gt;Plug.Crypto.KeyGenerator&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;image proxy&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A few notes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;MessageVerifier&lt;/code&gt; is deterministic: the same URL always gives the same token. &lt;code&gt;Phoenix.Token&lt;/code&gt; includes a timestamp, which would make every render produce a different URL. That breaks HTTP caching and any cache of rendered HTML.&lt;/li&gt;
&lt;li&gt;Deriving the key with a dedicated salt keeps the proxy’s signatures separate from your other uses of the secret.&lt;/li&gt;
&lt;li&gt;Signing is not a complete defense. The URLs still originate from third-party pages, so the fetch itself has to be safe too.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Step 2: rewrite the content&lt;/h1&gt;
&lt;p&gt;If your content is Markdown, a regular expression over the image syntax is enough for a first version:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-function-call&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;image &lt;span class=&quot;l-string-special&quot;&gt;~r/&lt;/span&gt;!&lt;span class=&quot;l-string-escape&quot;&gt;\[&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;([&lt;/span&gt;^&lt;span class=&quot;l-string-escape&quot;&gt;\]&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-string-escape&quot;&gt;\]\(&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;https&lt;span class=&quot;l-operator&quot;&gt;?&lt;/span&gt;:&lt;span class=&quot;l-string-escape&quot;&gt;\/\/&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;^&lt;span class=&quot;l-constant&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-string-escape&quot;&gt;\s&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)((?:&lt;/span&gt;&lt;span class=&quot;l-string-escape&quot;&gt;\s&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt;&amp;quot;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;^&lt;span class=&quot;l-constant&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt;&amp;quot;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-string-escape&quot;&gt;\)&lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;/&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;rewrite_markdown&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;markdown&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-module&quot;&gt;Regex&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-constant&quot;&gt;image&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;markdown&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;alt&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-string&quot;&gt;&amp;quot;![&lt;span class=&quot;l-string-special&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;alt&lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;}&lt;/span&gt;](&lt;span class=&quot;l-string-special&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;}#{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;}&lt;/span&gt;)&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Only absolute &lt;code&gt;http(s)&lt;/code&gt; sources are rewritten. Relative paths and &lt;code&gt;data:&lt;/code&gt; URIs stay untouched. The known limitation: an image example inside a code block gets rewritten too. If that matters, rewrite on the parsed AST instead.&lt;/p&gt;
&lt;h1&gt;Step 3: fetch defensively&lt;/h1&gt;
&lt;p&gt;This is the dangerous part. Everything below matters.&lt;/p&gt;
&lt;h2&gt;Block private addresses&lt;/h2&gt;
&lt;p&gt;Before connecting, resolve the host and refuse anything that isn’t a public address: loopback, private ranges, link-local (which includes cloud metadata endpoints), carrier-grade NAT, and multicast. Don’t forget IPv6, including IPv4-mapped addresses like &lt;code&gt;::ffff:127.0.0.1&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;public_address?&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-keyword-operator&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;127&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;or&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;         &lt;span class=&quot;l-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;224&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;or&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;         &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;127&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;or&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;         &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;169&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;254&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;or&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;         &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;172&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;or&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;         &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;192&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;168&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;public_address?&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0xFFFF&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lo&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;public_address?&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;rem&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;lo&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;rem&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;lo&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;public_address?&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-boolean&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;public_address?&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0xFC00&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;0xFDFF&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-boolean&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;public_address?&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0xFE80&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;0xFEBF&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-boolean&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;public_address?&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0xFF00&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-boolean&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;public_address?&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-boolean&quot;&gt;true&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Check &lt;em&gt;every&lt;/em&gt; address a hostname resolves to, not just the first.&lt;/p&gt;
&lt;h2&gt;Follow redirects yourself&lt;/h2&gt;
&lt;p&gt;If the HTTP client follows redirects automatically, an attacker can point a public URL at &lt;code&gt;http://169.254.169.254/...&lt;/code&gt;, and your check on the first host is useless. Turn automatic redirects off, follow them manually with a small limit, and run the full validation on every hop:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-comment&quot;&gt;_src&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;hops_left&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;hops_left&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:error&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:too_many_redirects&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;hops_left&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;uri&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;       &lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;ensure_public_host&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;uri&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;       &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-function-call&quot;&gt;handle_response&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;hops_left&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Limit what you accept&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Timeouts.&lt;/strong&gt; A slow host shouldn’t tie up a worker. Use a short receive timeout and disable retries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Size.&lt;/strong&gt; Stream the body and stop once it passes a limit (say 10 MB), rather than loading whatever the server sends into memory.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Content type.&lt;/strong&gt; Allow a short list of raster formats: PNG, JPEG, GIF, WebP, and AVIF. Leave SVG out. It can contain script, and serving it from your origin is an XSS risk.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With Req, the streaming limit looks like this. The accumulator lives on the response:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:data&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-variable&quot;&gt;chunks&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Req.Response&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;get_private&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:chunks&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-variable&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Req.Response&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;get_private&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:size&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;byte_size&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;  &lt;span class=&quot;l-variable&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;response&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Req.Response&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;put_private&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:chunks&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;chunks&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Req.Response&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;put_private&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:size&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-constant&quot;&gt;max_bytes&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:halt&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Req.Response&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;put_private&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:too_large&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)}}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;else&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:cont&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Don’t send a referer&lt;/h2&gt;
&lt;p&gt;Simply don’t set one. Send a plain user agent and an &lt;code&gt;Accept: image/*&lt;/code&gt; header.&lt;/p&gt;
&lt;h1&gt;Step 4: serve it safely&lt;/h1&gt;
&lt;p&gt;The controller verifies the token, fetches the image, and responds. Even though you only allow raster images, add defense in depth:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;token&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;ImageProxy&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;verify&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;       &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;content_type&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;ImageProxy&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;conn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;put_resp_content_type&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;content_type&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;put_resp_header&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;cache-control&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;public, max-age=604800, immutable&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;put_resp_header&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;content-security-policy&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;default-src &amp;#39;none&amp;#39;; sandbox&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;put_resp_header&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;x-content-type-options&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;nosniff&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;send_resp&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;else&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;    &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;send_resp&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;404&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Always return a plain 404 on failure. Don’t leak the reason to the client. Log it instead.&lt;/p&gt;
&lt;p&gt;Put the route in a scope with no browser pipeline. An image doesn’t need a session, CSRF protection, or HTML security headers.&lt;/p&gt;
&lt;h1&gt;Testing it&lt;/h1&gt;
&lt;p&gt;Req’s &lt;code&gt;plug&lt;/code&gt; option lets you stub the remote server without any network access:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-module&quot;&gt;Req.Test&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;stub&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;ImageProxy&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;conn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-variable&quot;&gt;conn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Plug.Conn&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;put_resp_content_type&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;image/png&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Plug.Conn&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;send_resp&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;png&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;png&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;image/png&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;ImageProxy&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;https://93.184.216.34/a.png&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using an IP literal in the test URL avoids a real DNS lookup. The tests worth writing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the token round-trips, and a tampered or garbage token is rejected&lt;/li&gt;
&lt;li&gt;tokens are deterministic&lt;/li&gt;
&lt;li&gt;loopback, private, link-local, and IPv4-mapped IPv6 hosts are refused&lt;/li&gt;
&lt;li&gt;non-&lt;code&gt;http(s)&lt;/code&gt; schemes are refused&lt;/li&gt;
&lt;li&gt;SVG and other content types are refused&lt;/li&gt;
&lt;li&gt;an oversized body is refused&lt;/li&gt;
&lt;li&gt;a redirect to a private address is refused&lt;/li&gt;
&lt;li&gt;a redirect loop gives up&lt;/li&gt;
&lt;li&gt;no &lt;code&gt;Referer&lt;/code&gt; header is sent&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Pitfalls I ran into&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Hidden middleware can block the route.&lt;/strong&gt; If you have a plug that blocks suspicious paths (many apps do, to deflect scanners), a path like &lt;code&gt;/proxy/...&lt;/code&gt; may be on its list. My tests got a friendly “not found” page with a 200 status until I allowlisted the route.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Where Req keeps streaming state.&lt;/strong&gt; With &lt;code&gt;into:&lt;/code&gt; as a function, the accumulator is &lt;code&gt;{request, response}&lt;/code&gt;, and the final response is built from the one you return. State stored on the request is gone by the time you get the result. Store it on the response.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sigils and parentheses.&lt;/strong&gt; &lt;code&gt;~s(...)&lt;/code&gt; breaks as soon as your test string contains parentheses. Use &lt;code&gt;~s|...|&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;What’s still missing&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;DNS rebinding.&lt;/strong&gt; The code resolves the host to validate it, then the HTTP client resolves it again to connect. A hostile DNS server could answer differently the second time. Closing that gap means connecting to the validated IP while still sending the right &lt;code&gt;Host&lt;/code&gt; header and TLS server name. Signed URLs shrink the exposure but don’t remove it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Server-side caching.&lt;/strong&gt; The &lt;code&gt;Cache-Control&lt;/code&gt; header lets browsers and CDNs cache, but your server will still refetch on a cold cache. A CDN in front is the cheapest fix. Storing the images yourself also keeps them alive after the source disappears.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cost.&lt;/strong&gt; You now pay for the bandwidth, and you’re re-serving other people’s images. Think about whether that’s acceptable for your use case.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Should you do this at all?&lt;/h1&gt;
&lt;p&gt;Not always. If you don’t care about the images, stripping them is simpler, has no attack surface, and costs nothing to run. Before building a proxy, check &lt;em&gt;why&lt;/em&gt; the images are broken. If it’s your own CSP, the cheapest fix may be a configuration change. If it’s &lt;code&gt;Referer&lt;/code&gt;-based hotlink protection, a &lt;code&gt;no-referrer&lt;/code&gt; referrer policy on the images may be enough.&lt;/p&gt;
&lt;p&gt;The proxy earns its place when you want the images, the hosts really do block direct loading, and you’re willing to own the security and operations that come with fetching URLs on behalf of the internet.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/http&quot;&gt;#http&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/elixir&quot;&gt;#elixir&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/phoenix&quot;&gt;#phoenix&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/auth&quot;&gt;#auth&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/a-tiny-self-invalidating-cache-in-elixir-with-ets</id>
    <title>🐥 A tiny, self-invalidating cache in Elixir with ETS</title>
    <updated>2026-11-26T18:00:00Z</updated>
    <published>2026-11-26T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/a-tiny-self-invalidating-cache-in-elixir-with-ets"/>
    <content type="html">&lt;p&gt;Sometimes you have a pure function that is expensive to run and gets called with the same input again and again. Rendering Markdown with syntax highlighting is a good example: it’s CPU-heavy, the output depends only on the input, and the same document is requested over and over.&lt;/p&gt;
&lt;p&gt;You could reach for a caching library, and often that’s the right call. But for a small, well-bounded case, a plain ETS table gets you surprisingly far in about 70 lines. Here’s the technique and the traps to avoid.&lt;/p&gt;
&lt;h1&gt;The shape of the solution&lt;/h1&gt;
&lt;p&gt;Three ideas, each small:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A GenServer that only owns the table.&lt;/strong&gt; It does no work on the hot path.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A public ETS table&lt;/strong&gt;, so callers read and write directly, without going through a single process.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A cache key that includes everything the output depends on&lt;/strong&gt;, so you never need to invalidate by hand.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;1. The GenServer is just a table owner&lt;/h2&gt;
&lt;p&gt;An ETS table lives as long as the process that created it. Put that process in your supervision tree, and the table follows the lifecycle of your app.&lt;/p&gt;
&lt;p&gt;The common mistake is to route every read through the GenServer. That turns a concurrent data structure into a bottleneck: one process handling one message at a time. Instead, make the table &lt;code&gt;:public&lt;/code&gt; and let callers talk to ETS directly:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyApp.Renderer.Cache&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;GenServer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-function-call&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;table &lt;span class=&quot;l-constant-builtin&quot;&gt;__MODULE__&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;start_link&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;opts&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;\\&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[])&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;GenServer&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;start_link&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-constant-builtin&quot;&gt;__MODULE__&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;opts&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;name: &lt;/span&gt;&lt;span class=&quot;l-constant-builtin&quot;&gt;__MODULE__&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-function-call&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;impl &lt;span class=&quot;l-boolean&quot;&gt;true&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-comment&quot;&gt;_opts&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;    &lt;span class=&quot;l-type&quot;&gt;:ets&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-constant&quot;&gt;table&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;      &lt;span class=&quot;l-string-special-symbol&quot;&gt;:set&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;      &lt;span class=&quot;l-string-special-symbol&quot;&gt;:public&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;      &lt;span class=&quot;l-string-special-symbol&quot;&gt;:named_table&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;      &lt;span class=&quot;l-string-special-symbol&quot;&gt;read_concurrency: &lt;/span&gt;&lt;span class=&quot;l-boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;      &lt;span class=&quot;l-string-special-symbol&quot;&gt;write_concurrency: &lt;/span&gt;&lt;span class=&quot;l-boolean&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;read_concurrency: true&lt;/code&gt; optimizes for many simultaneous readers, which is exactly the access pattern of a cache. The GenServer has no &lt;code&gt;handle_call&lt;/code&gt; at all; its only job is to keep the table alive.&lt;/p&gt;
&lt;h2&gt;2. A &lt;code&gt;fetch/3&lt;/code&gt; that computes on a miss&lt;/h2&gt;
&lt;p&gt;The public API is a single function: give it an input, a version, and a function that produces the value on a miss.&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;is_binary&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;is_function&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-variable&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;:crypto&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:sha256&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;lookup&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;      &lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:error&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;      &lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;      &lt;span class=&quot;l-function-call&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;      &lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;lookup&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;:ets&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;lookup&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-constant&quot;&gt;table&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}]&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:error&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;rescue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;  &lt;span class=&quot;l-module&quot;&gt;ArgumentError&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:error&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;rescue ArgumentError&lt;/code&gt; is deliberate. If the table does not exist (the cache process is down or restarting), &lt;code&gt;:ets.lookup/2&lt;/code&gt; raises. Treating that as a miss means the cache can fail without taking the feature down: you just fall back to computing the value. A cache should never be a hard dependency.&lt;/p&gt;
&lt;p&gt;Calling it looks like this:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;render_article&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;markdown&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-module&quot;&gt;Cache&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;markdown&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;renderer_version&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;markdown&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;sanitize&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;convert_to_html&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;3. Put the version in the key&lt;/h2&gt;
&lt;p&gt;This is the part that removes the hardest problem in caching.&lt;/p&gt;
&lt;p&gt;The key is &lt;code&gt;{version, sha256(input)}&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The hash&lt;/strong&gt; means identical input hits the same entry, and the key stays small no matter how large the document is.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The version&lt;/strong&gt; captures everything else the output depends on. Here, it’s a fingerprint built from the versions of the libraries doing the work:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;renderer_version&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-string&quot;&gt;&amp;quot;mdex-&lt;span class=&quot;l-string-special&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;app_version&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:mdex&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;}&lt;/span&gt;+lumis-&lt;span class=&quot;l-string-special&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;app_version&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:lumis&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;}&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;app_version&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Application&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:vsn&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;vsn&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;is_list&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;vsn&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;to_string&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;vsn&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-constant-builtin&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;unknown&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When you upgrade a dependency, the version string changes, every old key becomes unreachable, and new requests render fresh output. No flush on deploy, no “did we remember to clear the cache?” step. If you change your own rendering logic in a way the dependencies cannot detect, add a manual &lt;code&gt;@version &amp;quot;2&amp;quot;&lt;/code&gt; to the fingerprint and bump it.&lt;/p&gt;
&lt;p&gt;Stale entries are never served; they just sit there until the table is trimmed.&lt;/p&gt;
&lt;h1&gt;Keeping it bounded&lt;/h1&gt;
&lt;p&gt;A cache without a limit is a memory leak with good intentions. The simplest bound that works:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-function-call&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;max_entries &lt;span class=&quot;l-number&quot;&gt;500&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;:ets&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-constant&quot;&gt;table&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:size&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-constant&quot;&gt;max_entries&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;:ets&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;delete_all_objects&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-constant&quot;&gt;table&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;  &lt;span class=&quot;l-type&quot;&gt;:ets&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-constant&quot;&gt;table&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;rescue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;  &lt;span class=&quot;l-module&quot;&gt;ArgumentError&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When the table is full, throw everything away. It’s crude, and I wouldn’t call it a good eviction policy. But it’s bounded, it’s one line, and the worst case is that some entries get recomputed. For a cache whose misses are merely slow rather than dangerous, that’s a fair trade.&lt;/p&gt;
&lt;h1&gt;What this does not give you&lt;/h1&gt;
&lt;p&gt;Be honest about the limits. This approach is a good fit for small caches and a poor one when you need any of the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Real eviction.&lt;/strong&gt; Flushing everything at the limit means a busy moment can empty a warm cache. If you care about keeping the hottest entries, you want LRU or something similar.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TTLs.&lt;/strong&gt; Entries live until the next flush. That’s fine when the key already encodes everything that matters, but wrong for data that goes stale with time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stampede protection.&lt;/strong&gt; If ten requests miss the same key at once, all ten compute the value. For expensive work under bursty traffic, you want single-flight behavior, where one caller computes and the rest wait.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sharing across nodes.&lt;/strong&gt; The table is local to one node. Each node warms its own copy.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you need those features, a library such as Cachex provides them. The migration is small precisely because the call site is a single &lt;code&gt;fetch&lt;/code&gt;. Start simple, and swap the implementation when a real limitation shows up.&lt;/p&gt;
&lt;h1&gt;Testing it&lt;/h1&gt;
&lt;p&gt;Because the interesting behavior is “computed once per key and version,” the tests are short. Count how many times the function runs:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;computes the value only once per input and version&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-variable&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;once &lt;span class=&quot;l-string-special&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;unique_integer&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:positive&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;}&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;calls&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Agent&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;start_link&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;  &lt;span class=&quot;l-variable&quot;&gt;compute&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-module&quot;&gt;Agent&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;calls&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;amp;1&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-string&quot;&gt;&amp;quot;html&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Cache&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;v1&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;compute&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;html&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Cache&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;v1&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;compute&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;html&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Agent&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;calls&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;amp;1&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;does not share entries between versions&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;  &lt;span class=&quot;l-variable&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;versioned &lt;span class=&quot;l-string-special&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;unique_integer&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:positive&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;}&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Cache&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;v1&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;old&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;old&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Cache&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;v2&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;new&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;new&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Cache&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;v1&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;unused&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;old&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note the &lt;code&gt;System.unique_integer/1&lt;/code&gt; in the input. The table is global, so tests that run concurrently must use distinct keys or they’ll interfere with each other.&lt;/p&gt;
&lt;h1&gt;Takeaways&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;Use a GenServer to &lt;strong&gt;own&lt;/strong&gt; an ETS table, not to &lt;strong&gt;serve&lt;/strong&gt; it.&lt;/li&gt;
&lt;li&gt;Make the table public with &lt;code&gt;read_concurrency: true&lt;/code&gt; and let callers hit ETS directly.&lt;/li&gt;
&lt;li&gt;Put everything the output depends on into the &lt;strong&gt;key&lt;/strong&gt;, and you can skip invalidation entirely.&lt;/li&gt;
&lt;li&gt;Treat a missing table as a cache miss, so the cache can never take the feature down.&lt;/li&gt;
&lt;li&gt;Always bound the size, even if the policy is blunt.&lt;/li&gt;
&lt;li&gt;Know where this stops being enough, and have a plan for what you would swap in.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It’s not a replacement for a full caching library. It’s a good way to avoid pulling one in until you actually need it.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/development&quot;&gt;#development&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/database&quot;&gt;#database&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/elixir&quot;&gt;#elixir&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/updating-elixir-npm-and-cargo-dependencies-with-a-single-command</id>
    <title>🐥 Updating Elixir, npm and Cargo dependencies with a single command</title>
    <updated>2026-11-24T18:00:00Z</updated>
    <published>2026-11-24T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/updating-elixir-npm-and-cargo-dependencies-with-a-single-command"/>
    <content type="html">&lt;p&gt;If your project mixes Elixir, JavaScript, and Rust, keeping dependencies fresh quickly turns into a chore. You have three package managers, three lock files, and three commands to remember. Skip one, and you end up with a stale lock file that bites you months later.&lt;/p&gt;
&lt;p&gt;My fix is simple: wrap everything in one Mix alias, so a full dependency refresh is just:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;dependencies.update&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This post explains how it works and how you can build the same thing.&lt;/p&gt;
&lt;h1&gt;The setup&lt;/h1&gt;
&lt;p&gt;A typical Phoenix project with a Rust NIF (via &lt;a href=&quot;https://github.com/rusterlium/rustler&quot;&gt;Rustler&lt;/a&gt;) has three dependency worlds:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Hex&lt;/strong&gt; packages for Elixir, tracked in &lt;code&gt;mix.exs&lt;/code&gt; and &lt;code&gt;mix.lock&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;npm&lt;/strong&gt; packages for the frontend, tracked in &lt;code&gt;assets/package.json&lt;/code&gt; and &lt;code&gt;package-lock.json&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cargo&lt;/strong&gt; crates for the native code, tracked in &lt;code&gt;native/*/Cargo.toml&lt;/code&gt; and &lt;code&gt;Cargo.lock&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Each has its own update command. Mix has a feature that ties them together: &lt;strong&gt;aliases&lt;/strong&gt;.&lt;/p&gt;
&lt;h1&gt;The alias&lt;/h1&gt;
&lt;p&gt;In &lt;code&gt;mix.exs&lt;/code&gt;, an alias is just a named list of other Mix tasks that run in order:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;aliases&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;&amp;quot;dependencies.update&amp;quot;: &lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;      &lt;span class=&quot;l-string&quot;&gt;&amp;quot;local.hex --force&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;      &lt;span class=&quot;l-string&quot;&gt;&amp;quot;deps.update --all&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;      &lt;span class=&quot;l-string&quot;&gt;&amp;quot;deps.clean --unused&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;      &lt;span class=&quot;l-string&quot;&gt;&amp;quot;native.update&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;      &lt;span class=&quot;l-string&quot;&gt;&amp;quot;assets.update&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Aliases can contain built-in tasks, tasks from dependencies, and your own custom tasks. Here is what each step does.&lt;/p&gt;
&lt;h2&gt;1. &lt;code&gt;local.hex --force&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;This upgrades the Hex package manager itself, so you resolve dependencies with the latest client. &lt;code&gt;--force&lt;/code&gt; skips the confirmation prompt, which keeps the whole process non-interactive.&lt;/p&gt;
&lt;h2&gt;2. &lt;code&gt;deps.update --all&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;This updates every Elixir dependency to the newest version allowed by the constraints in &lt;code&gt;mix.exs&lt;/code&gt; and rewrites &lt;code&gt;mix.lock&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;3. &lt;code&gt;deps.clean --unused&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;After an update, some packages may no longer be needed, for example, because a dependency dropped one of its own. This removes them from &lt;code&gt;deps/&lt;/code&gt; so the build directory matches the lock file.&lt;/p&gt;
&lt;h2&gt;4. &lt;code&gt;native.update&lt;/code&gt;: Cargo&lt;/h2&gt;
&lt;p&gt;Mix doesn't know about Cargo, so this is a small custom task. It finds every crate under &lt;code&gt;native/&lt;/code&gt; and runs &lt;code&gt;cargo update&lt;/code&gt; in each:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Mix.Tasks.Native.Update&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Mix.Task&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;@&lt;span class=&quot;l-comment-documentation&quot;&gt;shortdoc&lt;/span&gt; &lt;span class=&quot;l-comment-documentation&quot;&gt;&amp;quot;Run cargo update for the Rustler crates in native/&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-function-call&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;impl &lt;span class=&quot;l-module&quot;&gt;Mix.Task&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-comment&quot;&gt;_args&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-string&quot;&gt;&amp;quot;native&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;crate_dirs&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Enum&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;each&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-function&quot;&gt;cargo_update&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;/1&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;crate_dirs&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;root&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Path&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;*/Cargo.toml&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Path&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;wildcard&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Enum&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;Path&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function&quot;&gt;dirname&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;/1&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Enum&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;cargo_update&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;    &lt;span class=&quot;l-module&quot;&gt;Mix&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;shell&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Running cargo update in &lt;span class=&quot;l-string-special&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;}&lt;/span&gt; ...&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;23&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;24&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;cargo&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;update&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;cd: &lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;into: &lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;IO&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;25&quot;&gt;      &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;26&quot;&gt;      &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Mix&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;raise&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;cargo update failed in &lt;span class=&quot;l-string-special&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;}&lt;/span&gt; with exit code &lt;span class=&quot;l-string-special&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;}&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;27&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;28&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;29&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A few details:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Discovery through a wildcard.&lt;/strong&gt; Adding a new crate under &lt;code&gt;native/&lt;/code&gt; requires no change to the alias. It is picked up automatically.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;cd:&lt;/code&gt; instead of changing directory.&lt;/strong&gt; &lt;code&gt;System.cmd/3&lt;/code&gt; runs Cargo inside the crate directory, so the right &lt;code&gt;Cargo.lock&lt;/code&gt; is updated.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;into: IO.stream()&lt;/code&gt;.&lt;/strong&gt; Cargo's output streams straight to your terminal, so you see progress live.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;Mix.raise/1&lt;/code&gt; on failure.&lt;/strong&gt; A non-zero exit code aborts the whole alias. You never end up with a half-updated project that looks fine.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;5. &lt;code&gt;assets.update&lt;/code&gt;: npm&lt;/h2&gt;
&lt;p&gt;The npm step follows the same pattern, but is simpler because there is only one &lt;code&gt;package.json&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Mix.Tasks.Assets.Update&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Mix.Task&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;@&lt;span class=&quot;l-comment-documentation&quot;&gt;shortdoc&lt;/span&gt; &lt;span class=&quot;l-comment-documentation&quot;&gt;&amp;quot;Run npm update in assets/&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-function-call&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;impl &lt;span class=&quot;l-module&quot;&gt;Mix.Task&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-comment&quot;&gt;_args&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;assets_dir&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Path&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;cwd!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;assets&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;    &lt;span class=&quot;l-module&quot;&gt;Mix&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;shell&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Running npm update in &lt;span class=&quot;l-string-special&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;assets_dir&lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;}&lt;/span&gt; ...&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;npm&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;update&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;cd: &lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;assets_dir&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;into: &lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;IO&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;      &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;      &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Mix&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;raise&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;npm update failed with exit code &lt;span class=&quot;l-string-special&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;}&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;npm update&lt;/code&gt; respects the semver ranges in &lt;code&gt;package.json&lt;/code&gt;. It won't jump across major versions. For those, you still need to bump the range yourself, which is what you want: major upgrades deserve a deliberate decision.&lt;/p&gt;
&lt;h1&gt;Why bother with a wrapper?&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;One command, nothing forgotten.&lt;/strong&gt; The point isn't saving keystrokes. It's that you can't forget the Cargo lock file when you don't have to remember it exists.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Fail fast.&lt;/strong&gt; Each custom task raises on a non-zero exit code, and Mix stops the alias at the first failure.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Easy to extend.&lt;/strong&gt; Need to update GitHub Actions versions or regenerate a vendored file? Add another task to the list.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Self-documenting.&lt;/strong&gt; The alias documents your update process. A new team member can read ten lines in &lt;code&gt;mix.exs&lt;/code&gt; and know exactly what happens.&lt;/p&gt;
&lt;h1&gt;What I do after running it&lt;/h1&gt;
&lt;p&gt;Updating is only half the job. Afterwards, I:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Review the diff of &lt;code&gt;mix.lock&lt;/code&gt;, &lt;code&gt;package-lock.json&lt;/code&gt;, and &lt;code&gt;Cargo.lock&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Run the full test suite, plus a compile with warnings treated as errors.&lt;/li&gt;
&lt;li&gt;Skim changelogs for anything with a major version bump.&lt;/li&gt;
&lt;li&gt;Commit the lock file changes separately from any code changes they require.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Because all three lock files change in one go, the resulting commit is a clean &amp;quot;update dependencies&amp;quot; snapshot that is easy to review and, if needed, revert.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/rust&quot;&gt;#rust&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/javascript&quot;&gt;#javascript&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/tools&quot;&gt;#tools&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/elixir&quot;&gt;#elixir&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/running-independent-work-concurrently-in-elixir-without-losing-your-error-handling</id>
    <title>🐥 Running Independent Work Concurrently in Elixir without Losing Your Error Handling</title>
    <updated>2026-11-22T18:00:00Z</updated>
    <published>2026-11-22T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/running-independent-work-concurrently-in-elixir-without-losing-your-error-handling"/>
    <content type="html">&lt;p&gt;Some workflows are slow, not because the work is hard, but because we do it one step at a time. A typical example is a pipeline that calls an LLM (or any slow external service) several times: clean up a body of text, generate a title, produce a social media blurb, and suggest tags. Each call takes seconds, none depends on another's output, and yet they run back to back.&lt;/p&gt;
&lt;p&gt;Elixir makes concurrency cheap, so the fix is obvious: run the calls in parallel. The interesting part is doing so &lt;em&gt;without&lt;/em&gt; changing how errors behave. This post covers a tiny helper module, &lt;code&gt;Concurrent&lt;/code&gt;, that we added to do exactly that.&lt;/p&gt;
&lt;h1&gt;The problem&lt;/h1&gt;
&lt;p&gt;The first instinct is to reach for &lt;code&gt;Task.async/1&lt;/code&gt; and &lt;code&gt;Task.await_many/2&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;social&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;tags&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;clean_body&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;generate_title&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;generate_social&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;suggest_tags&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Enum&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;/1&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;await_many&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works on the happy path and cuts the total latency from &lt;em&gt;the sum&lt;/em&gt; of the steps to roughly &lt;em&gt;the slowest&lt;/em&gt; step. But it has a sharp edge: &lt;code&gt;Task.async/1&lt;/code&gt; &lt;strong&gt;links&lt;/strong&gt; the task to the caller. If any function raises, the task process crashes, the link propagates the exit signal, and the caller dies with it.&lt;/p&gt;
&lt;p&gt;That is a very different failure mode from sequential code. With sequential code, you can write:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;run_pipeline&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;rescue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-variable&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;SomeError&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and it works. After naively parallelising, the &lt;code&gt;rescue&lt;/code&gt; never fires. The caller gets killed by an exit signal instead, and your error handling, retries, and logging around the call are bypassed. In a web request or a background job, this typically shows up as a confusing crash rather than the cleanly handled error you had before.&lt;/p&gt;
&lt;h1&gt;The solution&lt;/h1&gt;
&lt;p&gt;We wanted the speed-up of concurrency with the ergonomics of a normal function call. The idea is simple:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Catch exceptions &lt;strong&gt;inside&lt;/strong&gt; each task and return them as data.&lt;/li&gt;
&lt;li&gt;Wait for all tasks to finish.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Re-raise&lt;/strong&gt; the first error in the caller, with the original stacktrace.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Concurrent&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;@moduledoc&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;  Small helpers for running independent work concurrently.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;  &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-function-call&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;default_timeout &lt;span class=&quot;l-type&quot;&gt;:timer&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;minutes&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;@doc&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;  Runs all funs concurrently and returns their results in order.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;  A raise inside a fun is caught in its task and re-raised in the caller with&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;  the original stacktrace, so callers can still &lt;span class=&quot;l-markup-raw&quot;&gt;`rescue`&lt;/span&gt; it.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;  &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-function-call&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;spec &lt;span class=&quot;l-function-call&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;([(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;term&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;())]&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;term&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;funs&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;timeout&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;\\&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-constant&quot;&gt;@&lt;span class=&quot;l-constant&quot;&gt;default_timeout&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;funs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Enum&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;run_safely&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;amp;1&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;await_many&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;    &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Enum&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;      &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;      &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:error&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;stacktrace&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;reraise&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;stacktrace&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;23&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;24&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;25&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;run_safely&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;26&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;27&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;rescue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;28&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:error&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;__STACKTRACE__&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;29&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;30&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Usage is a drop-in replacement for the sequential version:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;social&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;tags&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-module&quot;&gt;Concurrent&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;([&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;clean_body&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;generate_title&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;generate_social&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;suggest_tags&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Why it works&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;Errors become values inside the task.&lt;/strong&gt; &lt;code&gt;run_safely/1&lt;/code&gt; wraps the function in an implicit &lt;code&gt;try/rescue&lt;/code&gt; and returns either &lt;code&gt;{:ok, value}&lt;/code&gt; or &lt;code&gt;{:error, error, stacktrace}&lt;/code&gt;. Because the task never crashes, the link never fires and the caller is never killed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Errors become exceptions again in the caller.&lt;/strong&gt; After &lt;code&gt;Task.await_many/2&lt;/code&gt; returns, we map over the results. A success unwraps to its value; a failure is re-raised with &lt;code&gt;reraise/2&lt;/code&gt;, using the &lt;em&gt;original&lt;/em&gt; stacktrace. The stacktrace points at the code that actually failed, not at the helper, which keeps your error tracker useful.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Results keep their order.&lt;/strong&gt; &lt;code&gt;Task.await_many/2&lt;/code&gt; returns results in the same order as the tasks were started, so destructuring the result list is safe.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;There's a sensible timeout.&lt;/strong&gt; &lt;code&gt;Task.await_many/2&lt;/code&gt; defaults to 5 seconds, which is too short for LLM calls and many network operations. The helper defaults to two minutes and lets the caller override it.&lt;/p&gt;
&lt;h1&gt;Design decisions and trade-offs&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No process without a runtime reason.&lt;/strong&gt; This is a plain module with plain functions. There is no GenServer, supervisor, or state. The tasks are short-lived and exist only for the duration of the call, which is exactly what &lt;code&gt;Task&lt;/code&gt; is for.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Only independent work.&lt;/strong&gt; The helper is valid only when the steps do not depend on each other. When we introduced it, we also had to examine the data flow: one step was waiting for the output of a previous, slow step when it only needed a cheaper, earlier intermediate result. Removing that artificial dependency made parallelism possible in the first place.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Only &lt;code&gt;rescue&lt;/code&gt;, not &lt;code&gt;catch&lt;/code&gt; or exits.&lt;/strong&gt; The helper handles exceptions. Throws and exits inside a task will still crash it and, because of the link, the caller. That's a deliberate simplification; if you need to handle those, extend &lt;code&gt;run_safely/1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;First error wins.&lt;/strong&gt; If several functions fail, the first failing one in list order is re-raised. The others are simply discarded, since all tasks have already completed by then.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Not a replacement for &lt;code&gt;Task.Supervisor&lt;/code&gt;.&lt;/strong&gt; For fire-and-forget work, or when you want isolation from the caller's lifecycle, use &lt;code&gt;Task.Supervisor.async_nolink/3&lt;/code&gt; and handle the results explicitly. &lt;code&gt;Concurrent.all/2&lt;/code&gt; is for the &amp;quot;do these N things and give me all the answers&amp;quot; case.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Testing it&lt;/h1&gt;
&lt;p&gt;The behaviour worth pinning down is small and easy to test:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;returns results in order&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Concurrent&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;runs concurrently&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;micros&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-type&quot;&gt;:timer&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;tc&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;      &lt;span class=&quot;l-module&quot;&gt;Concurrent&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;duplicate&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;micros&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;300_000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;re-raises errors in the caller&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;assert_raise&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;RuntimeError&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;boom&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;    &lt;span class=&quot;l-module&quot;&gt;Concurrent&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;boom&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The third test is important: it proves that a failure surfaces as a normal, rescuable exception in the calling process.&lt;/p&gt;
&lt;h1&gt;Takeaways&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;Parallelising independent, slow calls is often the cheapest performance win available, and in Elixir it takes only a few lines.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Task.async/1&lt;/code&gt; is linked. To make failures behave as they do in sequential code, convert them to data inside the task and re-raise them in the caller.&lt;/li&gt;
&lt;li&gt;Keep the abstraction tiny. Thirty lines and no processes to supervise are easier to reason about than a framework.&lt;/li&gt;
&lt;li&gt;Before you parallelise, examine the data flow. Often the real win is removing an unnecessary dependency between steps.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/development&quot;&gt;#development&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/best-practice&quot;&gt;#best-practice&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/ai&quot;&gt;#ai&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/elixir&quot;&gt;#elixir&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/why-dependabot-auto-merge-fails-on-protected-branches-and-how-to-fix-it</id>
    <title>🐥 Why Dependabot auto-merge fails on protected branches (and how to fix it)</title>
    <updated>2026-11-20T18:00:00Z</updated>
    <published>2026-11-20T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/why-dependabot-auto-merge-fails-on-protected-branches-and-how-to-fix-it"/>
    <content type="html">&lt;p&gt;You set up a workflow to approve and auto-merge safe Dependabot updates. Approval works, but the merge step fails with:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;GraphQL: Pull request User is not authorized for this protected branch (enablePullRequestAutoMerge)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The YAML is correct. The problem is a mismatch between the identity running the workflow and your branch protection rules.&lt;/p&gt;
&lt;h1&gt;The setup&lt;/h1&gt;
&lt;p&gt;A typical workflow looks like this:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-yaml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;pull_request&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;types&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;opened&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;synchronize&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;reopened&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;permissions&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;contents&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;write&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;pull-requests&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;write&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;jobs&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;dependabot&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;runs-on&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;ubuntu-latest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;github.actor == &amp;#39;dependabot[bot]&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;      &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;dependabot/fetch-metadata@v2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;metadata&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;          &lt;span class=&quot;l-property&quot;&gt;github-token&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;${{ secrets.GITHUB_TOKEN }}&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;      &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Approve patch and minor updates&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;steps.metadata.outputs.update-type == &amp;#39;version-update:semver-patch&amp;#39; || steps.metadata.outputs.update-type == &amp;#39;version-update:semver-minor&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;gh&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;pr&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;review&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;&lt;span class=&quot;l-punctuation-special&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;PR_URL&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--approve&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;23&quot;&gt;          &lt;span class=&quot;l-property&quot;&gt;PR_URL&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;${{ github.event.pull_request.html_url }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;24&quot;&gt;          &lt;span class=&quot;l-property&quot;&gt;GH_TOKEN&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;${{ secrets.GITHUB_TOKEN }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;25&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;26&quot;&gt;      &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Enable auto-merge&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;27&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;steps.metadata.outputs.update-type == &amp;#39;version-update:semver-patch&amp;#39; || steps.metadata.outputs.update-type == &amp;#39;version-update:semver-minor&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;28&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;gh&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;pr&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;merge&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--auto&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--merge&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;&lt;span class=&quot;l-punctuation-special&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;PR_URL&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;29&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;30&quot;&gt;          &lt;span class=&quot;l-property&quot;&gt;PR_URL&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;${{ github.event.pull_request.html_url }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;31&quot;&gt;          &lt;span class=&quot;l-property&quot;&gt;GH_TOKEN&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;${{ secrets.GITHUB_TOKEN }}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Both &lt;code&gt;gh&lt;/code&gt; calls authenticate with the built-in &lt;code&gt;GITHUB_TOKEN&lt;/code&gt;, which acts as the &lt;code&gt;github-actions&lt;/code&gt; bot.&lt;/p&gt;
&lt;h1&gt;What goes wrong&lt;/h1&gt;
&lt;p&gt;Auto-merge is performed on behalf of whoever enables it. If the target branch has &lt;strong&gt;&amp;quot;Restrict who can push to matching branches&amp;quot;&lt;/strong&gt; enabled, that actor must be on the allow-list, or GitHub refuses the request with the error above.&lt;/p&gt;
&lt;p&gt;The review step works because approving a PR isn't a push. The merge step fails because merging is.&lt;/p&gt;
&lt;p&gt;You can confirm this with the API:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;gh&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;api&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;repos/&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-string-special-path&quot;&gt;owner&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-string-special-path&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-string-special-path&quot;&gt;repo&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-string-special-path&quot;&gt;/branches/&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-string-special-path&quot;&gt;branch&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-string-special-path&quot;&gt;/protection&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Look for a non-empty &lt;code&gt;restrictions&lt;/code&gt; object. If it lists a few users and no apps, that's your answer.&lt;/p&gt;
&lt;h1&gt;The obvious fix doesn't work&lt;/h1&gt;
&lt;p&gt;The natural move is to add &amp;quot;GitHub Actions&amp;quot; to the allow-list. You can't. The allow-list search only finds apps installed on the repository or organization. The built-in Actions identity behind &lt;code&gt;GITHUB_TOKEN&lt;/code&gt; isn't an installable app, so it won't appear in the search.&lt;/p&gt;
&lt;p&gt;Your options are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Drop the push restriction.&lt;/strong&gt; The review and status-check rules still protect the branch, but you widen who can push directly. Usually not worth it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use a personal access token from an allow-listed user.&lt;/strong&gt; This works, but ties the automation to one person's account and the token's lifetime.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use a GitHub App token.&lt;/strong&gt; The app is installable, so it can be added to the allow-list, and it isn't tied to a person.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Option 3 is the best fit.&lt;/p&gt;
&lt;h1&gt;The fix: a GitHub App token for the merge step&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;Create a GitHub App with repository permissions &lt;strong&gt;Contents: Read &amp;amp; write&lt;/strong&gt; and &lt;strong&gt;Pull requests: Read &amp;amp; write&lt;/strong&gt;. No webhook is needed.&lt;/li&gt;
&lt;li&gt;Install it on the repository.&lt;/li&gt;
&lt;li&gt;Add the app to the branch's push allow-list. It will now appear in the search.&lt;/li&gt;
&lt;li&gt;Store the app ID and the private key as secrets.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Then generate a short-lived token in the workflow and use it only for the merge step:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-yaml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Generate app token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;app-token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;actions/create-github-app-token@v2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;app-id&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;${{ secrets.AUTOMERGE_APP_ID }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;private-key&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;${{ secrets.AUTOMERGE_APP_PRIVATE_KEY }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Enable auto-merge&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;steps.metadata.outputs.update-type == &amp;#39;version-update:semver-patch&amp;#39; || steps.metadata.outputs.update-type == &amp;#39;version-update:semver-minor&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;gh&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;pr&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;merge&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--auto&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--merge&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;&lt;span class=&quot;l-punctuation-special&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;PR_URL&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;PR_URL&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;${{ github.event.pull_request.html_url }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;GH_TOKEN&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;${{ steps.app-token.outputs.token }}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The approval step keeps using &lt;code&gt;GITHUB_TOKEN&lt;/code&gt;. That's deliberate: the approver and merger are different identities, so the app never approves its own PRs, and a separate actor satisfies the required-review rule.&lt;/p&gt;
&lt;h1&gt;Side benefits&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Downstream workflows run.&lt;/strong&gt; Events caused by &lt;code&gt;GITHUB_TOKEN&lt;/code&gt; don't trigger other workflows. Merges made with an app token do, so post-merge pipelines run as expected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scoped and short-lived.&lt;/strong&gt; The token is minted per run and expires, unlike a long-lived PAT.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No dependence on a person.&lt;/strong&gt; The automation survives people leaving or changing roles.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;What auto-merge still won't do&lt;/h1&gt;
&lt;p&gt;Auto-merge waits for your required status checks and review rules. It only helps if at least one check is required; otherwise, there is nothing to wait for. Keep your branch protection as strict as before: the app only gets permission to perform the merge once everything is green.&lt;/p&gt;
&lt;h1&gt;Takeaways&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&amp;quot;Not authorized for this protected branch&amp;quot; on auto-merge usually means the actor isn't on the push allow-list, not that the workflow is wrong.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GITHUB_TOKEN&lt;/code&gt; can't be added to that list, so use a GitHub App token for the merge step.&lt;/li&gt;
&lt;li&gt;Keep approval and merging on different identities.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/tools&quot;&gt;#tools&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/devops&quot;&gt;#devops&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/github&quot;&gt;#github&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/letting-dependabot-merge-its-own-safe-updates-with-github-actions</id>
    <title>🐥 Letting Dependabot Merge Its Own Safe Updates with GitHub Actions</title>
    <updated>2026-11-18T18:00:00Z</updated>
    <published>2026-11-18T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/letting-dependabot-merge-its-own-safe-updates-with-github-actions"/>
    <content type="html">&lt;p&gt;Dependency updates are the chore nobody wants and everybody needs. In a typical web application, Dependabot opens a steady stream of pull requests across several ecosystems—in our case, Composer, npm, and GitHub Actions. Most are patch or minor updates that pass CI and get merged without a second glance. Clicking “Approve” and “Merge” on each one is pure overhead.&lt;/p&gt;
&lt;p&gt;So we automated it. This post walks through the 35-line workflow that approves and auto-merges low-risk Dependabot PRs, and the safeguards that make it reasonable to trust.&lt;/p&gt;
&lt;h1&gt;The setup&lt;/h1&gt;
&lt;p&gt;Dependabot is configured to check each ecosystem weekly and target the &lt;code&gt;develop&lt;/code&gt; branch:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-yaml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;updates&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;package-ecosystem&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;github-actions&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;directory&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;/&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;target-branch&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;develop&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;schedule&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;      &lt;span class=&quot;l-property&quot;&gt;interval&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;weekly&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;  &lt;span class=&quot;l-comment&quot;&gt;# ...same for &amp;quot;npm&amp;quot; and &amp;quot;composer&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this setup, merging into &lt;code&gt;develop&lt;/code&gt; deploys to staging, and &lt;code&gt;main&lt;/code&gt; is promoted to production. That matters: an auto-merged dependency update lands on staging, not directly in production.&lt;/p&gt;
&lt;h1&gt;The workflow&lt;/h1&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-yaml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Dependabot Auto-Approve and Merge&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;pull_request&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;types&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;opened&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;synchronize&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;reopened&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;permissions&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;contents&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;write&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;pull-requests&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;write&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;jobs&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;dependabot&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;runs-on&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;ubuntu-latest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;github.actor == &amp;#39;dependabot[bot]&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;      &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Fetch Dependabot metadata&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;metadata&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;dependabot/fetch-metadata@v2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;          &lt;span class=&quot;l-property&quot;&gt;github-token&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;${{ secrets.GITHUB_TOKEN }}&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;      &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Approve patch and minor updates&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;steps.metadata.outputs.update-type == &amp;#39;version-update:semver-patch&amp;#39; || steps.metadata.outputs.update-type == &amp;#39;version-update:semver-minor&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;23&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;24&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;          &lt;span class=&quot;l-function-call&quot;&gt;gh&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;pr&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;review&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;&lt;span class=&quot;l-punctuation-special&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;PR_URL&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--approve&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;25&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;26&quot;&gt;          &lt;span class=&quot;l-property&quot;&gt;PR_URL&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;${{ github.event.pull_request.html_url }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;27&quot;&gt;          &lt;span class=&quot;l-property&quot;&gt;GH_TOKEN&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;${{ secrets.GITHUB_TOKEN }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;28&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;29&quot;&gt;      &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Enable auto-merge&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;30&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;steps.metadata.outputs.update-type == &amp;#39;version-update:semver-patch&amp;#39; || steps.metadata.outputs.update-type == &amp;#39;version-update:semver-minor&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;31&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;32&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;          &lt;span class=&quot;l-function-call&quot;&gt;gh&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;pr&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;merge&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--auto&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--merge&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;&lt;span class=&quot;l-punctuation-special&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;PR_URL&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;33&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;34&quot;&gt;          &lt;span class=&quot;l-property&quot;&gt;PR_URL&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;${{ github.event.pull_request.html_url }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;35&quot;&gt;          &lt;span class=&quot;l-property&quot;&gt;GH_TOKEN&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;${{ secrets.GITHUB_TOKEN }}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Trigger&lt;/h2&gt;
&lt;p&gt;The workflow runs on &lt;code&gt;pull_request&lt;/code&gt; events for &lt;code&gt;opened&lt;/code&gt;, &lt;code&gt;synchronize&lt;/code&gt;, and &lt;code&gt;reopened&lt;/code&gt;. That covers a new Dependabot PR, a force-push when Dependabot rebases it, and a PR that someone closes and reopens.&lt;/p&gt;
&lt;h2&gt;Permissions&lt;/h2&gt;
&lt;p&gt;The default &lt;code&gt;GITHUB_TOKEN&lt;/code&gt; is scoped to what the job needs: &lt;code&gt;contents: write&lt;/code&gt; (required to merge) and &lt;code&gt;pull-requests: write&lt;/code&gt; (required to approve and enable auto-merge). Nothing else is granted.&lt;/p&gt;
&lt;h2&gt;Only run for Dependabot&lt;/h2&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-yaml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;github.actor == &amp;#39;dependabot[bot]&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This job-level guard means human PRs never run these steps. The job is skipped entirely, so the approval logic cannot be triggered by someone else's PR.&lt;/p&gt;
&lt;h2&gt;Classifying the update&lt;/h2&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-yaml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;dependabot/fetch-metadata@v2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The official &lt;code&gt;dependabot/fetch-metadata&lt;/code&gt; action parses the PR and exposes outputs such as &lt;code&gt;dependency-names&lt;/code&gt;, &lt;code&gt;package-ecosystem&lt;/code&gt;, and, most importantly, &lt;code&gt;update-type&lt;/code&gt;. That output is one of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;version-update:semver-patch&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;version-update:semver-minor&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;version-update:semver-major&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is the policy lever. We use the semver level to determine whether a human needs to review the update.&lt;/p&gt;
&lt;h2&gt;Approve and auto-merge patch and minor updates&lt;/h2&gt;
&lt;p&gt;Both remaining steps share the same condition: the update type must be patch or minor. Major updates skip both steps. The PR stays open and unapproved, waiting for someone to read the changelog.&lt;/p&gt;
&lt;p&gt;Step one approves the PR with &lt;code&gt;gh pr review --approve&lt;/code&gt;. Step two runs &lt;code&gt;gh pr merge --auto --merge&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;--auto&lt;/code&gt; option does &lt;strong&gt;not&lt;/strong&gt; merge immediately. It tells GitHub to merge the PR once all branch protection requirements are met. That means the full CI suite: unit tests, static analysis, linters, formatters, type checks, and the production build. If any check fails, the PR waits while someone investigates. The workflow removes the manual clicking, not the safety net.&lt;/p&gt;
&lt;h2&gt;Passing the PR URL through &lt;code&gt;env&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;The PR URL goes through an environment variable rather than being interpolated directly into the shell script:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-yaml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;PR_URL&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;${{ github.event.pull_request.html_url }}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is the recommended pattern for avoiding script injection in Actions. The &lt;code&gt;${{ }}&lt;/code&gt; expression is evaluated before the shell runs, so interpolating untrusted values directly into &lt;code&gt;run:&lt;/code&gt; can be exploited. Passing the value through &lt;code&gt;env&lt;/code&gt; and quoting &lt;code&gt;&amp;quot;$PR_URL&amp;quot;&lt;/code&gt; in the script keeps it as data. The URL here is GitHub-generated, so the risk is low, but it’s a good habit.&lt;/p&gt;
&lt;h1&gt;Prerequisites&lt;/h1&gt;
&lt;p&gt;For this to work, the repository needs a few settings:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Allow auto-merge&lt;/strong&gt; must be enabled in the repository settings. Without it, &lt;code&gt;gh pr merge --auto&lt;/code&gt; fails.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Branch protection on the target branch&lt;/strong&gt; with required status checks. Auto-merge waits for these. Without them, a PR could merge immediately.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Required approvals&lt;/strong&gt; (optional). If you require one review, the bot's approval satisfies it. Be deliberate about this, since it means the bot can approve this category of PRs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Allow GitHub Actions to approve pull requests&lt;/strong&gt; must be enabled under Settings → Actions → General.&lt;/li&gt;
&lt;/ol&gt;
&lt;h1&gt;Trade-offs and limitations&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Semver is a promise, not a guarantee.&lt;/strong&gt; Maintainers sometimes break things in minor releases, and 0.x packages treat minor updates as breaking. We rely on our test suite to catch this. Where coverage is thin, the risk is real.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Staging is the backstop.&lt;/strong&gt; Because merges land on &lt;code&gt;develop&lt;/code&gt; and deploy to staging first, a bad update is caught before it reaches production.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Supply-chain risk remains.&lt;/strong&gt; Auto-merging reduces human review of new upstream code. A compromised dependency release would pass through automatically if it passes CI. If that concerns you, consider a cooldown period in &lt;code&gt;dependabot.yml&lt;/code&gt;, restricting auto-merge to development dependencies, or grouping updates.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pinning to &lt;code&gt;@v2&lt;/code&gt;.&lt;/strong&gt; The workflow references a mutable tag. For stricter supply-chain hygiene, pin actions to a commit SHA. Dependabot can keep the SHA up to date for you.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Majors are still manual.&lt;/strong&gt; That is by design, but major updates can pile up if nobody reviews them.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Possible improvements&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;Filter by &lt;code&gt;dependency-type&lt;/code&gt; (&lt;code&gt;direct:production&lt;/code&gt; vs. &lt;code&gt;direct:development&lt;/code&gt;) to auto-merge only development dependencies.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;dependabot/fetch-metadata&lt;/code&gt; outputs to exclude specific packages, such as the framework itself.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;groups&lt;/code&gt; in &lt;code&gt;dependabot.yml&lt;/code&gt; to reduce PR volume.&lt;/li&gt;
&lt;li&gt;Merge the two conditional steps into one by using a job-level &lt;code&gt;if&lt;/code&gt; on the update type.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/development&quot;&gt;#development&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/best-practice&quot;&gt;#best-practice&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/devops&quot;&gt;#devops&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/github&quot;&gt;#github&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/git&quot;&gt;#git&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/halving-the-size-of-our-rust-nifs-with-four-lines-of-cargo-config</id>
    <title>🐥 Halving the Size of Our Rust NIFs with Four Lines of Cargo Config</title>
    <updated>2026-11-16T18:00:00Z</updated>
    <published>2026-11-16T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/halving-the-size-of-our-rust-nifs-with-four-lines-of-cargo-config"/>
    <content type="html">&lt;p&gt;My Elixir app uses two Rust NIFs built with &lt;a href=&quot;https://github.com/rusterlium/rustler&quot;&gt;Rustler&lt;/a&gt;: one that parses and discovers feeds, and one that extracts readable articles from HTML. When I checked &lt;code&gt;priv/native&lt;/code&gt;, they weighed 9.1 MB and 6.5 MB. That seemed like a lot for what they do.&lt;/p&gt;
&lt;p&gt;Neither crate had a &lt;code&gt;[profile.release]&lt;/code&gt; section, so both were built with Cargo's defaults. Those defaults optimise for speed and keep symbols, which isn't necessary for a NIF that mostly waits on I/O.&lt;/p&gt;
&lt;h1&gt;The fix&lt;/h1&gt;
&lt;p&gt;Add this to the &lt;code&gt;Cargo.toml&lt;/code&gt; of each crate:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-toml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-property&quot;&gt;profile&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-property&quot;&gt;release&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;opt-level&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;s&amp;quot;&lt;/span&gt;      &lt;span class=&quot;l-comment&quot;&gt;# optimise for size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;lto&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-boolean&quot;&gt;true&lt;/span&gt;           &lt;span class=&quot;l-comment&quot;&gt;# whole-program dead-code removal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;codegen-units&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;strip&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;symbols&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What each setting does:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;opt-level = &amp;quot;s&amp;quot;&lt;/code&gt;&lt;/strong&gt; tells the compiler to favour a smaller binary over the fastest possible code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;lto = true&lt;/code&gt;&lt;/strong&gt; enables link-time optimisation across crates, so unused dependency code can be removed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;codegen-units = 1&lt;/code&gt;&lt;/strong&gt; gives the optimiser a single unit to work on. Builds are slower, but the result is smaller.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;strip = &amp;quot;symbols&amp;quot;&lt;/code&gt;&lt;/strong&gt; removes symbol information from the shared library.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;The result&lt;/h1&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;NIF&lt;/th&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;feed_parser.so&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;9.1 MB&lt;/td&gt;
&lt;td&gt;4.7 MB (about 48% smaller)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;article_extractor.so&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;6.5 MB&lt;/td&gt;
&lt;td&gt;5.0 MB (about 23% smaller)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The feed parser benefited the most. It pulls in &lt;code&gt;reqwest&lt;/code&gt; with &lt;code&gt;rustls&lt;/code&gt; and &lt;code&gt;aws-lc-rs&lt;/code&gt; through its HTTP support, leaving a lot of code for LTO to prune. The article extractor has no TLS stack, so there was less to remove.&lt;/p&gt;
&lt;h1&gt;A gotcha: don't use &lt;code&gt;panic = &amp;quot;abort&amp;quot;&lt;/code&gt;&lt;/h1&gt;
&lt;p&gt;The usual advice for tiny Rust binaries includes &lt;code&gt;panic = &amp;quot;abort&amp;quot;&lt;/code&gt;. I tried it, but &lt;code&gt;rustler&lt;/code&gt; failed to compile. Rustler relies on unwinding to turn a Rust panic into an Erlang error. Even if it had compiled, aborting would take down the entire BEAM VM instead of failing just one call, which is exactly what you don't want in a NIF.&lt;/p&gt;
&lt;h1&gt;Verifying it&lt;/h1&gt;
&lt;p&gt;&amp;quot;It compiles&amp;quot; isn't enough for a NIF, so after &lt;code&gt;mix compile --force&lt;/code&gt; I called the functions with realistic input:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;extract_article/2&lt;/code&gt; on a small HTML page returned an article with the correct title and markdown.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;parse_feed/1&lt;/code&gt; parsed an RSS document into a feed struct.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;find_feeds/2&lt;/code&gt; found a feed advertised in a &lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; tag.&lt;/li&gt;
&lt;li&gt;The existing test suite for both modules still passed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I didn't benchmark the speed difference. &lt;code&gt;opt-level = &amp;quot;s&amp;quot;&lt;/code&gt; can be slightly slower than the default, but both NIFs are dominated by network and parsing work rather than tight loops, so I don't expect it to matter. If your NIF is CPU-bound, measure before switching.&lt;/p&gt;
&lt;h1&gt;What's next&lt;/h1&gt;
&lt;p&gt;Profile settings only go so far. The biggest remaining cost in the feed parser is likely the HTTP and TLS stack. If fetching moved to the Elixir side and only parsing stayed in Rust, the TLS feature could be disabled entirely. A tool like &lt;code&gt;cargo bloat --release --crates&lt;/code&gt; will show where the remaining bytes go.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/rust&quot;&gt;#rust&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/development&quot;&gt;#development&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/best-practice&quot;&gt;#best-practice&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/elixir&quot;&gt;#elixir&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/calling-a-rust-crate-from-elixir-with-rustler-and-getting-it-through-ci</id>
    <title>🐥 Calling a Rust crate from Elixir with Rustler (and getting it through CI)</title>
    <updated>2026-11-14T18:00:00Z</updated>
    <published>2026-11-14T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/calling-a-rust-crate-from-elixir-with-rustler-and-getting-it-through-ci"/>
    <content type="html">&lt;p&gt;I wanted a proper &amp;quot;reader mode&amp;quot; for the articles I link to on this blog: fetch a page, strip the navigation, ads, and cookie banners, and keep only the article. In Rust, there's a crate for this called &lt;a href=&quot;https://crates.io/crates/readex&quot;&gt;&lt;code&gt;readex&lt;/code&gt;&lt;/a&gt;, which does exactly that and can even output Markdown.&lt;/p&gt;
&lt;p&gt;My app is Elixir/Phoenix, so the obvious question was: how do I use a Rust crate from Elixir? The answer is &lt;a href=&quot;https://github.com/rusterlium/rustler&quot;&gt;Rustler&lt;/a&gt;, which lets you write NIFs (Native Implemented Functions) in Rust with safe, ergonomic bindings.&lt;/p&gt;
&lt;p&gt;In this post, I'll walk through the whole process, from &lt;code&gt;mix.exs&lt;/code&gt; to a green GitHub Actions pipeline, including the mistakes I made along the way.&lt;/p&gt;
&lt;h1&gt;Prerequisites: Rust and Rustler&lt;/h1&gt;
&lt;p&gt;Rustler compiles your Rust code as part of &lt;code&gt;mix compile&lt;/code&gt;, so you need a Rust toolchain on every machine that builds the project. The easiest way is &lt;a href=&quot;https://rustup.rs&quot;&gt;rustup&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;curl&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--proto&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;=https&amp;#39;&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--tlsv1.2&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-sSf&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;https://sh.rustup.rs&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;sh&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;rustc&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--version&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;cargo&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--version&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then add Rustler to your dependencies in &lt;code&gt;mix.exs&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;deps&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-comment&quot;&gt;# ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;~&amp;gt; 0.38.0&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;runtime: &lt;/span&gt;&lt;span class=&quot;l-boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;runtime: false&lt;/code&gt; is the right choice here: Rustler is only needed at compile time. The compiled library is loaded by your own module, not by the Rustler application.&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;deps.get&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Scaffolding the NIF with &lt;code&gt;mix rustler.new&lt;/code&gt;&lt;/h1&gt;
&lt;p&gt;Rustler ships with a generator that creates the Cargo project for you:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;rustler.new&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It asks for the name of the Elixir module that will host the NIF and the name of the crate. I used &lt;code&gt;YellowduckBe.ArticleExtractor.Native&lt;/code&gt; and &lt;code&gt;article_extractor&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This generates the following:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;native/article_extractor/&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;├── Cargo.toml&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;├── README.md&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;└── src/&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    └── lib.rs&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The generated &lt;code&gt;Cargo.toml&lt;/code&gt; is where we add the &lt;code&gt;readex&lt;/code&gt; crate:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-toml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-property&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;article_extractor&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;0.1.0&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;authors&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;edition&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;2021&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-property&quot;&gt;lib&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;crate-type&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;cdylib&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-property&quot;&gt;dependencies&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;rustler&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;0.39.0&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;readex&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;0.19&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;crate-type = [&amp;quot;cdylib&amp;quot;]&lt;/code&gt; makes Cargo produce a shared library that the BEAM can load.&lt;/p&gt;
&lt;p&gt;You also don't want build artifacts in Git, so add this to &lt;code&gt;.gitignore&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-gitignore&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;# Rust / Rustler&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;l-string-special-path&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;l-string-special-path&quot;&gt;native&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;l-character-special&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;l-string-special-path&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;l-string-special-path&quot;&gt;priv&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;l-string-special-path&quot;&gt;native&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;/&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;priv/native/&lt;/code&gt; is where Rustler copies the compiled library so that it ends up in your release.&lt;/p&gt;
&lt;h1&gt;Implementing &lt;code&gt;extract_article&lt;/code&gt; in Rust&lt;/h1&gt;
&lt;p&gt;The first version of the NIF was a straightforward mapping of everything &lt;code&gt;readex&lt;/code&gt; returns. Here's the core idea:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-rust&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-import&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;readex&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;extract_with&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Options&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-keyword-import&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;NifStruct&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;NifStruct&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;YellowduckBe.ArticleExtractor.Article&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;span class=&quot;l-keyword-type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Article&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;word_count&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type-builtin&quot;&gt;usize&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;language&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;byline&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;excerpt&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;site_name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;published_time&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;nif&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;schedule&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;DirtyCpu&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;extract_article&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;options&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Options&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;include_html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;        &lt;span class=&quot;l-operator&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Options&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;23&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;24&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;25&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;extract_with&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;26&quot;&gt;        &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;map_err&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(|&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;l-keyword-exception&quot;&gt;format!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;{error:?}&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;27&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;28&quot;&gt;    &lt;span class=&quot;l-constant-builtin&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Article&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;29&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;30&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;31&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;32&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;word_count&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;word_count&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;33&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;language&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;language&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;34&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;byline&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;byline&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;35&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;excerpt&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;excerpt&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;36&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;site_name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;site_name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;37&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;published_time&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;published_time&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;38&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;39&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;40&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;41&quot;&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;init!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Elixir.YellowduckBe.ArticleExtractor.Native&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A few details are worth noting:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;#[derive(NifStruct)]&lt;/code&gt;&lt;/strong&gt; with &lt;code&gt;#[module = &amp;quot;...&amp;quot;]&lt;/code&gt; converts the Rust struct into an Elixir struct of that module. No manual encoding or decoding is needed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;Option&amp;lt;String&amp;gt;&lt;/code&gt;&lt;/strong&gt; becomes either a string or &lt;code&gt;nil&lt;/code&gt; on the Elixir side.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;Result&amp;lt;Article, String&amp;gt;&lt;/code&gt;&lt;/strong&gt; becomes &lt;code&gt;{:ok, %Article{}}&lt;/code&gt; or &lt;code&gt;{:error, &amp;quot;reason&amp;quot;}&lt;/code&gt;, so it works with &lt;code&gt;with&lt;/code&gt; and &lt;code&gt;case&lt;/code&gt; as you'd expect.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;schedule = &amp;quot;DirtyCpu&amp;quot;&lt;/code&gt;&lt;/strong&gt; is important. Parsing and scoring an HTML document can take a while. A normal NIF that runs for more than about a millisecond blocks a scheduler and hurts the whole VM's latency. Marking it as a dirty CPU NIF runs it on a separate scheduler pool.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;rustler::init!&lt;/code&gt;&lt;/strong&gt; registers the module the NIF is loaded into. The &lt;code&gt;Elixir.&lt;/code&gt; prefix is how Elixir module names are represented in Erlang.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Exposing it to Elixir&lt;/h1&gt;
&lt;p&gt;On the Elixir side, you need three small pieces. First, the struct that &lt;code&gt;NifStruct&lt;/code&gt; maps to:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;YellowduckBe.ArticleExtractor.Article&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;defstruct&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:title&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:text&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:word_count&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:language&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:byline&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:excerpt&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:site_name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:published_time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Second, the module that loads the native library and declares the stubs:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;YellowduckBe.ArticleExtractor.Native&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;otp_app: :yellowduck_be&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;crate: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;article_extractor&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-comment&quot;&gt;# Replaced by the NIF once it&amp;#39;s loaded.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;extract_article&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-comment&quot;&gt;_html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_url&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;:erlang&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;nif_error&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:nif_not_loaded&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The stub is overridden when the library loads. If you ever see &lt;code&gt;:nif_not_loaded&lt;/code&gt;, the library didn't compile or load.&lt;/p&gt;
&lt;p&gt;And third, a small public module, so the rest of the app doesn't need to know there's a NIF involved:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;YellowduckBe.ArticleExtractor&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;alias&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;YellowduckBe.ArticleExtractor.Native&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;extract&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-module&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;extract_article&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you want to swap out the implementation or mock it in tests, this is the seam to do it at.&lt;/p&gt;
&lt;h1&gt;Using it from Elixir&lt;/h1&gt;
&lt;p&gt;Using it from a LiveView is a matter of fetching the HTML with &lt;a href=&quot;https://hexdocs.pm/req&quot;&gt;Req&lt;/a&gt; and passing it along:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;body: &lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Req&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;       &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;article&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;ArticleExtractor&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;extract&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;page: &lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;article&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or in an &lt;code&gt;iex -S mix&lt;/code&gt; session:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;body: &lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Req&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;https://example.com/some-article&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;article&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;YellowduckBe.ArticleExtractor&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;extract&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;https://example.com/some-article&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;article&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;title&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Some article&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;article&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;word_count&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;span class=&quot;l-number&quot;&gt;1234&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Passing the URL is useful: &lt;code&gt;readex&lt;/code&gt; uses it to resolve relative links and images.&lt;/p&gt;
&lt;h1&gt;Compiling it in CI/CD&lt;/h1&gt;
&lt;p&gt;Everything worked locally, so I pushed it, and the pipeline immediately failed. The GitHub runner didn't know it had to build Rust.&lt;/p&gt;
&lt;p&gt;The fix is to install a Rust toolchain before the Elixir build. I use &lt;a href=&quot;https://github.com/dtolnay/rust-toolchain&quot;&gt;&lt;code&gt;dtolnay/rust-toolchain&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-yaml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Install Rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;dtolnay/rust-toolchain@stable&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My first attempt added this to the wrong job. My workflow has two jobs that compile the app (one to run the tests, one to build and deploy), and both need Rust. I added it to one, moved it to the other, and finally had to put it in both before the pipeline went green.&lt;/p&gt;
&lt;p&gt;The lesson: &lt;strong&gt;every job that runs &lt;code&gt;mix compile&lt;/code&gt; needs the Rust toolchain.&lt;/strong&gt; Remember this when you add a job later.&lt;/p&gt;
&lt;h1&gt;Caching the Rust build&lt;/h1&gt;
&lt;p&gt;A cold build of a Rust crate with its dependencies takes minutes, far longer than the rest of my pipeline. &lt;a href=&quot;https://github.com/Swatinem/rust-cache&quot;&gt;&lt;code&gt;Swatinem/rust-cache&lt;/code&gt;&lt;/a&gt; caches the Cargo registry and the &lt;code&gt;target&lt;/code&gt; directory, keyed on your &lt;code&gt;Cargo.lock&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-yaml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Install Rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;dtolnay/rust-toolchain@stable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Setup Rust cache&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Swatinem/rust-cache@v2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;workspaces&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;native/article_extractor&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;workspaces&lt;/code&gt; option matters: by default, the action looks for &lt;code&gt;Cargo.toml&lt;/code&gt; in the repository root. Since the crate lives in &lt;code&gt;native/article_extractor&lt;/code&gt;, I have to tell it where to look.&lt;/p&gt;
&lt;p&gt;While I was there, I also added &lt;code&gt;restore-keys&lt;/code&gt; to the existing caches for &lt;code&gt;_build&lt;/code&gt; and &lt;code&gt;deps&lt;/code&gt;. Without them, a changed &lt;code&gt;mix.lock&lt;/code&gt; means a full cache miss. With them, the most recent cache with the same prefix is restored, and only what changed gets rebuilt:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-yaml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Cache compiled build&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;actions/cache@v4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;_build&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;${{ runner.os }}-mix-build-${{ hashFiles(&amp;#39;**/mix.lock&amp;#39;) }}-${{ hashFiles(&amp;#39;**/.tool-versions&amp;#39;) }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;restore-keys&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;      ${{ runner.os }}-mix-build-&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I also replaced the &lt;code&gt;UBUNTU_VERSION&lt;/code&gt; variable in the cache keys with &lt;code&gt;runner.os&lt;/code&gt;, the built-in way to key a cache by platform.&lt;/p&gt;
&lt;h1&gt;The gotcha: force a recompile so the NIF is rebuilt&lt;/h1&gt;
&lt;p&gt;This one cost me the most time. The pipeline was green, the caches worked, and then the app crashed at runtime with &lt;code&gt;:nif_not_loaded&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here's what happens:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Rustler compiles the crate and copies the library into &lt;code&gt;priv/native/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;priv/native/&lt;/code&gt; is in &lt;code&gt;.gitignore&lt;/code&gt;, so it isn't in the checkout.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;_build&lt;/code&gt; &lt;strong&gt;is&lt;/strong&gt; restored from cache, including the compiled &lt;code&gt;.beam&lt;/code&gt; file for &lt;code&gt;YellowduckBe.ArticleExtractor.Native&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Mix sees that nothing changed in the Elixir sources, concludes the app is up to date, and skips compiling it. The Rust compiler never runs, so &lt;code&gt;priv/native/&lt;/code&gt; stays empty.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The cache gave me stale Elixir bytecode without the native library it needs. The fix is to delete the compiled output of just &lt;em&gt;my&lt;/em&gt; app (not the dependencies, which are the expensive part) before compiling:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-yaml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Force recompile of the app (rebuilds the Rust NIF into priv/native)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;rm&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-rf&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;_build/&lt;/span&gt;${{ env.MIX_ENV &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}}&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;/lib/yellowduck_be&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now Mix has to recompile the app, which invokes the Rustler compiler. The Rust build is already cached by &lt;code&gt;rust-cache&lt;/code&gt;, and Rustler copies the library into &lt;code&gt;priv/native/&lt;/code&gt;. You get the caching benefits and a correct build.&lt;/p&gt;
&lt;h1&gt;Adding Markdown support&lt;/h1&gt;
&lt;p&gt;Once everything was running, I considered what I actually wanted to display. Plain text loses all the structure, and raw HTML needs sanitising. &lt;code&gt;readex&lt;/code&gt; has &lt;code&gt;extract_to_markdown&lt;/code&gt;, which returns the clean article as Markdown, making it easy to render with an Elixir Markdown library.&lt;/p&gt;
&lt;p&gt;I also simplified the struct: I dropped the fields I wasn't using (&lt;code&gt;canonical_url&lt;/code&gt;, &lt;code&gt;image&lt;/code&gt;, &lt;code&gt;categories&lt;/code&gt;, &lt;code&gt;tags&lt;/code&gt;), replaced the optional &lt;code&gt;html&lt;/code&gt; with the &lt;code&gt;original_html&lt;/code&gt; I was handed, and added &lt;code&gt;markdown&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-rust&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-import&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;readex&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;extract_to_markdown&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;extract_with&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Options&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-keyword-import&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;NifStruct&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;NifStruct&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;YellowduckBe.ArticleExtractor.Article&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;span class=&quot;l-keyword-type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Article&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;original_html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;markdown&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;word_count&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type-builtin&quot;&gt;usize&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;language&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;byline&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;excerpt&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;site_name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;published_time&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;nif&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;schedule&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;DirtyCpu&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;extract_article&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;options&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Options&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;23&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;extracted&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;extract_with&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;24&quot;&gt;        &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;map_err&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(|&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;l-keyword-exception&quot;&gt;format!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;{error:?}&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;25&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;26&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;markdown&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;extract_to_markdown&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;27&quot;&gt;        &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;map_err&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(|&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;l-keyword-exception&quot;&gt;format!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;{error:?}&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;28&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;29&quot;&gt;    &lt;span class=&quot;l-constant-builtin&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Article&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;30&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;extracted&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;31&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;original_html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;32&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;markdown&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;33&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;extracted&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;34&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;word_count&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;extracted&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;word_count&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;35&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;language&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;extracted&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;language&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;36&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;byline&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;extracted&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;byline&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;37&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;excerpt&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;extracted&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;excerpt&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;38&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;site_name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;extracted&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;site_name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;39&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;published_time&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;extracted&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;published_time&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;40&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;41&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;42&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;43&quot;&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;init!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Elixir.YellowduckBe.ArticleExtractor.Native&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The Elixir struct must match, since &lt;code&gt;NifStruct&lt;/code&gt; expects the fields to match:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;YellowduckBe.ArticleExtractor.Article&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;defstruct&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:title&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:original_html&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:markdown&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:text&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:word_count&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:language&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:byline&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:excerpt&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:site_name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;:published_time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the LiveView template, I can render it with &lt;a href=&quot;https://hexdocs.pm/mdex&quot;&gt;MDEx&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-heex&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-tag-delimiter&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-tag&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;l-tag-attribute&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;&lt;span class=&quot;l-string&quot;&gt;readability-content&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-tag-attribute&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;&lt;span class=&quot;l-string&quot;&gt;prose max-w-none dark:prose-invert&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-tag-delimiter&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-tag-delimiter&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;safe_html&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;markdown&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MDEx&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;to_html!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;l-tag-delimiter&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-tag-delimiter&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;l-tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;l-tag-delimiter&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that this extracts the article twice (once for the metadata, once for the Markdown). For my use case, the cost is negligible, and the code stays simple. If this ever shows up in a profile, it's the first thing I'd look at.&lt;/p&gt;
&lt;p&gt;The Rust part was the easy bit. Rustler's derive macros make the boundary between Rust and Elixir nearly invisible, and dirty schedulers keep long-running work from hurting the VM.&lt;/p&gt;
&lt;p&gt;The real work was in the pipeline:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Every job that compiles the app needs a &lt;strong&gt;Rust toolchain&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;&lt;code&gt;rust-cache&lt;/code&gt;&lt;/strong&gt; with &lt;code&gt;workspaces&lt;/code&gt; pointing at your crate.&lt;/li&gt;
&lt;li&gt;Add &lt;strong&gt;&lt;code&gt;restore-keys&lt;/code&gt;&lt;/strong&gt; to your Mix caches so a small change doesn't trigger a full rebuild.&lt;/li&gt;
&lt;li&gt;When you cache &lt;code&gt;_build&lt;/code&gt;, &lt;strong&gt;force a recompile of your own app&lt;/strong&gt;, or the NIF never gets built into &lt;code&gt;priv/native&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And the result is a dependable article extractor in about 40 lines of Rust, running inside my Phoenix app with no extra services to run.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/rust&quot;&gt;#rust&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/development&quot;&gt;#development&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/elixir&quot;&gt;#elixir&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/phoenix&quot;&gt;#phoenix&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/github&quot;&gt;#github&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/passing-maps-and-enums-with-rust-nifs-in-elixir</id>
    <title>🐥 Passing Maps and Enums with Rust NIFs in Elixir</title>
    <updated>2026-11-12T18:00:00Z</updated>
    <published>2026-11-12T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/passing-maps-and-enums-with-rust-nifs-in-elixir"/>
    <content type="html">&lt;p&gt;In addition to structs, you can pass &lt;strong&gt;maps&lt;/strong&gt; and &lt;strong&gt;enums&lt;/strong&gt; between Elixir and Rust when writing Native Implemented Functions (NIFs) with Rustler. This gives you flexibility when working with more dynamic or variant data.&lt;/p&gt;
&lt;h1&gt;Passing maps from Elixir to Rust&lt;/h1&gt;
&lt;p&gt;Elixir maps can be received in Rust as &lt;code&gt;MapIterator&lt;/code&gt;s or &lt;code&gt;HashMap&lt;/code&gt;s, or decoded into Rust structs.&lt;/p&gt;
&lt;h2&gt;Example: reading keys from a map&lt;/h2&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-rust&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-import&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;MapIterator&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;NifResult&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Term&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;nif&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Term&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;NifResult&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Vec&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-type&quot;&gt;MapIterator&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;        &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;ok_or&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;BadArg&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;        &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(|&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-character-special&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;        &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyNif&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;a&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;b&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;a&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;b&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Example: decoding into a Rust struct&lt;/h2&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-rust&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-import&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;NifMap&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;NifResult&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Term&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;NifMap&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-keyword-type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;GreetUser&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type-builtin&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;nif&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;user_term&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Term&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;NifResult&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;GreetUser&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;user_term&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;    &lt;span class=&quot;l-constant-builtin&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-keyword-exception&quot;&gt;format!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Hello, {} (age {})&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyNif&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;name: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Alice&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;age: &lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Hello, Alice (age 30)&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Passing enums from Elixir to Rust&lt;/h1&gt;
&lt;p&gt;Enums can be passed using tagged maps or atoms.&lt;/p&gt;
&lt;h2&gt;Rust enum:&lt;/h2&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-rust&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;NifMap&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-keyword-type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Circle&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;radius&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type-builtin&quot;&gt;f64&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;NifMap&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-keyword-type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Rectangle&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type-builtin&quot;&gt;f64&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type-builtin&quot;&gt;f64&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;&lt;span class=&quot;l-keyword-type&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;    &lt;span class=&quot;l-constant&quot;&gt;Circle&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Circle&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;    &lt;span class=&quot;l-constant&quot;&gt;Rectangle&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Rectangle&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-keyword-modifier&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-attribute&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Decoder&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-keyword-modifier&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-attribute&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;    &lt;span class=&quot;l-keyword-function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;term&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Term&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-keyword-modifier&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-attribute&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;NifResult&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Atom&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;from_str&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;term&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;get_env&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;type&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;ty&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;term&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;map_get&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;        &lt;span class=&quot;l-keyword-conditional&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;ty&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;as_str&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;            &lt;span class=&quot;l-string&quot;&gt;&amp;quot;Circle&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;Circle&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;term&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;23&quot;&gt;            &lt;span class=&quot;l-string&quot;&gt;&amp;quot;Rectangle&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;Rectangle&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;term&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;24&quot;&gt;            &lt;span class=&quot;l-character-special&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;Err&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;BadArg&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;25&quot;&gt;        &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;26&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;27&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Function using the enum:&lt;/h2&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-rust&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;nif&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type-builtin&quot;&gt;f64&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-keyword-conditional&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;shape&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;        &lt;span class=&quot;l-type&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;Circle&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Circle&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;radius&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;f64&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;consts&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;PI&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;radius&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;radius&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;        &lt;span class=&quot;l-type&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;Rectangle&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Rectangle&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;height&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Elixir usage:&lt;/h2&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyNif&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;type: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Circle&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;radius: &lt;/span&gt;&lt;span class=&quot;l-number-float&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-number-float&quot;&gt;28.274333882308138&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyNif&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;type: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Rectangle&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;width: &lt;/span&gt;&lt;span class=&quot;l-number-float&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;height: &lt;/span&gt;&lt;span class=&quot;l-number-float&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-number-float&quot;&gt;20.0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Returning maps and enums from Rust to Elixir&lt;/h1&gt;
&lt;p&gt;To return dynamic structures like maps from Rust, construct them using &lt;code&gt;Env&lt;/code&gt; and Rustler's term constructors.&lt;/p&gt;
&lt;h2&gt;Returning a map:&lt;/h2&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-rust&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-import&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Env&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Term&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;NifResult&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;nif&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;status_map&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-keyword-modifier&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-attribute&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Env&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-keyword-modifier&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-attribute&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type-builtin&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;NifResult&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Term&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-keyword-modifier&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-attribute&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-keyword-conditional&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;200&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;ok&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-keyword-conditional&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;error&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-type&quot;&gt;Term&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;map_new&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;map_put&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;status&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyNif&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;status_map&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;status&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;ok&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Returning an enum as a tagged map&lt;/h2&gt;
&lt;p&gt;You can define a Rust enum and implement &lt;code&gt;Encoder&lt;/code&gt; for it:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-rust&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-import&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Encoder&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Env&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Term&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-keyword-type&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Response&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-constant&quot;&gt;Success&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-constant&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;reason&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;// Elixir side:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;//   %{&amp;quot;type&amp;quot; =&amp;gt; &amp;quot;success&amp;quot;, &amp;quot;result&amp;quot; =&amp;gt; &amp;quot;done&amp;quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;//   %{&amp;quot;type&amp;quot; =&amp;gt; &amp;quot;error&amp;quot;, &amp;quot;reason&amp;quot; =&amp;gt; &amp;quot;...&amp;quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Encoder&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Response&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;    &lt;span class=&quot;l-keyword-function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-keyword-modifier&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-attribute&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-variable-builtin&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Env&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-keyword-modifier&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-attribute&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Term&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-keyword-modifier&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-attribute&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;pairs&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Vec&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;(&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-type-builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;l-type-builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-keyword-conditional&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;l-variable-builtin&quot;&gt;self&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;            &lt;span class=&quot;l-type&quot;&gt;Response&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;Success&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-keyword-exception&quot;&gt;vec!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;type&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;success&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;result&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)]&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;            &lt;span class=&quot;l-type&quot;&gt;Response&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;reason&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-keyword-exception&quot;&gt;vec!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;type&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;error&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;reason&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;reason&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)]&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;        &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-keyword-modifier&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Term&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;map_new&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;        &lt;span class=&quot;l-keyword-repeat&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword-repeat&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;pairs&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;            &lt;span class=&quot;l-comment&quot;&gt;// String keys/values encode to Elixir binaries&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;            &lt;span class=&quot;l-variable&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;map_put&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;        &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;23&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;map&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;24&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;25&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;26&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;27&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;nif&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;28&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;status_map&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type-builtin&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Response&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;29&quot;&gt;    &lt;span class=&quot;l-keyword-conditional&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;200&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;30&quot;&gt;        &lt;span class=&quot;l-type&quot;&gt;Response&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;Success&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;done&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;to_string&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;31&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-keyword-conditional&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;32&quot;&gt;        &lt;span class=&quot;l-type&quot;&gt;Response&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-constant&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;reason&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-keyword-exception&quot;&gt;format!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;unexpected code {}&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;33&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;34&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This allows you to return tagged maps like:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;type&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;success&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;result&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;done&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyNif&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;status_map&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;result&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;done&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;type&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;success&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyNif&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;status_map&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;reason&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;unexpected code 500&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;type&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;error&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Handling invalid input&lt;/h1&gt;
&lt;p&gt;Instead of calling &lt;code&gt;.unwrap()&lt;/code&gt;, return a safe error from your NIF:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-rust&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-import&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Atom&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;NifMap&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Term&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;atoms!&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;invalid_user_struct&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;NifMap&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;span class=&quot;l-keyword-type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;GreetUser&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;nif&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;safe_greet&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;user_term&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Term&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Atom&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;    &lt;span class=&quot;l-keyword-conditional&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;user_term&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;GreetUser&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;&amp;gt;()&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;        &lt;span class=&quot;l-constant-builtin&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-keyword-exception&quot;&gt;format!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Hello, {}&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;        &lt;span class=&quot;l-constant-builtin&quot;&gt;Err&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-character-special&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;Err&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;invalid_user_struct&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In Elixir:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyNif&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;safe_greet&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;name: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Bob&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;Hello, Bob&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyNif&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;safe_greet&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;bad: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;input&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:error&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:invalid_user_struct&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/rust&quot;&gt;#rust&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/elixir&quot;&gt;#elixir&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/passing-structs-with-a-rust-nif-in-elixir</id>
    <title>🐥 Passing Structs with a Rust NIF in Elixir</title>
    <updated>2026-11-10T18:00:00Z</updated>
    <published>2026-11-10T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/passing-structs-with-a-rust-nif-in-elixir"/>
    <content type="html">&lt;p&gt;When using Rust NIFs in Elixir via the &lt;a href=&quot;https://github.com/rusterlium/rustler&quot;&gt;Rustler&lt;/a&gt; crate, it's often useful to pass structured data, not just integers and strings. This post shows how to pass an Elixir struct to Rust, manipulate it, and return a new struct to Elixir.&lt;/p&gt;
&lt;h1&gt;Define a struct in Elixir&lt;/h1&gt;
&lt;p&gt;Start by creating a simple struct, such as &lt;code&gt;User&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyNif.User&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;defstruct&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:age&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Define the same struct in Rust&lt;/h1&gt;
&lt;p&gt;In your NIF crate (&lt;code&gt;native/my_nif/src/lib.rs&lt;/code&gt;), define a matching Rust struct using Rustler's &lt;code&gt;NifStruct&lt;/code&gt; derive macro:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-rust&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-import&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;NifStruct&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;NifStruct&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;MyNif.User&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-keyword-type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-variable-member&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type-builtin&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;#[module = &amp;quot;MyNif.User&amp;quot;]&lt;/code&gt; attribute tells Rustler how to map the Elixir struct to the Rust struct.&lt;/p&gt;
&lt;h1&gt;Write a NIF function that uses the struct&lt;/h1&gt;
&lt;p&gt;Here's a Rust NIF that increases a user's age:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-rust&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;nif&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;birthday&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-type&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;age&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;        &lt;span class=&quot;l-operator&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;user&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Register the function:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-rust&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;init!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Elixir.MyNif&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Wire it up in Elixir&lt;/h1&gt;
&lt;p&gt;In your wrapper module:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyNif&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;otp_app: :my_nif_app&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;crate: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;my_nif&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;birthday&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-comment&quot;&gt;_user&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;:erlang&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;nif_error&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:nif_not_loaded&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Call it from IEx&lt;/h1&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;$ &lt;span class=&quot;l-function-call&quot;&gt;iex&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;S&lt;/span&gt; mix&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;iex&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;MyNif.User&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;name: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Alice&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;age: &lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;29&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;iex&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyNif&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;birthday&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;MyNif.User&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;name: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Alice&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;age: &lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Notes&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;Fields must match exactly between Elixir and Rust.&lt;/li&gt;
&lt;li&gt;Rustler will raise a badarg error if field names or types don't match.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NifStruct&lt;/code&gt; automatically handles encoding and decoding using &lt;code&gt;serde&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/rust&quot;&gt;#rust&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/elixir&quot;&gt;#elixir&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/creating-a-simple-elixir-nif-using-rust</id>
    <title>🐥 Creating a Simple Elixir NIF Using Rust</title>
    <updated>2026-11-08T18:00:00Z</updated>
    <published>2026-11-08T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/creating-a-simple-elixir-nif-using-rust"/>
    <content type="html">&lt;p&gt;Elixir is great for many things, but sometimes you need raw speed or access to native libraries. That’s where &lt;strong&gt;Native Implemented Functions (NIFs)&lt;/strong&gt; come in. This post walks through creating a simple Elixir NIF using Rust with the help of the &lt;a href=&quot;https://github.com/rusterlium/rustler&quot;&gt;&lt;code&gt;rustler&lt;/code&gt;&lt;/a&gt; crate.&lt;/p&gt;
&lt;h1&gt;Set up your Elixir project&lt;/h1&gt;
&lt;p&gt;Start with a basic Elixir application:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;my_nif_app&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-function-builtin&quot;&gt;cd&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;my_nif_app&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Add &lt;code&gt;:rustler&lt;/code&gt; as a dependency in &lt;code&gt;mix.exs&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;deps&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;~&amp;gt; 0.38.0&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Fetch the dependencies:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;deps.get&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Generate the Rust NIF scaffold&lt;/h1&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;rustler.new&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;This&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;Elixir&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;NIF&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;will&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;registered&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;to.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;Module&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string-special-path&quot;&gt;MyNif&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;This&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;used&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;generated&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;Rust&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;crate.&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;The&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;most&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;likely&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;fine.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;Library&lt;/span&gt; name &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;mynif&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string-special-path&quot;&gt;my_nif&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;Fetched&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;latest&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;rustler&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;crate&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;version:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;0.39.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;creating&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;native/my_nif/README.md&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;creating&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;native/my_nif/Cargo.toml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;creating&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;native/my_nif/src/lib.rs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;creating&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;Cargo.toml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;Updating&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;.gitignore&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;file&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;Ready&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;go!&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;See&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;native/my_nif/README.md&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;further&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;instructions.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This command will:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create a &lt;code&gt;native/&lt;/code&gt; directory&lt;/li&gt;
&lt;li&gt;Scaffold a Rust crate&lt;/li&gt;
&lt;li&gt;Link it into your Elixir project&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Choose a crate name when prompted, such as &lt;code&gt;my_nif&lt;/code&gt;.&lt;/p&gt;
&lt;h1&gt;Write a simple NIF in Rust&lt;/h1&gt;
&lt;p&gt;Open the generated &lt;code&gt;native/my_nif/src/lib.rs&lt;/code&gt; file and replace its contents with:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-rust&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;nif&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type-builtin&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type-builtin&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type-builtin&quot;&gt;i64&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;span class=&quot;l-module&quot;&gt;rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-macro&quot;&gt;init!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Elixir.MyNif&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This defines a basic &lt;code&gt;add/2&lt;/code&gt; function exposed to Elixir.&lt;/p&gt;
&lt;h1&gt;Define the Elixir wrapper&lt;/h1&gt;
&lt;p&gt;In &lt;code&gt;lib/my_nif.ex&lt;/code&gt;, wire up the NIF:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyNif&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;otp_app: :my_nif_app&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;crate: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;my_nif&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-comment&quot;&gt;# Fallback when the NIF is not loaded&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-comment&quot;&gt;_a&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_b&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;:erlang&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;nif_error&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:nif_not_loaded&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Build and test&lt;/h1&gt;
&lt;p&gt;Compile the NIF:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;compile&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then try it out in &lt;code&gt;iex&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;$ &lt;span class=&quot;l-function-call&quot;&gt;iex&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;S&lt;/span&gt; mix&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;iex&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyNif&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-number&quot;&gt;5&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You've created a simple Elixir NIF using Rust. This lets you use Rust's performance and ecosystem while retaining Elixir's productivity.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/rust&quot;&gt;#rust&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/elixir&quot;&gt;#elixir&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/giving-a-laravel-artisan-command-an-alias</id>
    <title>🐥 Giving a Laravel Artisan Command an Alias</title>
    <updated>2026-11-06T18:00:00Z</updated>
    <published>2026-11-06T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/giving-a-laravel-artisan-command-an-alias"/>
    <content type="html">&lt;p&gt;Sooner or later, every codebase has a command whose name you can't remember. Say you have &lt;code&gt;reports:generate-monthly&lt;/code&gt;. Half the team types &lt;code&gt;reports:monthly&lt;/code&gt; from muscle memory and gets &amp;quot;Command not defined&amp;quot;. You don't want to rename the command because runbooks, scripts, and Slack history all reference the old name. So you give it a second name.&lt;/p&gt;
&lt;h1&gt;The short version&lt;/h1&gt;
&lt;p&gt;Aliases are a Symfony Console feature, and Artisan is built on Symfony Console. Laravel supports them in three ways.&lt;/p&gt;
&lt;h2&gt;1. The &lt;code&gt;$aliases&lt;/code&gt; property&lt;/h2&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-php&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-keyword-type&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;GenerateMonthlyReportCommand&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Command&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-keyword-modifier&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;$signature&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;reports:generate-monthly {reportId}&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-keyword-modifier&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;$description&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;Generates the monthly report for the given id&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-keyword-modifier&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;$aliases&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;reports:monthly&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Laravel's &lt;code&gt;Command&lt;/code&gt; constructor reads this property and calls Symfony's &lt;code&gt;setAliases()&lt;/code&gt; for you. This is the approach we use.&lt;/p&gt;
&lt;h2&gt;2. The &lt;code&gt;#[Aliases]&lt;/code&gt; attribute&lt;/h2&gt;
&lt;p&gt;Laravel 13 also has class attributes for command metadata:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-php&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-keyword-import&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Illuminate&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;Attributes&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;Aliases&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-attribute&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;#[&lt;/span&gt;Aliases&lt;span class=&quot;l-punctuation-bracket&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;reports:monthly&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;])]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-keyword-type&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;GenerateMonthlyReportCommand&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Command&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-comment&quot;&gt;// ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;#[Signature]&lt;/code&gt; attribute also accepts an &lt;code&gt;aliases&lt;/code&gt; argument, so the signature and its aliases can sit together. Use whichever style your commands already use.&lt;/p&gt;
&lt;h2&gt;3. Overriding &lt;code&gt;configure()&lt;/code&gt;&lt;/h2&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-php&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-keyword-modifier&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;configure&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-type-builtin&quot;&gt;void&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-variable-builtin&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;configure&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-variable-builtin&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-function-method-call&quot;&gt;setAliases&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;reports:monthly&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works on any Laravel version because it uses plain Symfony. On recent versions, you rarely need it.&lt;/p&gt;
&lt;h1&gt;What you get&lt;/h1&gt;
&lt;p&gt;Both names run the same command with the same arguments and options:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;php&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;artisan&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;reports:generate-monthly&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;123&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;php&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;artisan&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;reports:monthly&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;123&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;php artisan list&lt;/code&gt; shows the alias as its own entry and marks the canonical command with the alias in brackets, so people can see that the two names belong together:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;reports:monthly            Generates the monthly report for the given id&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;reports:generate-monthly   [reports:monthly] Generates the monthly report for the given id&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Gotchas&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;An alias is only a name.&lt;/strong&gt; Don't put arguments or options in it. The signature (&lt;code&gt;{reportId}&lt;/code&gt;) belongs to the command and is shared by every name. If you write &lt;code&gt;'reports:monthly {reportId}'&lt;/code&gt; as an alias, Symfony takes the whole string as the name, including the placeholder.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Aliases must be unique.&lt;/strong&gt; Run &lt;code&gt;php artisan list&lt;/code&gt; after adding one to check that it doesn't collide with another command.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scheduling and queues.&lt;/strong&gt; &lt;code&gt;Schedule::command('reports:monthly')&lt;/code&gt;, &lt;code&gt;$this-&amp;gt;call()&lt;/code&gt;, and &lt;code&gt;Artisan::call()&lt;/code&gt; all resolve aliases. Still, use the canonical name in code so a grep for the command finds every use.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Decide which name is canonical.&lt;/strong&gt; The alias is a convenience. If you want people to move to the new name, make it the &lt;code&gt;$signature&lt;/code&gt; name and keep the old one as the alias. That gives you a painless rename.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Test it.&lt;/strong&gt; Run the command through its alias once in a test so a typo in the alias doesn't go unnoticed.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/laravel&quot;&gt;#laravel&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/php&quot;&gt;#php&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/tools&quot;&gt;#tools&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/rsync-backups-that-respect-your-gitignore</id>
    <title>🐥 rsync backups that respect your .gitignore</title>
    <updated>2026-11-03T18:00:00Z</updated>
    <published>2026-11-03T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/rsync-backups-that-respect-your-gitignore"/>
    <content type="html">&lt;p&gt;A naive &lt;code&gt;rsync&lt;/code&gt; backup copies everything — including the megabytes of compiled assets, cached responses, and third-party dependencies you never actually need to restore. Here is how to make rsync smarter, without maintaining a separate exclude list.&lt;/p&gt;
&lt;h1&gt;The problem with a bare rsync&lt;/h1&gt;
&lt;p&gt;The simplest backup command looks like this:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;rsync&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--delete&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-avh&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;/src/&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;/dst/&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It works, but it faithfully copies &lt;code&gt;node_modules/&lt;/code&gt; (often hundreds of megabytes), compiled caches, vendor trees, and log files that rotate anyway. These are not worth transferring, not worth storing, and not worth restoring.&lt;/p&gt;
&lt;h2&gt;Explicit excludes for the obvious offenders&lt;/h2&gt;
&lt;p&gt;The first fix is straightforward: pass &lt;code&gt;--exclude&lt;/code&gt; flags for the directories you know you never want.&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;rsync&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--delete&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-avh&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;logs/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;node_modules/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;vendor/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;cache/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;/src/&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;/dst/&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The trailing slash on each pattern matters: it matches directories specifically, not files that happen to share the name.&lt;/p&gt;
&lt;h1&gt;Honouring .gitignore automatically&lt;/h1&gt;
&lt;p&gt;The more interesting problem is everything your &lt;code&gt;.gitignore&lt;/code&gt; already documents but &lt;code&gt;rsync&lt;/code&gt; doesn't know about. Build outputs, compiled assets, secrets files — you've already told git to ignore them. Telling rsync separately is just duplication.&lt;/p&gt;
&lt;p&gt;rsync has a &lt;code&gt;--filter&lt;/code&gt; option that solves this cleanly:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;# &amp;#39;:- .gitignore&amp;#39; means: merge each .gitignore as an exclude list&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;rsync&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--delete&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-avh&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--filter=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;:- .gitignore&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;logs/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;node_modules/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;vendor/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;cache/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;/src/&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;/dst/&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;:- .gitignore&lt;/code&gt; rule is a &lt;em&gt;dir-merge&lt;/em&gt; filter. As rsync descends into each directory, it reads that directory's &lt;code&gt;.gitignore&lt;/code&gt; and applies its patterns as excludes — recursively, the same way git itself does.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The &lt;code&gt;--filter&lt;/code&gt; flag reads the &lt;em&gt;contents&lt;/em&gt; of &lt;code&gt;.gitignore&lt;/code&gt; files, not the &lt;code&gt;.git/&lt;/code&gt; directory itself. Git's implicit exclusion of &lt;code&gt;.git/&lt;/code&gt; is not part of any &lt;code&gt;.gitignore&lt;/code&gt;, so those folders &lt;strong&gt;are&lt;/strong&gt; included in the backup. Add &lt;code&gt;--exclude='.git/'&lt;/code&gt; if you want to omit them.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;Why keep the explicit excludes too?&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;node_modules/&lt;/code&gt; and friends are almost always in &lt;code&gt;.gitignore&lt;/code&gt; already, so in practice the explicit flags are redundant for most projects. But they act as a safety net: if you're backing up a directory that has no &lt;code&gt;.gitignore&lt;/code&gt;, or an older project where someone forgot to gitignore the vendor folder, the explicit rules still fire. Belt and braces.&lt;/p&gt;
&lt;h1&gt;When --delete can't clean up after itself&lt;/h1&gt;
&lt;p&gt;Once you add &lt;code&gt;--filter=':- .gitignore'&lt;/code&gt;, you may see a warning like this:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;rsync: not empty, cannot delete: ./some-project/.git&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;rsync: not empty, cannot delete: ./some-project&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This happens when rsync's &lt;code&gt;--delete&lt;/code&gt; flag tries to remove a directory from the destination that no longer exists (or is now excluded) on the source — but can't fully empty it first. The cause is the exclude rules themselves: rsync won't touch files it has been told to skip, so if a directory contains excluded files, rsync leaves those in place, and then can't remove the non-empty parent.&lt;/p&gt;
&lt;p&gt;The fix is &lt;code&gt;--delete-excluded&lt;/code&gt;, which tells rsync to also delete destination files that match an exclude rule:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;rsync&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--delete&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--delete-excluded&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-avh&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--filter=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;:- .gitignore&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;logs/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;node_modules/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;vendor/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;cache/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;/src/&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;/dst/&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Worth knowing:&lt;/strong&gt; &lt;code&gt;--delete-excluded&lt;/code&gt; cuts both ways. If you add a pattern to a &lt;code&gt;.gitignore&lt;/code&gt; after a directory has already been backed up, the next run will remove those files from the destination too. For a mirror backup that should faithfully reflect the source, that is exactly the right behaviour — but it is worth being aware of.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;The final script&lt;/h1&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-directive&quot;&gt;#!/bin/bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;rsync&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--delete&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--delete-excluded&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-avh&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--filter=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;:- .gitignore&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;logs/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;node_modules/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;vendor/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;  &lt;span class=&quot;l-variable-parameter&quot;&gt;--exclude=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;cache/&amp;#39;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;  &lt;span class=&quot;l-string&quot;&gt;&amp;quot;/path/to/source/&amp;quot;&lt;/span&gt; \&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;  &lt;span class=&quot;l-string&quot;&gt;&amp;quot;/path/to/destination/&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Two lines of flags, one principle: let the projects describe what's ignorable, and make the backup command listen.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/tools&quot;&gt;#tools&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/best-practice&quot;&gt;#best-practice&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/devops&quot;&gt;#devops&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/git&quot;&gt;#git&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/linux&quot;&gt;#linux&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/ini-set-memory-limit-now-throws-in-php-8-5</id>
    <title>🐥 ini_set('memory_limit') Now Throws in PHP 8.5</title>
    <updated>2026-10-29T18:00:00Z</updated>
    <published>2026-10-29T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/ini-set-memory-limit-now-throws-in-php-8-5"/>
    <content type="html">&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; PHP 8.5 changed &lt;code&gt;ini_set('memory_limit', ...)&lt;/code&gt; to throw an &lt;code&gt;ErrorException&lt;/code&gt; when the requested value exceeds &lt;code&gt;max_memory_limit&lt;/code&gt;. Previously it silently capped. The fix is either to lower your requested limit, configure &lt;code&gt;max_memory_limit&lt;/code&gt; at the server level, or suppress the error when the cap is acceptable.&lt;/p&gt;
&lt;h1&gt;What happened&lt;/h1&gt;
&lt;p&gt;We upgraded a Laravel application to PHP 8.5 and a long-running Artisan import command immediately started failing with a cryptic error:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;ErrorException: Failed to set memory_limit to 4294967296 bytes.&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;Setting to max_memory_limit instead (currently: 536870912 bytes)&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  File &amp;quot;app/Console/Commands/ImportDataCommand.php&amp;quot;, line 12&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    ini_set(&amp;#39;memory_limit&amp;#39;, &amp;#39;4096M&amp;#39;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The command was requesting 4 GB of memory. The server's hard ceiling (&lt;code&gt;max_memory_limit&lt;/code&gt;) is 512 MB. On PHP 8.4 that call silently set the limit to 512 MB and kept going. On PHP 8.5 it throws.&lt;/p&gt;
&lt;h1&gt;The PHP 8.5 change&lt;/h1&gt;
&lt;p&gt;PHP has two memory-related INI settings:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;memory_limit&lt;/code&gt;&lt;/strong&gt; — The per-request limit a script can raise or lower at runtime via &lt;code&gt;ini_set()&lt;/code&gt;. Typically set in &lt;code&gt;php.ini&lt;/code&gt; or a per-vhost config.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;max_memory_limit&lt;/code&gt;&lt;/strong&gt; — A hard ceiling added in PHP 8.5. Only changeable at the system level (php.ini, not at runtime). Scripts cannot exceed it.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Before 8.5, setting &lt;code&gt;memory_limit&lt;/code&gt; above the OS-level ceiling either worked (no hard enforcement) or was silently clamped. PHP 8.5 introduced &lt;code&gt;max_memory_limit&lt;/code&gt; as a formal concept and made the runtime enforce it with an error — because silently ignoring the requested value is surprising and hard to debug.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This is a &lt;strong&gt;silent-to-loud&lt;/strong&gt; change. Code that appeared to work fine on 8.4 — because the limit was quietly capped — will now throw on 8.5 if the cap is exceeded.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;The three real fixes&lt;/h1&gt;
&lt;h2&gt;1. Raise &lt;code&gt;max_memory_limit&lt;/code&gt; on the server (correct, if you own the server)&lt;/h2&gt;
&lt;p&gt;If the command genuinely needs 4 GB, the honest fix is to grant it at the system level. Add this to your &lt;code&gt;php.ini&lt;/code&gt; or CLI php.ini:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-ini&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;max_memory_limit&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt; 4096M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;memory_limit&lt;/span&gt;     &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt; 4096M&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then remove the &lt;code&gt;ini_set&lt;/code&gt; from the command entirely. Runtime limits belong in configuration, not in application code.&lt;/p&gt;
&lt;h2&gt;2. Pass the limit via the CLI (good for scheduled commands)&lt;/h2&gt;
&lt;p&gt;If you run the command from a scheduler or cron, set the limit as a CLI flag rather than from inside the script:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;php&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;memory_limit=4096M&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;artisan&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;import:data&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This keeps the application code clean and makes the resource budget visible at the call site — right where your ops team or scheduler can see and change it.&lt;/p&gt;
&lt;h2&gt;3. Suppress the error and accept the cap (quick, pragmatic)&lt;/h2&gt;
&lt;p&gt;If the command works fine within the system's 512 MB ceiling, suppress the error with &lt;code&gt;@&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-php&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;// Requests 4096M, silently accepts the max if lower&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;ini_set&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;memory_limit&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;4096M&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; Only use this if you are genuinely okay with whatever the server's ceiling is. If the command &lt;em&gt;actually needs&lt;/em&gt; 4 GB and only gets 512 MB, you have suppressed a real warning and set yourself up for an OOM crash later.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;4. Use &lt;code&gt;-1&lt;/code&gt; (unlimited)&lt;/h2&gt;
&lt;p&gt;Passing &lt;code&gt;-1&lt;/code&gt; removes the PHP-level cap entirely and relies on the OS to manage memory. This does not trigger the &lt;code&gt;max_memory_limit&lt;/code&gt; check:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-php&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;ini_set&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;memory_limit&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;-1&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; Unlimited memory is a footgun on shared hosts or containers with soft limits. Use it only for trusted CLI commands on dedicated infrastructure.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;What we did&lt;/h1&gt;
&lt;p&gt;The command runs as a scheduled job on a dedicated server. We moved the memory configuration out of the PHP script and into the cron invocation:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;# Scheduler entry (before)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;php&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;artisan&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;import:data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;# Scheduler entry (after)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;php&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;memory_limit=4096M&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;artisan&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;import:data&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And removed the &lt;code&gt;ini_set&lt;/code&gt; from the command's &lt;code&gt;handle()&lt;/code&gt; method entirely. The command no longer makes assumptions about its own resource budget — that's the scheduler's job.&lt;/p&gt;
&lt;h1&gt;How to find this in your own codebase&lt;/h1&gt;
&lt;p&gt;Before upgrading to PHP 8.5, grep your codebase for runtime memory overrides:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-rn&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;ini_set.*memory_limit&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;app/&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;bootstrap/&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Any hit that requests more than your server's &lt;code&gt;max_memory_limit&lt;/code&gt; will throw. Check what your server's ceiling is with:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;php&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-r&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;echo ini_get(&amp;#39;max_memory_limit&amp;#39;);&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If that returns empty or &lt;code&gt;-1&lt;/code&gt;, no ceiling is currently configured and you are fine until an admin adds one.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.php.net/releases/8.5/en.php&quot;&gt;PHP 8.5 release notes&lt;/a&gt; cover this change under the INI settings section. The RFC that introduced &lt;code&gt;max_memory_limit&lt;/code&gt; is &lt;a href=&quot;https://wiki.php.net/rfc/max_memory_limit&quot;&gt;wiki.php.net/rfc/max_memory_limit&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/development&quot;&gt;#development&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/laravel&quot;&gt;#laravel&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/php&quot;&gt;#php&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/smarter-pr-size-labels-with-weighted-scoring</id>
    <title>🐥 Smarter PR Size Labels with Weighted Scoring</title>
    <updated>2026-10-27T18:00:00Z</updated>
    <published>2026-10-27T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/smarter-pr-size-labels-with-weighted-scoring"/>
    <content type="html">&lt;p&gt;At work, we do a lot of code review. Like most teams, we've learned the hard way that a 1,500-line PR is not the same as a 500-line PR—even if the diff viewer disagrees.&lt;/p&gt;
&lt;p&gt;We've been running a GitHub Actions workflow that automatically labels every pull request with a size badge: &lt;code&gt;small&lt;/code&gt;, &lt;code&gt;medium&lt;/code&gt;, or &lt;code&gt;large&lt;/code&gt;. Recently, we overhauled it, and the improvements are worth sharing.&lt;/p&gt;
&lt;h1&gt;Why bother labelling PR size?&lt;/h1&gt;
&lt;p&gt;A size label on a PR does one simple thing: it sets expectations before anyone opens the diff.&lt;/p&gt;
&lt;p&gt;A reviewer who sees &lt;code&gt;small&lt;/code&gt; knows they can knock it out between two meetings. Someone who sees &lt;code&gt;large&lt;/code&gt; knows to carve out an hour and bring coffee. And if the whole team can see the size distribution in the pull request list at a glance, they can prioritise review queues more effectively and spot when a feature branch is growing out of hand before it becomes a problem.&lt;/p&gt;
&lt;p&gt;It also creates a gentle forcing function for authors. When you know the workflow is going to slap a &lt;code&gt;large&lt;/code&gt; on your PR, you think twice about whether those refactors really need to live in the same commit.&lt;/p&gt;
&lt;h1&gt;The naive approach and its problems&lt;/h1&gt;
&lt;p&gt;The first version of our workflow was simple: count additions, count deletions, and compare the total to a threshold.&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;score = additions + deletions&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That works, but it has some obvious blind spots:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Deleting code is easy to review.&lt;/strong&gt; A file removal or a big &lt;code&gt;git rm&lt;/code&gt; shouldn't count the same as a 200-line feature addition. The reviewer isn't absorbing new logic; they're confirming that something is gone.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Test files are verbose by design.&lt;/strong&gt; A well-tested change might add 300 lines of test code alongside 80 lines of feature code. Penalising it the same as 380 lines of business logic discourages good testing habits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Some PRs are simply not reviewable.&lt;/strong&gt; Our old workflow topped out at &lt;code&gt;large&lt;/code&gt; regardless of score. A 5,000-line PR got the same label as a 700-line one, which gave reviewers no signal that they were looking at something that genuinely needed to be split.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;The weighted scoring model&lt;/h1&gt;
&lt;p&gt;Our new approach scores each file individually, with different weights depending on the type of change.&lt;/p&gt;
&lt;h2&gt;Three file categories&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Deleted files&lt;/strong&gt; (&lt;code&gt;status === 'removed'&lt;/code&gt;): the entire file is going away. There's almost no cognitive load for a reviewer—they need to confirm the deletion is intentional and check for stray references. We weight the lines at 20% and add a flat 10 to the score per file.&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-javascript&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;additions&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;deletions&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0.2&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Test files&lt;/strong&gt;: identified by a set of patterns covering the conventions we actually use (&lt;code&gt;*Test.php&lt;/code&gt;, &lt;code&gt;*Tests.php&lt;/code&gt;, &lt;code&gt;*.spec.ts&lt;/code&gt;, &lt;code&gt;*.test.ts&lt;/code&gt;, and paths under &lt;code&gt;tests/&lt;/code&gt; or &lt;code&gt;test/&lt;/code&gt;). These are important, but they're also repetitive—a reviewer can skim a block of assertions much faster than an equivalent block of business logic. Additions are weighted at 50%, deletions at 15%, and each test file adds 10 to the flat score.&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-javascript&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;additions&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;deletions&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0.15&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Everything else&lt;/strong&gt;: regular source files count additions at full weight, deletions at 30%, and each file adds 20 to the flat score. The per-file component matters because touching ten files in different parts of the system—even with few lines changed—still takes real review time to understand the blast radius.&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-javascript&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;additions&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-variable-member&quot;&gt;deletions&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0.3&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Thresholds&lt;/h2&gt;
&lt;p&gt;Once we have the weighted score, we bucket it:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Label&lt;/th&gt;
&lt;th&gt;Score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;small&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;≤ 250&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;medium&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;≤ 900&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;large&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;≤ 1800&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;not-reviewable&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&amp;gt; 1800&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;We deliberately raised the thresholds compared to our first version. The old numbers were too aggressive—a well-structured change with a handful of files and some tests would land in &lt;code&gt;medium&lt;/code&gt; even when it was genuinely easy to review. The new thresholds better reflect the actual reviewer experience.&lt;/p&gt;
&lt;h2&gt;The &lt;code&gt;not-reviewable&lt;/code&gt; label&lt;/h2&gt;
&lt;p&gt;This is new, and it's probably the most useful addition. When a PR scores above 1,800, it gets labelled &lt;code&gt;not-reviewable&lt;/code&gt;. That's a signal to the author—not a judgment, just a fact—that the PR is too large to give a meaningful review.&lt;/p&gt;
&lt;p&gt;It's also a signal to reviewers that they're not expected to wade through it; the author needs to split it first.&lt;/p&gt;
&lt;p&gt;The label swaps in and out cleanly alongside the others: if a PR starts as &lt;code&gt;not-reviewable&lt;/code&gt;, gets split, and the score drops, the next push removes the label and applies the correct one.&lt;/p&gt;
&lt;h1&gt;Ignoring noise&lt;/h1&gt;
&lt;p&gt;The workflow reads a &lt;code&gt;.github/pr-size-ignore.txt&lt;/code&gt; file where you can list glob patterns for files that should be excluded entirely from scoring. Ours includes things like lock files, generated assets, and vendored code. There's no point penalising a PR because Composer updated a JSON file.&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;# .github/pr-size-ignore.txt&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;composer.lock&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;package-lock.json&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;*.min.js&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;public/build/**&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;The full workflow&lt;/h1&gt;
&lt;p&gt;The workflow triggers on &lt;code&gt;pull_request_target&lt;/code&gt; (opened, synchronised, or reopened), skips Dependabot PRs, and needs &lt;code&gt;pull-requests: write&lt;/code&gt; and &lt;code&gt;issues: write&lt;/code&gt; permissions to manage labels.&lt;/p&gt;
&lt;p&gt;It paginates through the file list (GitHub caps the API at 3,000 files), filters out ignored paths, scores each file with the logic above, determines the label, removes any stale size label, and applies the new one. The whole thing runs in a single &lt;code&gt;actions/github-script&lt;/code&gt; step with no external action dependencies beyond &lt;code&gt;actions/checkout&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The label swap is idempotent: if the correct label is already on the PR, no API call is made.&lt;/p&gt;
&lt;h1&gt;Full workflow source&lt;/h1&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-yaml&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;PR Size Label&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;pull_request_target&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;types&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;opened&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;synchronize&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;reopened&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;permissions&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;contents&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;read&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;pull-requests&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;write&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;issues&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;write&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;&lt;span class=&quot;l-property&quot;&gt;jobs&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;  &lt;span class=&quot;l-property&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;runs-on&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;ubuntu-latest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;github.actor != &amp;#39;dependabot[bot]&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;    &lt;span class=&quot;l-property&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;      &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;actions/checkout@v4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;      &lt;span class=&quot;l-punctuation-delimiter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;Apply size label&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;actions/github-script@v7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;        &lt;span class=&quot;l-property&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;23&quot;&gt;          &lt;span class=&quot;l-property&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;24&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;fs&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;fs&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;25&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;path&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;26&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;27&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;Read&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;ignore&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;patterns&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;.github/pr-size-ignore.txt&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;28&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;ignoreFile&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;.github/pr-size-ignore.txt&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;29&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;ignorePatterns&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;fs.existsSync&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;ignoreFile&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;30&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-call&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;fs.readFileSync&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;ignoreFile,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;utf8&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;31&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  &lt;span class=&quot;l-function-call&quot;&gt;.split&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;\n&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;32&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  &lt;span class=&quot;l-function-call&quot;&gt;.map&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string-special-path&quot;&gt;l.trim&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;())&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;33&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  &lt;span class=&quot;l-function-call&quot;&gt;.filter&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string-special-path&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;!l.startsWith&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;#&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;34&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-builtin&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;35&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;36&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-keyword-function&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;globToRegex&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;pattern&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;37&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-builtin&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;38&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-keyword-repeat&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-builtin&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;l-string-special-path&quot;&gt;pattern.length&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;i++&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;39&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;pattern[i]&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;40&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-keyword-conditional&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;*&amp;#39;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;pattern&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;i &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;*&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;41&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  &lt;span class=&quot;l-function-call&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;.*&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;42&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  &lt;span class=&quot;l-function-call&quot;&gt;i++&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;43&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  &lt;span class=&quot;l-keyword-conditional&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;pattern&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;i &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;/&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; i++&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;44&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;*&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;45&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  &lt;span class=&quot;l-function-call&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;[^/]*&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;46&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;?&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;47&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  &lt;span class=&quot;l-function-call&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;[^/]&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;48&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;.&amp;#39;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;+&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;^&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;$&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;{&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;}&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;(&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;)&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;|&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;[&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;]&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;\\&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;].includes&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;49&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  &lt;span class=&quot;l-function-call&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;\\&amp;#39;&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;50&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; else &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;51&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  &lt;span class=&quot;l-function-call&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;52&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;53&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-call&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;54&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              return new RegExp&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;^&amp;#39;&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;$&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;55&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;56&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;57&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-keyword-function&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;isIgnored&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;filePath,&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;patterns&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;58&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-builtin&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;patterns.some&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string-special-path&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;59&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;regex&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;globToRegex&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;pattern&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;60&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;Patterns&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;without&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;slash&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;against&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;basename&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;well&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;61&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-keyword-conditional&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;!pattern.includes&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;/&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;!pattern.startsWith&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;**/&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;62&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  &lt;span class=&quot;l-function-builtin&quot;&gt;return&lt;/span&gt; regex.test&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;filePath&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;regex.test&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;path.basename&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;filePath&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;63&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;64&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-builtin&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;regex.test&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;filePath&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;65&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-call&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;66&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;67&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;68&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;Fetch&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;all&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;changed&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;files&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;paginated, GitHub caps at 3 000)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;69&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            const allFiles = [];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;70&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            for (let page = 1; ; page++) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;71&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              const { data: batch &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;github.rest.pulls.listFiles&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;({&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;72&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;owner:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;context.repo.owner,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;73&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;repo:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;context.repo.repo,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;74&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;pull_number:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;context.issue.number,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;75&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;per_page:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;100,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;76&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;page,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;77&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;78&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-call&quot;&gt;allFiles.push&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;...batch&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;79&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-keyword-conditional&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;batch.length&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; break&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;80&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;81&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;82&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;relevant&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;allFiles.filter&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;f =&amp;gt; !isIgnored(f.filename, ignorePatterns));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;83&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;84&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            function isTestFile(filePath) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;85&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              const base = path.basename(filePath);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;86&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              return (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;87&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                /Test(s)?\.php$/.test(base) ||&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;88&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                /\.(spec|test)\.(ts|js|tsx|jsx)$/.test(base) ||&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;89&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                /^tests\//.test(filePath) ||&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;90&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                /\/tests\//.test(filePath) ||&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;91&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                /\/test\//.test(filePath)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;92&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;93&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;94&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;95&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            // &lt;span class=&quot;l-variable-parameter&quot;&gt;Score&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;weighted&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;contributions:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;96&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;Deleted&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;files:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;20&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;weight,&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;10&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;97&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;Test&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;files:&lt;/span&gt;    &lt;span class=&quot;l-variable-parameter&quot;&gt;additions&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;50&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;%,&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;deletions&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;15&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;%,&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;10&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;98&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;Regular&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;files:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;additions&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;%,&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;deletions&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;30&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;%,&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;20&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;99&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-builtin&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;100&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-builtin&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;additions&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;0,&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;deletions&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;101&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-keyword-repeat&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;relevant&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;102&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-call&quot;&gt;additions&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;f.additions&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;103&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-call&quot;&gt;deletions&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;f.deletions&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;104&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-keyword-conditional&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;f.status&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;removed&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;105&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;f.additions + f.deletions) * 0.2 + 10;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;106&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-keyword-conditional&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;l-keyword-conditional&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;isTestFile&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;f.filename&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;107&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;f.additions&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;f.deletions&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;0.15&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;108&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-call&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;109&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;f.additions&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;f.deletions&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;0.3&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;110&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-call&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;111&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;112&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;113&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;filesChanged&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;relevant.length&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;114&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;115&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;core.info&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;`&lt;span class=&quot;l-function-call&quot;&gt;Relevant&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;files:&lt;/span&gt; &lt;span class=&quot;l-punctuation-special&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;filesChanged&lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;additions&lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;deletions&lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;score:&lt;/span&gt; ${&lt;span class=&quot;l-variable&quot;&gt;score&lt;/span&gt;.toFixed(1)&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;`&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;116&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;117&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;sizeLabel&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-string-special-path&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;250&lt;/span&gt; &lt;span class=&quot;l-string-special-path&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;small&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;118&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                            &lt;span class=&quot;l-function-builtin&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-string-special-path&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;900&lt;/span&gt; &lt;span class=&quot;l-string-special-path&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;medium&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;119&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                            &lt;span class=&quot;l-function-builtin&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;l-string-special-path&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;1800&lt;/span&gt; &lt;span class=&quot;l-string-special-path&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;large&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;120&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                            &lt;span class=&quot;l-function-builtin&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;not-reviewable&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;121&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;122&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;Swap&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;any&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;existing&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;label&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;123&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;SIZE_LABELS&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;#39;small&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;medium&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;large&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;not-reviewable&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-variable-parameter&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;124&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;data:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;currentLabels&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;github.rest.issues.listLabelsOnIssue&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;({&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;125&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-call&quot;&gt;owner:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;context.repo.owner,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;126&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-call&quot;&gt;repo:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;context.repo.repo,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;127&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-call&quot;&gt;issue_number:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;context.issue.number,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;128&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;129&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-keyword-repeat&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;const label of currentLabels) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;130&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              if (SIZE_LABELS.includes(label.name) &amp;amp;&amp;amp; label.name !== sizeLabel) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;131&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                await github.rest.issues.removeLabel({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;132&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  owner: context.repo.owner,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;133&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  repo: context.repo.repo,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;134&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  issue_number: context.issue.number,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;135&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                  name: label.name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;136&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;137&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;138&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;139&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-keyword-conditional&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;!currentLabels.some&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-string-special-path&quot;&gt;l.name&lt;/span&gt; &lt;span class=&quot;l-string-special-path&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;l-string-special-path&quot;&gt;sizeLabel&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;140&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-function-call&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;github.rest.issues.addLabels&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;({&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;141&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;owner:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;context.repo.owner,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;142&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;repo:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;context.repo.repo,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;143&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;issue_number:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;context.issue.number,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;144&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;                &lt;span class=&quot;l-function-call&quot;&gt;labels:&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;[sizeLabel],&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;145&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;              &lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;146&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;147&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;148&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;            &lt;span class=&quot;l-function-call&quot;&gt;core.info&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;`&lt;span class=&quot;l-function-call&quot;&gt;Applied&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;label:&lt;/span&gt; &lt;span class=&quot;l-punctuation-special&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;sizeLabel&lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;}&lt;/span&gt;`&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Things to keep in mind&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;Create the &lt;code&gt;not-reviewable&lt;/code&gt; label first.&lt;/strong&gt; GitHub's label API throws if you try to apply a label that doesn't exist in the repository. Add it once through the UI or with the API before the workflow runs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Test file detection is heuristic.&lt;/strong&gt; The patterns we use match our conventions. If your project puts tests in a &lt;code&gt;__tests__&lt;/code&gt; directory or uses a &lt;code&gt;.test.rb&lt;/code&gt; suffix, you'll need to adjust the &lt;code&gt;isTestFile&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The thresholds are starting points.&lt;/strong&gt; What counts as &lt;code&gt;small&lt;/code&gt; depends entirely on your team's pace and context. We tuned ours over a few weeks by looking at which PRs felt easy and which felt painful, and adjusting until the labels matched our intuition. Start with something reasonable and iterate.&lt;/p&gt;
&lt;p&gt;PR size labels are a low-effort, high-signal addition to any review workflow. The key insight behind the weighted model is that &lt;em&gt;not all lines are equal&lt;/em&gt;. A deleted file, a test assertion, and a new service class represent very different amounts of reviewer work. Treating them the same produces labels that mislead; treating them differently produces labels that actually help.&lt;/p&gt;
&lt;p&gt;The workflow is about 100 lines of JavaScript inside a YAML file. It's easy to read, easy to adapt, and it has meaningfully changed how we queue and prioritise reviews at Contractify.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/javascript&quot;&gt;#javascript&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/development&quot;&gt;#development&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/best-practice&quot;&gt;#best-practice&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/github&quot;&gt;#github&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/git&quot;&gt;#git&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/debugging-a-502-on-a-signed-redirect-nginxs-upstream-sent-too-big-header</id>
    <title>🐥 Debugging a 502 on a Signed Redirect: nginx's &quot;upstream sent too big header&quot;</title>
    <updated>2026-10-26T18:00:00Z</updated>
    <published>2026-10-26T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/debugging-a-502-on-a-signed-redirect-nginxs-upstream-sent-too-big-header"/>
    <content type="html">&lt;p&gt;You've built a signed redirect endpoint. It validates a token, generates a URL, and sends the browser on its way with a &lt;code&gt;302&lt;/code&gt;. It works perfectly in local development but fails with a cryptic &lt;strong&gt;502 Bad Gateway&lt;/strong&gt; in staging. Here's how to diagnose and fix it without touching your web server config.&lt;/p&gt;
&lt;h1&gt;The symptom&lt;/h1&gt;
&lt;p&gt;A &lt;code&gt;curl -v&lt;/code&gt; request to the endpoint returns nginx's own 502 page—not a Laravel or application error page, just nginx's default 150-byte HTML. That detail matters: it means PHP-FPM never delivered a response nginx could forward. The application may have produced &lt;em&gt;something&lt;/em&gt;, but nginx rejected it before the browser ever saw it.&lt;/p&gt;
&lt;h1&gt;Finding the root cause&lt;/h1&gt;
&lt;p&gt;The nginx error log is the first place to look:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;upstream sent too big header while reading response header from upstream&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That single line explains everything. nginx has a default buffer for reading response headers from an upstream FastCGI process (&lt;code&gt;fastcgi_buffer_size&lt;/code&gt;, which defaults to 4 KB). The &lt;code&gt;302&lt;/code&gt; response your application returned was otherwise valid, but its &lt;code&gt;Location&lt;/code&gt; header was too long to fit in that buffer. nginx dropped the whole response and returned a 502.&lt;/p&gt;
&lt;p&gt;This happens frequently with e-signature providers, OAuth flows, and any service that encodes its entire state (session, return URL, signature, expiry) in a redirect URL. Those URLs routinely run to several kilobytes.&lt;/p&gt;
&lt;h1&gt;Why it only fails in staging&lt;/h1&gt;
&lt;p&gt;Local development typically uses the built-in PHP server or a framework dev server, neither of which imposes nginx's header buffer limits. The problem is invisible until nginx sits between the application and the browser—which is the case in every hosted environment but rarely in local development.&lt;/p&gt;
&lt;h1&gt;The obvious fix and why to think twice&lt;/h1&gt;
&lt;p&gt;The textbook answer is to raise nginx's buffer size:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-nginx&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;fastcgi_buffer_size&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;32k&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;fastcgi_buffers&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;32k&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works, but it requires access to the server config and a reload. It also papers over the underlying issue: you're transmitting a very large URL in an HTTP header, which is inherently fragile. Different proxies, CDNs, and load balancers have their own header size limits, and you may hit another one later.&lt;/p&gt;
&lt;h1&gt;A better fix: move the URL out of the header&lt;/h1&gt;
&lt;p&gt;HTTP redirects put the destination in a &lt;code&gt;Location&lt;/code&gt; &lt;em&gt;header&lt;/em&gt;. If instead you put the URL in the response &lt;em&gt;body&lt;/em&gt;, header buffer limits are irrelevant—response bodies are streamed, not buffered in header-size chunks.&lt;/p&gt;
&lt;p&gt;The simplest implementation is a minimal HTML page with a &lt;code&gt;&amp;lt;meta&amp;gt;&lt;/code&gt; refresh and a JavaScript fallback:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-php&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-variable&quot;&gt;$url&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable-builtin&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;l-function-method-call&quot;&gt;buildSigningUrl&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;$signer&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-keyword-return&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-string&quot;&gt;&amp;#39;&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&amp;lt;meta http-equiv=&amp;quot;refresh&amp;quot; content=&amp;quot;0;url=&amp;#39;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;$url&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;&amp;quot;&amp;gt;&amp;lt;/head&amp;gt;&amp;#39;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-string&quot;&gt;&amp;#39;&amp;lt;body&amp;gt;&amp;lt;script&amp;gt;window.location.replace(&amp;#39;&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;json_encode&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;$url&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;)&amp;lt;/script&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The browser executes the redirect client-side in under a frame. From the user's perspective, it's indistinguishable from a &lt;code&gt;302&lt;/code&gt;. From nginx's perspective, it's a normal 200 response with a small body—no oversized headers and no 502.&lt;/p&gt;
&lt;h2&gt;Why not use a URL shortener or cache indirection?&lt;/h2&gt;
&lt;p&gt;An alternative is to store the long URL behind a short token and redirect the browser to a local endpoint that looks it up:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;/esign/{uuid}?redirect=1  →  /esign/forward/{token}  →  {long signing URL}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;em&gt;first&lt;/em&gt; redirect is short and fits in nginx's buffer. But the &lt;em&gt;second&lt;/em&gt; redirect—from your forward endpoint to the signing URL—still goes through PHP-FPM and nginx, still puts the long URL in a &lt;code&gt;Location&lt;/code&gt; header, and still hits the same limit. You've added a round trip without solving anything.&lt;/p&gt;
&lt;p&gt;The only indirection that works is one where the browser makes the final hop to the long URL, not your server—which is exactly what the HTML body approach does.&lt;/p&gt;
&lt;h1&gt;The diagnostic checklist&lt;/h1&gt;
&lt;p&gt;If you hit a 502 on a redirect endpoint and can't reproduce it locally:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;curl -v&lt;/code&gt; the URL—check whether the response is an application error page or nginx's own 502 page. Nginx's own page means the problem is between nginx and upstream, not inside your application.&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;/var/log/nginx/error.log&lt;/code&gt; around the timestamp. &lt;code&gt;upstream sent too big header&lt;/code&gt; points to buffer limits; &lt;code&gt;connect() failed&lt;/code&gt; or &lt;code&gt;upstream timed out&lt;/code&gt; point elsewhere.&lt;/li&gt;
&lt;li&gt;Check PHP-FPM's log for crashes or OOM kills if the nginx log is silent.&lt;/li&gt;
&lt;li&gt;Measure the URL you're redirecting to. If it's over 3–4 KB, the buffer is likely the culprit.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The pattern generalizes beyond PHP. Any application server behind nginx—Python/WSGI, Node, or Go—can trigger the same error with a large redirect. The fix is the same: put the long URL in the body, not a header.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/laravel&quot;&gt;#laravel&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/php&quot;&gt;#php&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/http&quot;&gt;#http&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/devops&quot;&gt;#devops&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/why-phps-recent-critical-rces-are-an-architectural-non-problem-in-elixir</id>
    <title>🐥 Why PHP's Recent Critical RCEs Are an Architectural Non-Problem in Elixir</title>
    <updated>2026-10-25T18:00:00Z</updated>
    <published>2026-10-25T18:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/why-phps-recent-critical-rces-are-an-architectural-non-problem-in-elixir"/>
    <content type="html">&lt;p&gt;The cybersecurity landscape was shaken in October 2026 by the disclosure of two critical, unauthenticated Remote Code Execution (RCE) vulnerabilities in PHP affecting versions 8.2 through 8.5. Rated at a severe &lt;a href=&quot;https://www.first.org/cvss/v4-0/&quot;&gt;CVSS v4.0&lt;/a&gt; score of 9.2, these vulnerabilities allow attackers to compromise servers before any application-level security code even executes.&lt;/p&gt;
&lt;p&gt;Whenever major security events occur in mainstream runtime environments, it is worth examining how alternative language ecosystems handle the same vectors. For teams using Elixir and the BEAM virtual machine, these specific vulnerabilities are not just avoided by careful coding—they are entirely neutralized by the runtime's underlying architecture.&lt;/p&gt;
&lt;p&gt;Here is a deep technical breakdown of why the structural flaws exposed in PHP's 2026 advisories are a non-problem in Elixir, followed by how to safely manage memory and package security in the BEAM ecosystem.&lt;/p&gt;
&lt;h1&gt;Vulnerability 1: The OOM Partial Initialization Attack (CVE-2026-109618)&lt;/h1&gt;
&lt;h2&gt;What Happened in PHP&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://nvd.nist.gov/vuln/detail/CVE-2026-109618&quot;&gt;CVE-2026-109618&lt;/a&gt; (tracked via &lt;a href=&quot;https://github.com/advisories/GHSA-w8hw-w9fx-j79w&quot;&gt;GHSA-w8hw-w9fx-j79w&lt;/a&gt;) describes an Out-of-Memory (OOM) corruption flaw. When a web request hits a PHP worker, the runtime allocates memory to initialize internal request data structures. If an allocation failure occurs mid-initialization, the process is interrupted, leaving the request data in a partially formed state.&lt;/p&gt;
&lt;p&gt;During the subsequent request cleanup phase, PHP's memory management engine misinterprets this malformed, incomplete internal data as valid data structure pointers. Attackers can weaponize this behavior to hijack the pointer execution path, achieving unauthenticated Remote Code Execution.&lt;/p&gt;
&lt;h2&gt;Why It Is a Non-Problem in Elixir&lt;/h2&gt;
&lt;p&gt;The BEAM virtual machine manages memory in a completely decentralized fashion compared to traditional monolithic runtimes.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Per-Process Heaps:&lt;/strong&gt; In Elixir, every concurrent operation (like an incoming HTTP request handled by &lt;a href=&quot;https://github.com/ninenines/cowboy&quot;&gt;Cowboy&lt;/a&gt; or &lt;a href=&quot;https://github.com/mtrudel/bandit&quot;&gt;Bandit&lt;/a&gt; in a &lt;a href=&quot;https://phoenixframework.org&quot;&gt;Phoenix&lt;/a&gt; application) runs inside its own isolated, lightweight BEAM process. Each process contains its own private heap. There is no massive, shared global heap for request initialization.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Fail-Stop Mentality (&amp;quot;Let it Crash&amp;quot;):&lt;/strong&gt; If an Elixir process encounters a fatal error—such as allocating memory beyond a configured limit—the runtime does not attempt a delicate, complex cleanup dance over partially initialized, untyped memory structures. The BEAM instantly kills the isolated process.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Immutability and Type Safety:&lt;/strong&gt; Because data structures in Elixir are immutable and managed strictly by a type-safe virtual machine layer, a crash during process creation simply causes the supervision tree to discard the process ID (PID) and reclaim the isolated heap in one sweeping, atomic operation. There are no dangling initialization pointers to misinterpret.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Defensive Engineering: Monitoring BEAM Process Memory Thresholds&lt;/h2&gt;
&lt;p&gt;While an OOM error cannot corrupt the BEAM runtime state, a runaway process consuming too much memory can still starve the operating system. Elixir allows you to proactively enforce memory limits at the individual process level to safely drop abnormal requests before they impact the node.&lt;/p&gt;
&lt;p&gt;You can set a max memory threshold dynamically for temporary request processes using &lt;a href=&quot;https://hexdocs.pm/elixir/Process.html#flag/2&quot;&gt;&lt;code&gt;Process.flag/2&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;# Enforcing a 50MB memory ceiling on a specific task process&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-module&quot;&gt;Task.Supervisor&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;start_child&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;MyTaskSupervisor&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-module&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;flag&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:max_heap_size&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;size: &lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;6_250_000&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;kill: &lt;/span&gt;&lt;span class=&quot;l-boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;error_logger: &lt;/span&gt;&lt;span class=&quot;l-boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;  &lt;span class=&quot;l-comment&quot;&gt;# Process payload here...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;perform_heavy_lifting&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;size&lt;/code&gt;:&lt;/strong&gt; Measured in machine words (1 word = 8 bytes on 64-bit systems). &lt;code&gt;6_250_000&lt;/code&gt; words equals exactly 50 MB.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;kill: true&lt;/code&gt;:&lt;/strong&gt; Instructs the BEAM to instantly kill the process if it crosses the limit, rather than letting it slow down the node with emergency garbage collection loops.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;error_logger: true&lt;/code&gt;:&lt;/strong&gt; Automatically logs a descriptive crash dump so security and operations teams can audit potential memory-exhaustion attack vectors.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Vulnerability 2: Stale Worker Global State (CVE-2026-109619)&lt;/h1&gt;
&lt;h2&gt;What Happened in PHP&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://nvd.nist.gov/vuln/detail/CVE-2026-109619&quot;&gt;CVE-2026-109619&lt;/a&gt; (tracked via &lt;a href=&quot;https://github.com/advisories/GHSA-6wmf-4rv5-p3rp&quot;&gt;GHSA-6wmf-4rv5-p3rp&lt;/a&gt;) is a Use-After-Free variant tied to persistent worker state. PHP setups typically rely on persistent processes (like &lt;a href=&quot;https://www.php.net/manual/en/install.fpm.php&quot;&gt;PHP-FPM&lt;/a&gt;, LiteSpeed, or Apache mod_php modules) to handle thousands of serial HTTP requests over their lifespan to avoid cold-start overhead.&lt;/p&gt;
&lt;p&gt;In this exploit, request-specific internal state managed to persist in the worker's memory space even after its backing allocation was freed. When a subsequent, unrelated HTTP request was assigned to that exact same worker thread, the worker referenced that stale, dangling memory state before normal initialization routines could clean it. This state bleeding creates a predictable memory window for arbitrary code execution. An informational advisory (&lt;a href=&quot;https://nvd.nist.gov/vuln/detail/CVE-2026-115560&quot;&gt;CVE-2026-115560&lt;/a&gt;) was later issued regarding session upload progress handlers interacting with this state.&lt;/p&gt;
&lt;h2&gt;Why It Is a Non-Problem in Elixir&lt;/h2&gt;
&lt;p&gt;This vulnerability represents a classic failure of the Shared-State Architecture. Elixir completely eradicates this entire attack vector using a Share-Nothing Architecture driven by the &lt;a href=&quot;https://en.wikipedia.org/wiki/Actor_model&quot;&gt;Actor Model&lt;/a&gt;.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Zero State Leakage Between Requests:&lt;/strong&gt; When a web request reaches an Elixir web server, a completely new BEAM process is spawned. When the response is sent, that process terminates. Because memory is not shared between processes, it is physically impossible for Request B to access, inherit, or accidentally execute memory allocated by Request A.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;No Mutability, No Side-Effects:&lt;/strong&gt; A major factor in the PHP exploit is that runtime configuration and state can be mutated dynamically at request runtime. In Elixir, data is entirely immutable. You cannot mutate an underlying global memory buffer during a request lifecycle because global mutable state does not exist in the traditional sense. Even stateful storage (like an &lt;a href=&quot;https://hexdocs.pm/elixir/Agent.html&quot;&gt;&lt;code&gt;Agent&lt;/code&gt;&lt;/a&gt; or &lt;a href=&quot;https://hexdocs.pm/elixir/GenServer.html&quot;&gt;&lt;code&gt;GenServer&lt;/code&gt;&lt;/a&gt;) relies on explicit message passing rather than shared pointer access.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h1&gt;Ecosystem Security: Managing Hex Third-Party Supply Chain Risks&lt;/h1&gt;
&lt;p&gt;While the BEAM protects you from core runtime vulnerabilities, any modern application is still susceptible to supply chain attacks via third-party libraries. PHP handles this via &lt;a href=&quot;https://getcomposer.org&quot;&gt;Composer&lt;/a&gt;; Elixir relies on the &lt;a href=&quot;https://hex.pm&quot;&gt;Hex&lt;/a&gt; package manager. Because the Elixir ecosystem is smaller, proactive supply chain auditing is critical.&lt;/p&gt;
&lt;p&gt;To secure your production pipeline against malicious dependency injections or unpatched packages, incorporate these three automated layers into your Continuous Integration (CI) suite:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;mix deps.unlock --check-unused&lt;/code&gt;:&lt;/strong&gt; Ensures that your &lt;code&gt;mix.lock&lt;/code&gt; file matches your configuration exactly and prevents dangling or untracked packages from sneaking into your builds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;mix hex.audit&lt;/code&gt;:&lt;/strong&gt; Checks your local dependency tree against the Hex registry to find packages that have been retired by their maintainers due to security vulnerabilities or deprecation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://github.com/nccgroup/sobelow&quot;&gt;Sobelow&lt;/a&gt; (Static Analysis):&lt;/strong&gt; Integrate Sobelow into your build process to scan Elixir code specifically for application-level vulnerability paths (like SQL injection or insecure deserialization in Phoenix applications).&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;# Add to your CI workflow&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;deps.unlock&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--check-unused&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;deps.get&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;hex.audit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;sobelow&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--config&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;.sobelow-conf&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;--exit&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Architecture as a Security Shield&lt;/h1&gt;
&lt;p&gt;The October 2026 PHP Security Advisory demonstrates that even when you write flawless application code, you remain exposed to the memory mechanics of your underlying runtime platform. PHP engineers were forced to scramble to apply emergency configurations (such as setting &lt;code&gt;memory_limit=-1&lt;/code&gt; or disabling session upload progress features) as brittle stopgaps while waiting for patches.&lt;/p&gt;
&lt;p&gt;Elixir developers didn't have to change a single line of code or adjust their production topologies during this advisory cycle. By moving away from shared-memory, single-threaded worker abstractions and adopting the BEAM's immutable, process-isolated model, Elixir turns severe runtime engineering vulnerabilities into an architectural impossibility.&lt;/p&gt;
&lt;h1&gt;Architectural &amp;amp; Security Feature Summary&lt;/h1&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Security Risk / Feature&lt;/th&gt;
&lt;th&gt;PHP Runtime Environment (C-Based Engine)&lt;/th&gt;
&lt;th&gt;Elixir / BEAM Runtime Environment&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Primary CVE Focus&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://nvd.nist.gov/vuln/detail/CVE-2026-109618&quot;&gt;CVE-2026-109618&lt;/a&gt; &amp;amp; &lt;a href=&quot;https://nvd.nist.gov/vuln/detail/CVE-2026-109619&quot;&gt;CVE-2026-109619&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;None (architecturally immune to these vectors)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Concurrency Model&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Ephemeral request execution inside persistent, reusable worker processes.&lt;/td&gt;
&lt;td&gt;Spawns unique, highly isolated lightweight processes for every single request.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Memory Isolation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Shared heap inside the worker process; vulnerable to pointer corruption.&lt;/td&gt;
&lt;td&gt;Share-nothing architecture; absolute memory boundaries per process.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Handling Allocation Faults&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Transitions into delicate cleanup paths that risk treating uninitialized state as valid.&lt;/td&gt;
&lt;td&gt;Immediate process termination (&amp;quot;Let it crash&amp;quot;) accompanied by atomic heap reclamation.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Process Control&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Handled globally via pool managers (e.g., &lt;a href=&quot;https://www.php.net/manual/en/install.fpm.configuration.php&quot;&gt;&lt;code&gt;php-fpm.conf&lt;/code&gt;&lt;/a&gt;).&lt;/td&gt;
&lt;td&gt;Granular, per-process heap caps using &lt;a href=&quot;https://hexdocs.pm/elixir/Process.html#flag/2&quot;&gt;&lt;code&gt;Process.flag(:max_heap_size, ...)&lt;/code&gt;&lt;/a&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Supply Chain Guardrails&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Mainstream security coverage via &lt;a href=&quot;https://getcomposer.org/doc/03-cli.md#audit&quot;&gt;Composer Audit&lt;/a&gt;.&lt;/td&gt;
&lt;td&gt;Targeted protection via &lt;code&gt;mix hex.audit&lt;/code&gt; and static application analysis with &lt;a href=&quot;https://github.com/nccgroup/sobelow&quot;&gt;Sobelow&lt;/a&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/development&quot;&gt;#development&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/php&quot;&gt;#php&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/elixir&quot;&gt;#elixir&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/phoenix&quot;&gt;#phoenix&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/fixing-header-too-long-when-downloading-precompiled-nifs-in-elixir</id>
    <title>🐥 Fixing &quot;header_too_long&quot; When Downloading Precompiled NIFs in Elixir</title>
    <updated>2026-10-24T17:00:00Z</updated>
    <published>2026-10-24T17:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/fixing-header-too-long-when-downloading-precompiled-nifs-in-elixir"/>
    <content type="html">&lt;p&gt;If you've recently run &lt;code&gt;mix compile&lt;/code&gt; on an Elixir project that uses precompiled NIFs, such as &lt;a href=&quot;https://github.com/leandrocp/mdex&quot;&gt;mdex&lt;/a&gt; or &lt;a href=&quot;https://github.com/leandrocp/lumis&quot;&gt;lumis&lt;/a&gt;, you may have seen an error like this:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;[info] Attempt 1 failed with {:error, &amp;quot;couldn&amp;#39;t fetch NIF from https://github.com/...: {:error, {:header_too_long, {:max, 10240}}}&amp;quot;}&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;== Compilation error in file lib/mdex_native/native.ex ==&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;** (RuntimeError) Error while downloading precompiled NIF: couldn&amp;#39;t fetch NIF from https://github.com/...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is frustrating because &lt;code&gt;mix deps.get&lt;/code&gt; succeeds. The problem only appears during compilation.&lt;/p&gt;
&lt;h1&gt;What's Going On?&lt;/h1&gt;
&lt;p&gt;Elixir libraries that include native code (Rust, C, etc.) often ship precompiled binaries called NIFs (Native Implemented Functions), so you don't need a native toolchain to use them. The library &lt;a href=&quot;https://github.com/philss/rustler_precompiled&quot;&gt;&lt;code&gt;rustler_precompiled&lt;/code&gt;&lt;/a&gt; handles downloading the right binary for your platform at compile time.&lt;/p&gt;
&lt;p&gt;Under the hood, &lt;code&gt;rustler_precompiled&lt;/code&gt; uses &lt;a href=&quot;https://github.com/elixir-mint/mint&quot;&gt;Mint&lt;/a&gt;, an Elixir HTTP client, to fetch NIF archives from GitHub Releases.&lt;/p&gt;
&lt;p&gt;Here's the problem: GitHub's CDN now returns &lt;strong&gt;very large response headers&lt;/strong&gt; on release asset redirect requests—headers that exceed Mint's default maximum of &lt;strong&gt;10,240 bytes&lt;/strong&gt; (10 KB). When Mint hits that cap, it aborts the request with &lt;code&gt;{:header_too_long, {:max, 10240}}&lt;/code&gt;, and compilation fails.&lt;/p&gt;
&lt;p&gt;This isn't a bug in your code. It's an upstream issue involving GitHub's infrastructure and Mint's conservative defaults.&lt;/p&gt;
&lt;h1&gt;Three Ways to Fix It&lt;/h1&gt;
&lt;h2&gt;Option 1 — Download the NIF Manually with curl (Fastest)&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;curl&lt;/code&gt; doesn't have the same header size restriction. You can download the NIF archive directly and let &lt;code&gt;rustler_precompiled&lt;/code&gt; pick it up from its cache on the next compile.&lt;/p&gt;
&lt;p&gt;First, find the download URLs in the error message in your terminal, then fetch the archive:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;curl&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-L&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;-o&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;/tmp/your_nif.tar.gz&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;https://github.com/.../your_nif.tar.gz&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;rustler_precompiled&lt;/code&gt; caches NIFs in &lt;code&gt;~/.cache/rustler_precompiled/&lt;/code&gt; (Linux/macOS). Copy the downloaded archive there, then rerun &lt;code&gt;mix compile&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Option 2 — Update the Dependency&lt;/h2&gt;
&lt;p&gt;The maintainers of these libraries are aware of the issue. A newer release may have switched away from Mint for the download step or increased Mint's header limit. Try:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;deps.update&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;your_nif_package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;compile&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is the cleanest long-term fix if a patched version is available.&lt;/p&gt;
&lt;h2&gt;Option 3 — Force Build from Source&lt;/h2&gt;
&lt;p&gt;If you have the &lt;a href=&quot;https://rustup.rs/&quot;&gt;Rust toolchain&lt;/a&gt; installed, you can tell &lt;code&gt;rustler_precompiled&lt;/code&gt; to compile the NIF locally instead of downloading it.&lt;/p&gt;
&lt;p&gt;Add this to &lt;code&gt;config/dev.exs&lt;/code&gt; (or &lt;code&gt;config/config.exs&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:rustler_precompiled&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:force_build&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;your_nif_package: &lt;/span&gt;&lt;span class=&quot;l-boolean&quot;&gt;true&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And add Rustler as an optional dependency in &lt;code&gt;mix.exs&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:rustler&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;&amp;gt;= 0.0.0&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;optional: &lt;/span&gt;&lt;span class=&quot;l-boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then run &lt;code&gt;mix deps.get &amp;amp;&amp;amp; mix compile&lt;/code&gt;. Compilation will take longer because it builds native code, but it will work regardless of GitHub's header behavior.&lt;/p&gt;
&lt;h1&gt;Which Should You Choose?&lt;/h1&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;Recommended fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Just need to unblock right now&lt;/td&gt;
&lt;td&gt;Option 1 (manual curl)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A patched release is available&lt;/td&gt;
&lt;td&gt;Option 2 (update deps)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;You control the environment and have Rust&lt;/td&gt;
&lt;td&gt;Option 3 (force build)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CI/CD pipeline is broken&lt;/td&gt;
&lt;td&gt;Option 1 or 3, depending on whether Rust is available&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h1&gt;The Broader Lesson&lt;/h1&gt;
&lt;p&gt;This failure mode—a third-party infrastructure change silently breaking your build—is a good reminder to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pin your NIF dependency versions so you're not surprised by new behavior after an update.&lt;/li&gt;
&lt;li&gt;Cache precompiled NIFs in CI so you're not downloading them on every build.&lt;/li&gt;
&lt;li&gt;Have a fallback strategy (force build) for environments where downloading isn't reliable.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Precompiled NIFs are a major convenience for Elixir developers who don't want to manage native toolchains. But they introduce a network dependency at compile time, and as this issue shows, that dependency can break in unexpected ways.&lt;/p&gt;
&lt;h1&gt;The Proper Fix&lt;/h1&gt;
&lt;p&gt;The poeple behind &lt;a href=&quot;https://github.com/philss/rustler_precompiled&quot;&gt;&lt;code&gt;rustler_precompiled&lt;/code&gt;&lt;/a&gt; are working on a proper fix. You can follow the progress with &lt;a href=&quot;https://github.com/philss/rustler_precompiled/pull/113&quot;&gt;this pull request&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/development&quot;&gt;#development&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/http&quot;&gt;#http&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/elixir&quot;&gt;#elixir&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/terminal&quot;&gt;#terminal&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/streaming-zip-downloads-in-phoenix-with-zstream</id>
    <title>🐥 Streaming ZIP downloads in Phoenix with zstream</title>
    <updated>2026-10-23T17:00:00Z</updated>
    <published>2026-10-23T17:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/streaming-zip-downloads-in-phoenix-with-zstream"/>
    <content type="html">&lt;p&gt;When you need to let users download multiple files as a ZIP in a Phoenix application, creating the archive upfront is often wasteful. It costs disk space, adds latency, and does not scale well for large files.&lt;/p&gt;
&lt;p&gt;A better approach is to &lt;strong&gt;stream the ZIP archive directly to the browser&lt;/strong&gt; while it's being generated. In Elixir, the &lt;code&gt;zstream&lt;/code&gt; library makes this straightforward and production-ready.&lt;/p&gt;
&lt;p&gt;This post shows how to stream a ZIP file from a Phoenix controller without creating temporary files.&lt;/p&gt;
&lt;h1&gt;The idea&lt;/h1&gt;
&lt;p&gt;The solution combines three pieces:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;Plug.Conn.send_chunked/2&lt;/code&gt; to start a streaming HTTP response&lt;/li&gt;
&lt;li&gt;&lt;code&gt;zstream&lt;/code&gt; to generate ZIP data incrementally&lt;/li&gt;
&lt;li&gt;File streams (&lt;code&gt;File.stream!/3&lt;/code&gt;) to avoid loading files into memory&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The browser starts downloading immediately, while the server produces ZIP chunks on the fly.&lt;/p&gt;
&lt;h1&gt;Adding the dependency&lt;/h1&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;deps&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:zstream&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;~&amp;gt; 0.6&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Streaming a ZIP from a controller&lt;/h1&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyAppWeb.DownloadController&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyAppWeb&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:controller&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;images&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_params&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;files&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;      &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;image1.jpg&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;/data/images/image1.jpg&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;      &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;image2.png&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;/data/images/image2.png&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;conn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;      &lt;span class=&quot;l-variable&quot;&gt;conn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;      &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;put_resp_header&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;content-type&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;application/zip&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;      &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;put_resp_header&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;        &lt;span class=&quot;l-string&quot;&gt;&amp;quot;content-disposition&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;        &lt;span class=&quot;l-string&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;~s(&lt;/span&gt;attachment; filename=&amp;quot;images.zip&amp;quot;&lt;span class=&quot;l-string&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;      &lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;      &lt;span class=&quot;l-operator&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;send_chunked&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;zip_stream&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;      &lt;span class=&quot;l-module&quot;&gt;Zstream&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;        &lt;span class=&quot;l-module&quot;&gt;Enum&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;files&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;          &lt;span class=&quot;l-module&quot;&gt;Zstream&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;entry&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;stream!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;64_000&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;23&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;24&quot;&gt;      &lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;25&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;26&quot;&gt;    &lt;span class=&quot;l-module&quot;&gt;Enum&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;reduce_while&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;zip_stream&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;conn&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;27&quot;&gt;      &lt;span class=&quot;l-keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Plug.Conn&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;28&quot;&gt;        &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:cont&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;29&quot;&gt;        &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:error&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:closed&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:halt&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;30&quot;&gt;      &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;31&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;32&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;33&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What's happening here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Each file is represented as a &lt;code&gt;Zstream&lt;/code&gt; entry&lt;/li&gt;
&lt;li&gt;The file contents are streamed from disk&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Zstream.zip/1&lt;/code&gt; produces a stream of ZIP binary chunks&lt;/li&gt;
&lt;li&gt;Each chunk is forwarded to the client using &lt;code&gt;Plug.Conn.chunk/2&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At no point is the full ZIP or any full file loaded into memory.&lt;/p&gt;
&lt;h1&gt;Adding folders to the ZIP&lt;/h1&gt;
&lt;p&gt;ZIP archives don't require explicit directory entries. Just use paths in the entry name:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-module&quot;&gt;Zstream&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;entry&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;images/image1.jpg&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;stream!&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;64_000&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Why this approach works well&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;No temporary ZIP files&lt;/li&gt;
&lt;li&gt;Constant memory usage&lt;/li&gt;
&lt;li&gt;Handles large files gracefully&lt;/li&gt;
&lt;li&gt;The download starts immediately in the browser&lt;/li&gt;
&lt;li&gt;Works with standard reverse proxies like Nginx or Caddy&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Common pitfalls&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;Always call &lt;code&gt;send_chunked/2&lt;/code&gt; before streaming data&lt;/li&gt;
&lt;li&gt;Handle &lt;code&gt;{:error, :closed}&lt;/code&gt; when the client disconnects&lt;/li&gt;
&lt;li&gt;Use reasonably large chunk sizes (32–128 KB)&lt;/li&gt;
&lt;li&gt;Ensure file paths and names are trusted to avoid ZIP path traversal&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Using this from LiveView&lt;/h1&gt;
&lt;p&gt;LiveView cannot stream responses itself. The usual pattern is to trigger a navigation to the controller action:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;push_navigate&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;to: &lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;~p&amp;quot;&lt;/span&gt;/downloads/images&lt;span class=&quot;l-string-special&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The controller then handles the streaming ZIP download as shown above.&lt;/p&gt;
&lt;h1&gt;Closing thoughts&lt;/h1&gt;
&lt;p&gt;If you need to package many files into a ZIP download in Phoenix, &lt;code&gt;zstream&lt;/code&gt; is a simple and robust solution. It fits naturally with Plug's chunked responses and keeps resource usage predictable, even under load.&lt;/p&gt;
&lt;p&gt;For most applications, this is the cleanest way to offer ZIP downloads without touching the filesystem at all.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/pattern&quot;&gt;#pattern&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/http&quot;&gt;#http&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/elixir&quot;&gt;#elixir&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/phoenix&quot;&gt;#phoenix&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/8-chunking-strategies-for-rag-based-agents</id>
    <title>🐥 8 Chunking strategies for RAG-based agents</title>
    <updated>2026-10-20T17:00:00Z</updated>
    <published>2026-10-20T17:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/8-chunking-strategies-for-rag-based-agents"/>
    <content type="html">&lt;p&gt;&lt;img src=&quot;https://www.yellowduck.be/media/88462ca4-b0a1-4441-9537-59dae9d5ef65_2484x3002.webp&quot; alt=&quot;8 Chunking strategies for RAG-based agents&quot; /&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Fixed-Si&lt;/strong&gt;ze: cuts text every fixed number of tokens, with a small overlap.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Recursive&lt;/strong&gt;: splits on paragraphs first, falling back to lines then words and characters only when a piece is still too big.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Document-Based&lt;/strong&gt;: splits wherever the document already marks a break, a heading, a chapter, an HTML tag, so a markdown file becomes one chunk per section on its own.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Semantic&lt;/strong&gt;: embeds each sentence, checks its similarity to the one before it, and starts a new chunk when that similarity drops. This finds real topic shifts instead of guessing by length.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LLM-Based&lt;/strong&gt;: asks an LLM to rewrite the text as standalone propositions, one idea per line, and turns each into a chunk, high quality but expensive across a large corpus.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Agentic&lt;/strong&gt;: an AI agent reads each document, decides what it is (code, contract, table), and picks the chunking method for that file instead of one rule for everything.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Late Chunking&lt;/strong&gt;: runs the full document through an embedding model, then applies normal split boundaries and mean-pools within each boundary to create chunk embeddings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hierarchical&lt;/strong&gt;: breaks the document into layers, sections, paragraphs, then sentences&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Courtesy of &lt;a href=&quot;https://newsletter.bytebytego.com/authors/bytebytego&quot;&gt;the ByteByteGo newsletter&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/ai&quot;&gt;#ai&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/langchain&quot;&gt;#langchain&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/translating-country-codes-to-full-names-in-elixir-using-ex-cldr-countries</id>
    <title>🐥 Translating country codes to full names in Elixir using `ex_cldr_countries`</title>
    <updated>2026-10-18T17:00:00Z</updated>
    <published>2026-10-18T17:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/translating-country-codes-to-full-names-in-elixir-using-ex-cldr-countries"/>
    <content type="html">&lt;p&gt;Unlike JavaScript's &lt;code&gt;Intl.DisplayNames&lt;/code&gt;, Elixir doesn't provide built-in localization for country names. However, with the help of the excellent &lt;a href=&quot;https://hexdocs.pm/ex_cldr_countries/readme.html&quot;&gt;&lt;code&gt;ex_cldr_countries&lt;/code&gt;&lt;/a&gt; package, you can easily achieve dynamic, localized country translations backed by official Unicode CLDR data.&lt;/p&gt;
&lt;h1&gt;Installation&lt;/h1&gt;
&lt;p&gt;Add the package to your &lt;code&gt;mix.exs&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;deps&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ex_cldr_countries&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;~&amp;gt; 2.0&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then fetch it:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-bash&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;l-variable-parameter&quot;&gt;deps.get&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Setting Up a Backend&lt;/h1&gt;
&lt;p&gt;Most examples online show calling &lt;code&gt;Cldr.Countries.from_iso/2&lt;/code&gt; directly, but modern &lt;code&gt;ex_cldr&lt;/code&gt; usage requires a &lt;strong&gt;backend module&lt;/strong&gt;. Without one, you're relying on a default backend that may not be configured — and you lose control over which locales get compiled in.&lt;/p&gt;
&lt;p&gt;Create a backend module in your app:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;# lib/my_app/cldr.ex&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyApp.Cldr&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Cldr&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;locales: &lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;en&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;fr&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;nl&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;default_locale: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;en&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-string-special-symbol&quot;&gt;providers: &lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;Cldr.Countries&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;locales&lt;/code&gt; list determines which translations get compiled into your app. Keep it to what you actually need — CLDR data is large and including everything will bloat your build.&lt;/p&gt;
&lt;h1&gt;Translating Country Codes&lt;/h1&gt;
&lt;p&gt;With the backend in place, translating a country code is straightforward:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-module&quot;&gt;MyApp.Cldr.Countries&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;from_iso&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:BE&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;locale: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;fr&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;# =&amp;gt; {:ok, &amp;quot;Belgique&amp;quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-module&quot;&gt;MyApp.Cldr.Countries&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;from_iso&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:NL&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;locale: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;en&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;# =&amp;gt; {:ok, &amp;quot;Netherlands&amp;quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-module&quot;&gt;MyApp.Cldr.Countries&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;from_iso&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:DE&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;locale: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;nl&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;# =&amp;gt; {:ok, &amp;quot;Duitsland&amp;quot;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;The first argument is the ISO 3166-1 Alpha-2 code as an atom (&lt;code&gt;:BE&lt;/code&gt;, &lt;code&gt;:NL&lt;/code&gt;, &lt;code&gt;:DE&lt;/code&gt;, …)&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;locale&lt;/code&gt; option sets the output language&lt;/li&gt;
&lt;li&gt;If the translation can't be found, it returns an &lt;code&gt;{:error, reason}&lt;/code&gt; tuple, so it's safe to pattern match on&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Why This Approach&lt;/h1&gt;
&lt;p&gt;The backend module gives you three concrete benefits:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Scoped locale compilation&lt;/strong&gt; — only the locales you list get compiled, keeping your build lean&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Explicit and testable&lt;/strong&gt; — the backend is a real module you can reference directly in tests&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No global state surprises&lt;/strong&gt; — the bare &lt;code&gt;Cldr.*&lt;/code&gt; namespace is just a shim that delegates to whichever backend is configured as the default; using your own backend is always more predictable&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For most Phoenix applications, dropping this into a &lt;code&gt;lib/my_app/cldr.ex&lt;/code&gt; file is all the setup you need.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/development&quot;&gt;#development&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/best-practice&quot;&gt;#best-practice&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/elixir&quot;&gt;#elixir&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/handling-local-timezones-in-ash-while-storing-utc-datetimes</id>
    <title>🐥 Handling local timezones in Ash while storing UTC datetimes</title>
    <updated>2026-10-14T17:00:00Z</updated>
    <published>2026-10-14T17:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/handling-local-timezones-in-ash-while-storing-utc-datetimes"/>
    <content type="html">&lt;p&gt;When building Elixir applications with the Ash Framework, it's a best practice to store all datetimes in UTC in your database. However, users typically interact with your application in their &lt;strong&gt;local timezone&lt;/strong&gt;, so you need a reliable way to convert their input into UTC—and show it back in their local time.&lt;/p&gt;
&lt;p&gt;This post shows how to handle that conversion cleanly and reusably in Ash actions using a custom &lt;code&gt;change&lt;/code&gt; module.&lt;/p&gt;
&lt;h1&gt;Why store in UTC?&lt;/h1&gt;
&lt;p&gt;Storing all datetimes in UTC avoids timezone ambiguity and makes it easier to compare, sort, and query times consistently—especially if your users are spread across multiple regions. PostgreSQL and Ash support UTC datetime types (&lt;code&gt;:utc_datetime&lt;/code&gt;), and your application should convert values at the boundaries: when receiving input or displaying output.&lt;/p&gt;
&lt;h1&gt;Define UTC datetime attributes&lt;/h1&gt;
&lt;p&gt;First, define the attributes on your Ash resource using &lt;code&gt;:utc_datetime&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;attributes&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;attribute&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:starts_at&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:utc_datetime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;attribute&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:ends_at&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:utc_datetime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Add user-local input arguments&lt;/h1&gt;
&lt;p&gt;In your Ash action, accept user-local datetime input as strings, &lt;code&gt;NaiveDateTime&lt;/code&gt;, or &lt;code&gt;DateTime&lt;/code&gt; structs:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;actions&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-function-call&quot;&gt;create&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:schedule&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-function-call&quot;&gt;argument&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:starts_at_local&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-function-call&quot;&gt;argument&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:ends_at_local&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:naive_datetime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-function-call&quot;&gt;change&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;MyApp.Changes.ConvertLocalDatetimeToUtc&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;arg_key: :starts_at_local&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;field: :starts_at&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-function-call&quot;&gt;change&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;MyApp.Changes.ConvertLocalDatetimeToUtc&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;arg_key: :ends_at_local&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;field: :ends_at&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Reusable change module&lt;/h1&gt;
&lt;p&gt;Ash's &lt;code&gt;change&lt;/code&gt; DSL expects a module implementing &lt;code&gt;Ash.Resource.Change&lt;/code&gt;. Here's a reusable implementation you can drop into your project:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword-function&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;MyApp.Changes.ConvertLocalDatetimeToUtc&lt;/span&gt; &lt;span class=&quot;l-keyword-function&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;@moduledoc&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;  Converts a local datetime argument (string, NaiveDateTime, or DateTime)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;  to UTC and assigns it to an Ash attribute. Gracefully handles &lt;span class=&quot;l-markup-raw&quot;&gt;`nil`&lt;/span&gt; values.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;  Options:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;    - &lt;span class=&quot;l-markup-raw&quot;&gt;`:arg_key`&lt;/span&gt; — the action argument holding the local datetime value&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;    - &lt;span class=&quot;l-markup-raw&quot;&gt;`:field`&lt;/span&gt; — the resource attribute to assign the UTC value to&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;    - &lt;span class=&quot;l-markup-raw&quot;&gt;`:timezone_key`&lt;/span&gt; — the context key for the user&amp;#39;s timezone (default: &lt;span class=&quot;l-markup-raw&quot;&gt;`:user_timezone`&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;span class=&quot;l-operator&quot;&gt;&lt;span class=&quot;l-comment-documentation&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;  &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Ash.Resource.Change&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;opts&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;opts&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Keyword&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;put_new&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;opts&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:timezone_key&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:user_timezone&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;opts&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;change&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;changeset&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;opts&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_context&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;arg_key&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;opts&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:arg_key&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;field&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;opts&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:field&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;timezone_key&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;opts&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:timezone_key&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;23&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;timezone&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Ash.Changeset&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;get_context&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;changeset&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;timezone_key&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;UTC&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;24&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;25&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Ash.Changeset&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;fetch_argument&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;changeset&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;arg_key&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;26&quot;&gt;      &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;27&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;changeset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;28&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;29&quot;&gt;      &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;30&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;do_convert_to_utc&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;timezone&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;31&quot;&gt;          &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;utc_dt&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;32&quot;&gt;            &lt;span class=&quot;l-module&quot;&gt;Ash.Changeset&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;force_change_attribute&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;changeset&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;field&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;utc_dt&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;33&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;34&quot;&gt;          &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;35&quot;&gt;            &lt;span class=&quot;l-module&quot;&gt;Ash.Changeset&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;add_error&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;36&quot;&gt;              &lt;span class=&quot;l-variable&quot;&gt;changeset&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;37&quot;&gt;              &lt;span class=&quot;l-string&quot;&gt;&amp;quot;&lt;span class=&quot;l-string-special&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;arg_key&lt;/span&gt;&lt;span class=&quot;l-string-special&quot;&gt;}&lt;/span&gt; is invalid or could not be converted to UTC&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;38&quot;&gt;            &lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;39&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;40&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;41&quot;&gt;      &lt;span class=&quot;l-string-special-symbol&quot;&gt;:error&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;42&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;changeset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;43&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;44&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;45&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;46&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;do_convert_to_utc&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-constant-builtin&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;47&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;48&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;do_convert_to_utc&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_timezone&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;49&quot;&gt;    &lt;span class=&quot;l-module&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;shift_zone&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;Etc/UTC&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;50&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;51&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;52&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;do_convert_to_utc&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;NaiveDateTime&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;naive&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;timezone&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;53&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;dt_local&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;from_naive&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;naive&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;timezone&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;54&quot;&gt;         &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;dt_utc&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;shift_zone&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;dt_local&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;Etc/UTC&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;55&quot;&gt;      &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;dt_utc&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;56&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;57&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;58&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;59&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;do_convert_to_utc&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;timezone&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;l-function-call&quot;&gt;is_binary&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;60&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;naive&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;NaiveDateTime&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;from_iso8601&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;61&quot;&gt;         &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;dt_local&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;from_naive&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;naive&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;timezone&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;62&quot;&gt;         &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;dt_utc&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;shift_zone&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;dt_local&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;Etc/UTC&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;63&quot;&gt;      &lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:ok&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;dt_utc&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;64&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;65&quot;&gt;  &lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;66&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;67&quot;&gt;  &lt;span class=&quot;l-keyword-function&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;do_convert_to_utc&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-comment&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;do: &lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;:error&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:invalid&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;68&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;DateTime.shift_zone/2&lt;/code&gt; requires a timezone database. Add &lt;code&gt;{:tzdata, &amp;quot;~&amp;gt; 1.1&amp;quot;}&lt;/code&gt; to your &lt;code&gt;mix.exs&lt;/code&gt; dependencies and configure it:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function-call&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:elixir&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:time_zone_database&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-module&quot;&gt;Tzdata.TimeZoneDatabase&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;h1&gt;Setting the timezone in context&lt;/h1&gt;
&lt;p&gt;To apply the conversion correctly, the user's timezone should be passed in the Ash changeset context.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ash 3.x (domain-based):&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-module&quot;&gt;AshPhoenix.Form&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;for_action&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;MyApp.Event&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:schedule&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-string-special-symbol&quot;&gt;domain: &lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;MyApp.Domain&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-string-special-symbol&quot;&gt;as: :event&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-string-special-symbol&quot;&gt;context: &lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;user_timezone: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Europe/Brussels&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Ash 2.x (api-based):&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-elixir&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-module&quot;&gt;AshPhoenix.Form&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-function-call&quot;&gt;for_action&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;MyApp.Event&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-string-special-symbol&quot;&gt;:schedule&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  &lt;span class=&quot;l-string-special-symbol&quot;&gt;api: &lt;/span&gt;&lt;span class=&quot;l-module&quot;&gt;MyApp.Api&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;  &lt;span class=&quot;l-string-special-symbol&quot;&gt;as: :event&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  &lt;span class=&quot;l-string-special-symbol&quot;&gt;context: &lt;/span&gt;&lt;span class=&quot;l-punctuation-special&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;l-string-special-symbol&quot;&gt;user_timezone: &lt;/span&gt;&lt;span class=&quot;l-string&quot;&gt;&amp;quot;Europe/Brussels&amp;quot;&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Summary&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;Store all datetime attributes in UTC.&lt;/li&gt;
&lt;li&gt;Accept user-local input via custom &lt;code&gt;argument&lt;/code&gt;s.&lt;/li&gt;
&lt;li&gt;Use a reusable &lt;code&gt;Ash.Resource.Change&lt;/code&gt; module to convert values to UTC.&lt;/li&gt;
&lt;li&gt;Pass the user's timezone via &lt;code&gt;context&lt;/code&gt; on the changeset.&lt;/li&gt;
&lt;li&gt;Handle strings, &lt;code&gt;NaiveDateTime&lt;/code&gt;, &lt;code&gt;DateTime&lt;/code&gt;, and &lt;code&gt;nil&lt;/code&gt; values gracefully.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This pattern keeps your Ash resources clean and your time handling robust across different timezones.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/database&quot;&gt;#database&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/elixir&quot;&gt;#elixir&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/phoenix&quot;&gt;#phoenix&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/postgresql&quot;&gt;#postgresql&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/sql&quot;&gt;#sql&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/building-a-lift-scheduling-algorithm-in-python</id>
    <title>🐥 Building a lift scheduling algorithm in Python</title>
    <updated>2026-10-11T17:00:00Z</updated>
    <published>2026-10-11T17:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/building-a-lift-scheduling-algorithm-in-python"/>
    <content type="html">&lt;p&gt;A lift looks simple from the outside: press a button, wait, and eventually it arrives. Internally, however, a lift controller is a scheduling problem.&lt;/p&gt;
&lt;p&gt;With multiple lifts and multiple passengers, the controller needs to decide which lift should handle which request, while trying to minimise waiting time, travel time, and unnecessary stops.&lt;/p&gt;
&lt;p&gt;Let's build a simplified version in Python.&lt;/p&gt;
&lt;h1&gt;Start with a single lift&lt;/h1&gt;
&lt;p&gt;A lift needs to keep track of its current state:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-python&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;dataclasses&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;dataclass&lt;/span&gt;, &lt;span class=&quot;l-variable&quot;&gt;field&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-function&quot;&gt;@&lt;span class=&quot;l-function&quot;&gt;dataclass&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;l-constructor&quot;&gt;Lift&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;direction&lt;/span&gt;: &lt;span class=&quot;l-variable&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;targets&lt;/span&gt;: &lt;span class=&quot;l-variable&quot;&gt;set&lt;/span&gt;[&lt;span class=&quot;l-type&quot;&gt;int&lt;/span&gt;] &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;field&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;default_factory&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;l-variable&quot;&gt;set&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A simple controller can use the &lt;strong&gt;LOOK scheduling algorithm&lt;/strong&gt; to select the next floor.&lt;/p&gt;
&lt;p&gt;LOOK works like this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Continue in the current direction while there are targets in that direction.&lt;/li&gt;
&lt;li&gt;Visit the closest target in that direction.&lt;/li&gt;
&lt;li&gt;When there are no more targets in that direction, reverse.&lt;/li&gt;
&lt;li&gt;If there are no targets at all, become idle.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-python&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;next_target&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;Lift&lt;/span&gt;) &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;None&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;targets&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;None&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; (&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;            &lt;span class=&quot;l-string&quot;&gt;&amp;quot;up&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-function-builtin&quot;&gt;min&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;targets&lt;/span&gt;) &lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;floor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;down&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;        )&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;up&amp;quot;&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;above&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; [&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;            &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;targets&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;floor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;        ]&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;above&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-function-builtin&quot;&gt;min&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;above&lt;/span&gt;)&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;23&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;down&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;24&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;25&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;down&amp;quot;&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;26&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;below&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; [&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;27&quot;&gt;            &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;28&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;targets&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;29&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;floor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;30&quot;&gt;        ]&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;31&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;32&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;below&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;33&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-function-builtin&quot;&gt;max&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;below&lt;/span&gt;)&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;34&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;35&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;up&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;36&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;37&quot;&gt;    &lt;span class=&quot;l-comment&quot;&gt;# If we get here, there are targets but none in the&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;38&quot;&gt;    &lt;span class=&quot;l-comment&quot;&gt;# current direction. Pick the closest one and establish&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;39&quot;&gt;    &lt;span class=&quot;l-comment&quot;&gt;# the corresponding direction.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;40&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-function-builtin&quot;&gt;min&lt;/span&gt;(&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;41&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;targets&lt;/span&gt;,&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;42&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;l-keyword&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt;: &lt;span class=&quot;l-function-builtin&quot;&gt;abs&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;floor&lt;/span&gt;),&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;43&quot;&gt;    )&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;44&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;45&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; (&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;46&quot;&gt;        &lt;span class=&quot;l-string&quot;&gt;&amp;quot;up&amp;quot;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;down&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;47&quot;&gt;    )&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;48&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;49&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;target&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;Lift:       5&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;Requests:   2, 4, 6, 8, 10&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;Direction:  up&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The lift visits:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;5 → 6 → 8 → 10 → 8 → 6 → 4 → 2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, that example exposes an important implementation detail: &lt;strong&gt;once a target has been served, it must be removed from the target set&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;A simple simulation might therefore look like this:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-python&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;serve_next&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;Lift&lt;/span&gt;) &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;None&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;next_target&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;)&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;None&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;target&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;targets&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;remove&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;target&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now the route is correctly:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;5 → 6 → 8 → 10 → 4 → 2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The lift continues upwards until there are no targets above it, then reverses direction.&lt;/p&gt;
&lt;h2&gt;A simpler implementation&lt;/h2&gt;
&lt;p&gt;We can make the LOOK behaviour more explicit by separating target selection from state changes:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-python&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;next_target&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;Lift&lt;/span&gt;) &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;None&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;targets&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;up&amp;quot;&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;above&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-function-builtin&quot;&gt;sorted&lt;/span&gt;(&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;            &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;targets&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;floor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;        )&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;above&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;above&lt;/span&gt;[&lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;]&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;down&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;down&amp;quot;&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;below&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-function-builtin&quot;&gt;sorted&lt;/span&gt;(&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;            &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;targets&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;floor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;        )&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;below&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;23&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;below&lt;/span&gt;[&lt;span class=&quot;l-operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;l-number&quot;&gt;1&lt;/span&gt;]&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;24&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;25&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;up&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;26&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;27&quot;&gt;    &lt;span class=&quot;l-comment&quot;&gt;# The lift was idle, or has just changed direction.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;28&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;up&amp;quot;&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;29&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;above&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; [&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;30&quot;&gt;            &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;targets&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;31&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;floor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;32&quot;&gt;        ]&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;33&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;above&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;34&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-function-builtin&quot;&gt;min&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;above&lt;/span&gt;)&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;35&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;36&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;down&amp;quot;&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;37&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;below&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; [&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;38&quot;&gt;            &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;targets&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;39&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;floor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;40&quot;&gt;        ]&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;41&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;below&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;42&quot;&gt;            &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-function-builtin&quot;&gt;max&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;below&lt;/span&gt;)&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;43&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;44&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;None&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The important distinction from a naive implementation is that LOOK &lt;strong&gt;does not choose the globally closest floor&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Given:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;Current floor: 5&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;Targets:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;10&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and direction &lt;code&gt;up&lt;/code&gt;, the next target is:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;6&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;not:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;4&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Even though both are equally close, the lift is already travelling upwards.&lt;/p&gt;
&lt;p&gt;After serving 6 and 10, it reverses:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;5 → 6 → 10 → 4&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That's the defining behaviour of LOOK.&lt;/p&gt;
&lt;h2&gt;Hall requests are different&lt;/h2&gt;
&lt;p&gt;There are actually two kinds of requests.&lt;/p&gt;
&lt;p&gt;A passenger inside the lift says:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;I want to go to floor 10.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A passenger outside says:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;I&amp;#39;m on floor 5 and want to go up.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The second request also has a direction.&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-python&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-function&quot;&gt;@&lt;span class=&quot;l-function&quot;&gt;dataclass&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;l-constructor&quot;&gt;HallRequest&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;direction&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;str&lt;/span&gt;  &lt;span class=&quot;l-comment&quot;&gt;# &amp;quot;up&amp;quot; or &amp;quot;down&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This matters because a lift travelling upwards shouldn't necessarily stop for someone waiting to travel downwards.&lt;/p&gt;
&lt;p&gt;For example, if the lift is travelling:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;2 → 5 → 8 → 10&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;a request on floor 4 for a downward journey doesn't necessarily belong on that route.&lt;/p&gt;
&lt;p&gt;The controller should generally prioritise requests matching its current direction.&lt;/p&gt;
&lt;h1&gt;Multiple lifts&lt;/h1&gt;
&lt;p&gt;Now the problem gets more interesting.&lt;/p&gt;
&lt;p&gt;Imagine three lifts:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;Lift A: floor 2, going up&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;Lift B: floor 8, idle&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;Lift C: floor 15, going down&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Someone on floor 6 presses the up button.&lt;/p&gt;
&lt;p&gt;The obvious solution is to choose the closest lift. But that's not necessarily optimal.&lt;/p&gt;
&lt;p&gt;Instead, we can give every lift a score.&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-python&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;score&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;Lift&lt;/span&gt;, &lt;span class=&quot;l-variable&quot;&gt;request&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;HallRequest&lt;/span&gt;) &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;float&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;distance&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-function-builtin&quot;&gt;abs&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;request&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;floor&lt;/span&gt;)&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;request&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;direction_penalty&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;None&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;direction_penalty&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;else&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;direction_penalty&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;distance&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;direction_penalty&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can then select the lowest-scoring lift:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-python&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;assign_lift&lt;/span&gt;(&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;lifts&lt;/span&gt;: &lt;span class=&quot;l-variable&quot;&gt;list&lt;/span&gt;[&lt;span class=&quot;l-type&quot;&gt;Lift&lt;/span&gt;],&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;request&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;HallRequest&lt;/span&gt;,&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;) &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Lift&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-function-builtin&quot;&gt;min&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;lifts&lt;/span&gt;, &lt;span class=&quot;l-variable&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;l-keyword&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;: &lt;span class=&quot;l-function&quot;&gt;score&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;, &lt;span class=&quot;l-variable&quot;&gt;request&lt;/span&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is already considerably better than simply selecting the nearest lift.&lt;/p&gt;
&lt;h1&gt;Simulating the cost&lt;/h1&gt;
&lt;p&gt;A more sophisticated controller doesn't just ask:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;How far away is this lift?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It asks:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;How much additional work will this request cause?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Consider a lift currently at floor 3 with these destinations:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;7, 10, 15&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A new passenger wants to go from floor 5 to floor 12.&lt;/p&gt;
&lt;p&gt;The existing route might be:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;3 → 7 → 10 → 15&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Adding the passenger changes it to:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;3 → 5 → 7 → 10 → 12 → 15&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The difference between those routes is the &lt;strong&gt;cost of accepting the request&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;We can model this with a cost function:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-python&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;request_cost&lt;/span&gt;(&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;waiting_time&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;float&lt;/span&gt;,&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;travel_time&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;float&lt;/span&gt;,&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;additional_stops&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;int&lt;/span&gt;,&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-variable&quot;&gt;direction_changes&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;int&lt;/span&gt;,&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;) &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;float&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; (&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;waiting_time&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;        &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;travel_time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;        &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;additional_stops&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;        &lt;span class=&quot;l-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;direction_changes&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;    )&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The weights are deliberately configurable.&lt;/p&gt;
&lt;p&gt;For example, we might consider waiting five times more important than travel time because making somebody wait for a lift is generally worse than adding a few seconds to the journey of someone already inside.&lt;/p&gt;
&lt;h1&gt;Destination dispatch&lt;/h1&gt;
&lt;p&gt;Modern lift systems can go one step further.&lt;/p&gt;
&lt;p&gt;Instead of pressing:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;↑&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and later selecting a destination inside the lift, the passenger enters the destination immediately:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;Floor 5 → Floor 17&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The controller therefore knows the complete journey before assigning a lift.&lt;/p&gt;
&lt;p&gt;That allows it to group passengers with similar destinations.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;3 → 17&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;4 → 18&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;5 → 16&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;6 → 17&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;could potentially be handled by the same lift.&lt;/p&gt;
&lt;p&gt;The dispatcher can therefore optimise the entire route instead of treating every passenger as an independent request.&lt;/p&gt;
&lt;h2&gt;Separating the dispatcher from the lift&lt;/h2&gt;
&lt;p&gt;A useful architecture is to separate two responsibilities.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;dispatcher&lt;/strong&gt; decides:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;Which lift should handle this request?&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;strong&gt;lift controller&lt;/strong&gt; decides:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;Which floor should I visit next?&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In Python, that could look like:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-python&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;l-constructor&quot;&gt;Dispatcher&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;__init__&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;self&lt;/span&gt;, &lt;span class=&quot;l-variable&quot;&gt;lifts&lt;/span&gt;: &lt;span class=&quot;l-variable&quot;&gt;list&lt;/span&gt;[&lt;span class=&quot;l-type&quot;&gt;Lift&lt;/span&gt;]):&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;lifts&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lifts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;assign&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;self&lt;/span&gt;, &lt;span class=&quot;l-variable&quot;&gt;request&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;HallRequest&lt;/span&gt;) &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;Lift&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-function-builtin&quot;&gt;min&lt;/span&gt;(&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;            &lt;span class=&quot;l-variable&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;lifts&lt;/span&gt;,&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;            &lt;span class=&quot;l-variable&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;l-keyword&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;: &lt;span class=&quot;l-function&quot;&gt;score&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;, &lt;span class=&quot;l-variable&quot;&gt;request&lt;/span&gt;),&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;        )&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The lift itself can remain responsible for its own state:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-python&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;l-constructor&quot;&gt;LiftController&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;__init__&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;self&lt;/span&gt;, &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;Lift&lt;/span&gt;):&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;lift&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;lift&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;add_target&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;self&lt;/span&gt;, &lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt;: &lt;span class=&quot;l-type&quot;&gt;int&lt;/span&gt;) &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;None&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;        &lt;span class=&quot;l-variable&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;lift&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;targets&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;add&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;floor&lt;/span&gt;)&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;next_floor&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;self&lt;/span&gt;) &lt;span class=&quot;l-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;l-constant-builtin&quot;&gt;None&lt;/span&gt;:&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;l-function&quot;&gt;next_target&lt;/span&gt;(&lt;span class=&quot;l-variable&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;l-property&quot;&gt;lift&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This separation makes the system much easier to reason about and test.&lt;/p&gt;
&lt;h1&gt;Model it as a state machine&lt;/h1&gt;
&lt;p&gt;A real controller would also have explicit states:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-python&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;l-constructor&quot;&gt;Enum&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;l-constructor&quot;&gt;LiftState&lt;/span&gt;(&lt;span class=&quot;l-constructor&quot;&gt;Enum&lt;/span&gt;):&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-constant&quot;&gt;IDLE&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;idle&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-constant&quot;&gt;MOVING_UP&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;moving_up&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-constant&quot;&gt;MOVING_DOWN&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;moving_down&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;    &lt;span class=&quot;l-constant&quot;&gt;DOOR_OPENING&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;door_opening&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;    &lt;span class=&quot;l-constant&quot;&gt;DOOR_OPEN&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;door_open&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;    &lt;span class=&quot;l-constant&quot;&gt;DOOR_CLOSING&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;quot;door_closing&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Events then cause state transitions:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;IDLE&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;  ↓ request above&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;MOVING_UP&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;  ↓ target reached&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;DOOR_OPENING&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;  ↓&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;DOOR_OPEN&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;  ↓&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;DOOR_CLOSING&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;  ↓&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;MOVING_UP&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This becomes particularly useful once you need to handle things such as emergency stops, overloaded lifts, door obstruction, and new requests arriving while the lift is moving.&lt;/p&gt;
&lt;h2&gt;The interesting part is the optimisation&lt;/h2&gt;
&lt;p&gt;The basic mechanics of a lift are relatively straightforward.&lt;/p&gt;
&lt;p&gt;The difficult part is deciding what &lt;strong&gt;optimal&lt;/strong&gt; means.&lt;/p&gt;
&lt;p&gt;A real controller might try to minimise:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-plaintext&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;total waiting time&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;+ total travel time&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;+ number of stops&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;+ direction changes&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;+ energy consumption&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;+ passenger congestion&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And these objectives can conflict.&lt;/p&gt;
&lt;p&gt;Optimising one passenger's journey may make everybody else's journey worse.&lt;/p&gt;
&lt;p&gt;That's why lift scheduling is a useful example of a broader class of software problems: &lt;strong&gt;resource allocation under changing constraints&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The same pattern appears in job schedulers, CPU scheduling, delivery routing, database query planning, and many other systems.&lt;/p&gt;
&lt;p&gt;You don't necessarily need to find the mathematically perfect solution. A good practical approach is often:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Model the current state.&lt;/li&gt;
&lt;li&gt;Generate possible decisions.&lt;/li&gt;
&lt;li&gt;Estimate the cost of each decision.&lt;/li&gt;
&lt;li&gt;Pick the lowest-cost option.&lt;/li&gt;
&lt;li&gt;Recalculate when the state changes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For a lift, that's enough to go from a simple &amp;quot;nearest lift wins&amp;quot; implementation to a surprisingly sophisticated scheduling system.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/development&quot;&gt;#development&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/pattern&quot;&gt;#pattern&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/python&quot;&gt;#python&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>https://www.yellowduck.be/posts/cross-schema-queries-in-postgresql-without-the-loop</id>
    <title>🐥 Cross-schema queries in PostgreSQL without the loop</title>
    <updated>2026-10-08T17:00:00Z</updated>
    <published>2026-10-08T17:00:00Z</published>
    <author><name>Pieter Claerhout</name><email>pieter@yellowduck.be</email></author>
    <link rel="alternate" href="https://www.yellowduck.be/posts/cross-schema-queries-in-postgresql-without-the-loop"/>
    <content type="html">&lt;p&gt;Schema-based multi-tenancy is a popular pattern in PostgreSQL SaaS applications. Each tenant gets its own schema — &lt;code&gt;tenant_1&lt;/code&gt;, &lt;code&gt;tenant_2&lt;/code&gt;, and so on — with an identical table structure but fully isolated data. The application layer queries the right schema at runtime, and the database enforces the separation cleanly.&lt;/p&gt;
&lt;p&gt;The headache arrives when you need to query &lt;em&gt;across&lt;/em&gt; tenants: a one-off data audit, a cross-tenant report, a support investigation. The table exists in every schema, but there is no built-in &lt;code&gt;SELECT * FROM ALL SCHEMAS&lt;/code&gt; syntax. You have to build that query yourself.&lt;/p&gt;
&lt;h1&gt;The procedural approach&lt;/h1&gt;
&lt;p&gt;The first instinct is usually a loop. Iterate over every tenant schema, query the table in each one, and collect the results somewhere. In PL/pgSQL:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-sql&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-comment&quot;&gt;-- Step 1: Create a staging table using the first tenant&amp;#39;s column structure&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;DO $$&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;DECLARE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;    sch &lt;span class=&quot;l-type-builtin&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    FOR sch &lt;span class=&quot;l-keyword-operator&quot;&gt;IN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;nspname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;          &lt;span class=&quot;l-keyword&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;pg_namespace&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;         &lt;span class=&quot;l-keyword&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;nspname&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;LIKE&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;tenant\_%&amp;#39;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;ESCAPE&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;\&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;         &lt;span class=&quot;l-keyword&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;nspname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;         &lt;span class=&quot;l-keyword&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;l-number&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;    LOOP&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;EXECUTE&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;            &amp;#39;CREATE TEMP TABLE all_records AS&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;             SELECT %L::text AS tenant, * FROM %I.orders WHERE false&amp;#39;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;            sch&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; sch&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;        &lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;END&lt;/span&gt; LOOP&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;    &lt;span class=&quot;l-comment&quot;&gt;-- Step 2: Populate from every tenant schema&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;    FOR sch &lt;span class=&quot;l-keyword-operator&quot;&gt;IN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;22&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;nspname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;23&quot;&gt;          &lt;span class=&quot;l-keyword&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;pg_namespace&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;24&quot;&gt;         &lt;span class=&quot;l-keyword&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;nspname&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;LIKE&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;tenant\_%&amp;#39;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;ESCAPE&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;\&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;25&quot;&gt;         &lt;span class=&quot;l-keyword&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;nspname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;26&quot;&gt;    LOOP&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;27&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;EXECUTE&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;28&quot;&gt;            &lt;span class=&quot;l-string&quot;&gt;&amp;#39;INSERT INTO all_records&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;29&quot;&gt;&lt;span class=&quot;l-string&quot;&gt;             SELECT %L::text AS tenant, * FROM %I.orders&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;30&quot;&gt;            &lt;span class=&quot;l-variable-member&quot;&gt;sch&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;sch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;31&quot;&gt;        &lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;32&quot;&gt;    END LOOP&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;33&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;END&lt;/span&gt; $$&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;34&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;35&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;all_records&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;tenant&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;36&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;DROP&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;TABLE&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;all_records&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works. The first loop borrows a schema's column structure to create the temp table; the second loop populates it. But there is an awkward quality to it: two loops, two passes over &lt;code&gt;pg_namespace&lt;/code&gt;, and a &lt;code&gt;DO&lt;/code&gt; block that can't return rows directly, so a temp table is required as a staging area.&lt;/p&gt;
&lt;h1&gt;The set-based approach&lt;/h1&gt;
&lt;p&gt;PostgreSQL's &lt;code&gt;string_agg&lt;/code&gt; function lets you collapse the entire loop into a single expression. Instead of visiting schemas one at a time, you assemble the full &lt;code&gt;UNION ALL&lt;/code&gt; query string in one pass, then execute it once:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-sql&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;DO $$&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;DECLARE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;    sql &lt;span class=&quot;l-type-builtin&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;string_agg&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;        &lt;span class=&quot;l-type&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;            &lt;span class=&quot;l-string&quot;&gt;&amp;#39;SELECT %L::text AS tenant, * FROM %I.orders&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;            &lt;span class=&quot;l-variable-member&quot;&gt;nspname&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;nspname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;        &lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;        &lt;span class=&quot;l-string&quot;&gt;&amp;#39; UNION ALL &amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;        &lt;span class=&quot;l-keyword&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;nspname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;12&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;13&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;INTO&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;sql&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;14&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;pg_namespace&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;15&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;nspname&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;LIKE&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;tenant\_%&amp;#39;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;ESCAPE&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;\&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;16&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;17&quot;&gt;    EXECUTE &amp;#39;CREATE TEMP TABLE all_records AS &amp;#39; || sql&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;18&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;END&lt;/span&gt; $$&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;19&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;20&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;all_records&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;tenant&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;21&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;DROP&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;TABLE&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;all_records&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;No loop. &lt;code&gt;string_agg&lt;/code&gt; scans &lt;code&gt;pg_namespace&lt;/code&gt; once and produces a string like:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-sql&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;tenant_1&amp;#39;&lt;/span&gt;::&lt;span class=&quot;l-type-builtin&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;tenant&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;FROM&lt;/span&gt; tenant_1&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;&lt;span class=&quot;l-keyword-operator&quot;&gt;UNION&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;ALL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;tenant_2&amp;#39;&lt;/span&gt;::&lt;span class=&quot;l-type-builtin&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;tenant&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;FROM&lt;/span&gt; tenant_2&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;&lt;span class=&quot;l-keyword-operator&quot;&gt;UNION&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;ALL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;tenant_3&amp;#39;&lt;/span&gt;::&lt;span class=&quot;l-type-builtin&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;l-variable&quot;&gt;tenant&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;FROM&lt;/span&gt; tenant_3&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;l-type&quot;&gt;orders&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A single &lt;code&gt;EXECUTE&lt;/code&gt; then runs that string wrapped in a &lt;code&gt;CREATE TEMP TABLE AS&lt;/code&gt;, which also infers the column structure automatically — no need for a separate setup step.&lt;/p&gt;
&lt;h1&gt;Breaking It Down&lt;/h1&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Element&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pg_namespace&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;PostgreSQL's system catalog table for schemas. Filter by &lt;code&gt;nspname&lt;/code&gt; to target tenant schemas.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;format('%I', …)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Safely quotes an identifier (schema name, table name). Prevents SQL injection in dynamic queries.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;format('%L', …)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Safely quotes a string literal. Used here to emit the tenant name as a constant in each sub-query.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;string_agg(…, ' UNION ALL ')&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Joins all sub-queries into one SQL string. The &lt;code&gt;ORDER BY&lt;/code&gt; clause ensures a deterministic join order.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CREATE TEMP TABLE AS …&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Creates a temporary table whose columns are inferred from the query result. Scoped to the session and dropped automatically when the connection closes.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;On format() and injection safety&lt;/strong&gt; — Always use &lt;code&gt;%I&lt;/code&gt; for identifiers and &lt;code&gt;%L&lt;/code&gt; for literals when building dynamic SQL with &lt;code&gt;format()&lt;/code&gt;. They handle quoting and escaping correctly, even for edge cases like schema names containing reserved words or special characters.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;When to reach for this&lt;/h1&gt;
&lt;p&gt;This pattern is well suited to ad-hoc, operational queries:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Cross-tenant reporting and aggregations&lt;/li&gt;
&lt;li&gt;Data consistency audits across all schemas&lt;/li&gt;
&lt;li&gt;Support investigations that span multiple tenants&lt;/li&gt;
&lt;li&gt;One-off backfill or data migration checks&lt;/li&gt;
&lt;li&gt;Debugging anomalies that may appear in only some tenants&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For production cross-tenant queries that run on a schedule, a dedicated reporting schema or a materialized view strategy is usually a better fit. Dynamic SQL assembled at query time adds latency and bypasses the planner's ability to optimize across tenants as a whole.&lt;/p&gt;
&lt;h2&gt;The underlying idea&lt;/h2&gt;
&lt;p&gt;The loop version is imperative: it tells the database &lt;em&gt;how&lt;/em&gt; to visit each schema. The &lt;code&gt;string_agg&lt;/code&gt; version is declarative: it describes the result it wants and lets a single &lt;code&gt;EXECUTE&lt;/code&gt; handle execution. That shift is modest here, but it reflects a broader principle in SQL — set-based operations tend to be shorter, easier to read, and easier to extend than their procedural equivalents.&lt;/p&gt;
&lt;p&gt;Adding a filter is illustrative. In the loop version you modify the format string inside the loop body and track whether you've touched it everywhere. In the &lt;code&gt;string_agg&lt;/code&gt; version you change the format string in one place:&lt;/p&gt;
&lt;pre class=&quot;lumis&quot;&gt;&lt;code class=&quot;language-sql&quot; translate=&quot;no&quot; tabindex=&quot;0&quot;&gt;&lt;span class=&quot;l-line&quot; data-line=&quot;1&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;string_agg&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;2&quot;&gt;    &lt;span class=&quot;l-type&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;3&quot;&gt;        &lt;span class=&quot;l-string&quot;&gt;&amp;#39;SELECT %L::text AS tenant, * FROM %I.orders WHERE status = &amp;#39;&amp;#39;pending&amp;#39;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;4&quot;&gt;        &lt;span class=&quot;l-variable-member&quot;&gt;nspname&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;nspname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;5&quot;&gt;    &lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;6&quot;&gt;    &lt;span class=&quot;l-string&quot;&gt;&amp;#39; UNION ALL &amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;7&quot;&gt;    &lt;span class=&quot;l-keyword&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;l-keyword-operator&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;nspname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;8&quot;&gt;&lt;span class=&quot;l-punctuation-bracket&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;9&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;INTO&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;sql&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;10&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;l-type&quot;&gt;pg_namespace&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;l-line&quot; data-line=&quot;11&quot;&gt;&lt;span class=&quot;l-keyword&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;l-variable-member&quot;&gt;nspname&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;LIKE&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;tenant\_%&amp;#39;&lt;/span&gt; &lt;span class=&quot;l-keyword&quot;&gt;ESCAPE&lt;/span&gt; &lt;span class=&quot;l-string&quot;&gt;&amp;#39;\&amp;#39;&lt;/span&gt;&lt;span class=&quot;l-punctuation-delimiter&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One change, one place. The loop version requires the same change inside the loop body, with the same effect — but the set-based version makes the scope of the change obvious at a glance.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.yellowduck.be/tags/database&quot;&gt;#database&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/postgresql&quot;&gt;#postgresql&lt;/a&gt; &lt;a href=&quot;https://www.yellowduck.be/tags/sql&quot;&gt;#sql&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
</feed>
