<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Harishankar Narayanan</title>
    <description>The latest articles on DEV Community by Harishankar Narayanan (@codetiger).</description>
    <link>https://dev.to/codetiger</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2458138%2Ff7ecf819-e945-4c89-942c-30a15ae54fba.jpeg</url>
      <title>DEV Community: Harishankar Narayanan</title>
      <link>https://dev.to/codetiger</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codetiger"/>
    <language>en</language>
    <item>
      <title>Building JSONLogic in Rust with Github Copilot: A 5-Hour Journey</title>
      <dc:creator>Harishankar Narayanan</dc:creator>
      <pubDate>Wed, 20 Nov 2024 07:01:33 +0000</pubDate>
      <link>https://dev.to/codetiger/building-jsonlogic-in-rust-with-github-copilot-a-5-hour-journey-cp1</link>
      <guid>https://dev.to/codetiger/building-jsonlogic-in-rust-with-github-copilot-a-5-hour-journey-cp1</guid>
      <description>&lt;p&gt;Hey devs! 👋 I recently took on an interesting challenge: implementing the complete JSONLogic specification in Rust, with Github Copilot as my pair programming buddy. The entire project took less than 5 hours, and I wanted to share my experience of how AI can be effectively leveraged in systems programming.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Open-Payments/datalogic-rs" rel="noopener noreferrer"&gt;datalogic-rs&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is JSONLogic?
&lt;/h2&gt;

&lt;p&gt;JSONLogic is a way to write portable logic rules as JSON. It's particularly useful when you need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Share business logic between frontend and backend&lt;/li&gt;
&lt;li&gt;Store rules in a database&lt;/li&gt;
&lt;li&gt;Create dynamic validation rules&lt;/li&gt;
&lt;li&gt;Build expression evaluators&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Project Goals
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Implement the complete JSONLogic spec in Rust&lt;/li&gt;
&lt;li&gt;Ensure type safety and zero-copy parsing&lt;/li&gt;
&lt;li&gt;Maintain production-grade code quality&lt;/li&gt;
&lt;li&gt;Use AI effectively without compromising code quality&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  My Approach with Github Copilot
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Clear Architecture First
&lt;/h3&gt;

&lt;p&gt;Instead of jumping straight into coding, I started by designing the project structure. I explained this structure to Copilot, which helped set a consistent pattern for implementation.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Test-Driven Development
&lt;/h3&gt;

&lt;p&gt;JSONLogic comes with an extensive test suite. I:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Imported all test cases&lt;/li&gt;
&lt;li&gt;Set up the test infrastructure&lt;/li&gt;
&lt;li&gt;Let all tests fail initially
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[test]&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;test_var_operator&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;cases&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;load_test_cases&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"var"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;case&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;cases&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nf"&gt;evaluate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;case&lt;/span&gt;&lt;span class="py"&gt;.logic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;case&lt;/span&gt;&lt;span class="py"&gt;.data&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="n"&gt;case&lt;/span&gt;&lt;span class="py"&gt;.expected&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Pattern-Based Implementation
&lt;/h3&gt;

&lt;p&gt;Started with implementing one operator as a template.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Leveraging Copilot
&lt;/h3&gt;

&lt;p&gt;Once I had the pattern established:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Wrote test cases for each operator&lt;/li&gt;
&lt;li&gt;Explained the operator's requirements to Copilot&lt;/li&gt;
&lt;li&gt;Let Copilot generate the initial implementation&lt;/li&gt;
&lt;li&gt;Reviewed and refined the code&lt;/li&gt;
&lt;li&gt;Moved to the next operator&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  5. Optimization &amp;amp; Polish
&lt;/h3&gt;

&lt;p&gt;Final steps included:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Running Clippy and fixing all warnings&lt;/li&gt;
&lt;li&gt;Adding comprehensive error messages&lt;/li&gt;
&lt;li&gt;Optimizing performance (reducing allocations)&lt;/li&gt;
&lt;li&gt;Adding documentation&lt;/li&gt;
&lt;li&gt;Setting up CI/CD&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key Learnings
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Worked Well
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Clear Direction&lt;/strong&gt;: Copilot performs best when given clear patterns to follow&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test-Driven Approach&lt;/strong&gt;: Having tests upfront made validation seamless&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Incremental Development&lt;/strong&gt;: Building operator by operator kept the scope manageable&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Review &amp;amp; Refine&lt;/strong&gt;: AI-generated code needs human oversight for best practices&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Challenges Faced
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Edge Cases&lt;/strong&gt;: Some JSON parsing edge cases needed manual handling&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Messages&lt;/strong&gt;: Crafting user-friendly error messages needed human touch&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Documentation&lt;/strong&gt;: Generated docs needed refinement for clarity&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The Result
&lt;/h2&gt;

&lt;p&gt;The final library (&lt;a href="https://github.com/Open-Payments/datalogic-rs" rel="noopener noreferrer"&gt;GitHub Link&lt;/a&gt;) is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Production-ready&lt;/li&gt;
&lt;li&gt;Fully tested&lt;/li&gt;
&lt;li&gt;Well-documented&lt;/li&gt;
&lt;li&gt;Performance optimized&lt;/li&gt;
&lt;li&gt;Type-safe&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try It Out!
&lt;/h2&gt;

&lt;p&gt;Add to your &lt;code&gt;Cargo.toml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[dependencies]&lt;/span&gt;
&lt;span class="py"&gt;datalogic&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.1.0"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basic usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;datalogic&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;evaluate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;serde_json&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;logic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;json!&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="s"&gt;"=="&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]});&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;json!&lt;/span&gt;&lt;span class="p"&gt;({});&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;evaluate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;logic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;json!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This project showed me that AI can be an effective pair programming partner when used with proper structure and oversight. It's not about replacing human developers but augmenting our capabilities to build faster while maintaining quality.&lt;/p&gt;

&lt;p&gt;Would love to hear your thoughts and experiences with AI-assisted development! Feel free to check out the repository, raise issues, or contribute.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's Next?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Adding more optimizations&lt;/li&gt;
&lt;li&gt;Supporting custom operators&lt;/li&gt;
&lt;li&gt;Building a web playground&lt;/li&gt;
&lt;li&gt;Your suggestions? 😊&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;a href="https://github.com/Open-Payments/datalogic-rs" rel="noopener noreferrer"&gt;datalogic-rs&lt;/a&gt;&lt;br&gt;
Remember to ⭐️ the repository if you found it useful!&lt;/p&gt;

</description>
      <category>rust</category>
      <category>json</category>
      <category>ai</category>
      <category>logic</category>
    </item>
  </channel>
</rss>
