Logo

dev-resources.site

for different kinds of informations.

Building JSONLogic in Rust with Github Copilot: A 5-Hour Journey

Published at
11/20/2024
Categories
rust
json
ai
logic
Author
codetiger
Categories
4 categories in total
rust
open
json
open
ai
open
logic
open
Author
9 person written this
codetiger
open
Building JSONLogic in Rust with Github Copilot: A 5-Hour Journey

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.

datalogic-rs

What is JSONLogic?

JSONLogic is a way to write portable logic rules as JSON. It's particularly useful when you need to:

  • Share business logic between frontend and backend
  • Store rules in a database
  • Create dynamic validation rules
  • Build expression evaluators

The Project Goals

  • Implement the complete JSONLogic spec in Rust
  • Ensure type safety and zero-copy parsing
  • Maintain production-grade code quality
  • Use AI effectively without compromising code quality

My Approach with Github Copilot

1. Clear Architecture First

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.

2. Test-Driven Development

JSONLogic comes with an extensive test suite. I:

  • Imported all test cases
  • Set up the test infrastructure
  • Let all tests fail initially
#[test]
fn test_var_operator() {
    let cases = load_test_cases("var");
    for case in cases {
        assert_eq!(
            evaluate(case.logic, case.data),
            case.expected
        );
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Pattern-Based Implementation

Started with implementing one operator as a template.

4. Leveraging Copilot

Once I had the pattern established:

  1. Wrote test cases for each operator
  2. Explained the operator's requirements to Copilot
  3. Let Copilot generate the initial implementation
  4. Reviewed and refined the code
  5. Moved to the next operator

5. Optimization & Polish

Final steps included:

  • Running Clippy and fixing all warnings
  • Adding comprehensive error messages
  • Optimizing performance (reducing allocations)
  • Adding documentation
  • Setting up CI/CD

Key Learnings

What Worked Well

  1. Clear Direction: Copilot performs best when given clear patterns to follow
  2. Test-Driven Approach: Having tests upfront made validation seamless
  3. Incremental Development: Building operator by operator kept the scope manageable
  4. Review & Refine: AI-generated code needs human oversight for best practices

Challenges Faced

  1. Edge Cases: Some JSON parsing edge cases needed manual handling
  2. Error Messages: Crafting user-friendly error messages needed human touch
  3. Documentation: Generated docs needed refinement for clarity

The Result

The final library (GitHub Link) is:

  • Production-ready
  • Fully tested
  • Well-documented
  • Performance optimized
  • Type-safe

Try It Out!

Add to your Cargo.toml:

[dependencies]
datalogic = "0.1.0"
Enter fullscreen mode Exit fullscreen mode

Basic usage:

use datalogic::evaluate;
use serde_json::json;

fn main() {
    let logic = json!({"==": [1, 1]});
    let data = json!({});
    let result = evaluate(&logic, &data).unwrap();
    assert_eq!(result, json!(true));
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

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.

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

What's Next?

  • Adding more optimizations
  • Supporting custom operators
  • Building a web playground
  • Your suggestions? 😊

datalogic-rs
Remember to ⭐️ the repository if you found it useful!

logic Article's
30 articles in total
Favicon
I am currently reducing at least 22 proofs by at least 99312 steps total.
Favicon
Building JSONLogic in Rust with Github Copilot: A 5-Hour Journey
Favicon
Tackling fundamental logic: A very hard automated deduction challenge (free for all)
Favicon
Halloween, (CSS) Masks, and Logic Gates
Favicon
The Blueprint of Logic
Favicon
Part II: Foundations of Logical Thinking in Programming, Logical Connectors
Favicon
The 3 Ls of Quantum Physics Law Logic and Love
Favicon
Foundations of Logical Thinking in Programming
Favicon
The 3 Ls of Quantum Physics Law Logic and Love
Favicon
5 Tips to improve your logic building in Programming.
Favicon
Higher Order Reasoning / Hierarchical Planning
Favicon
Sort arrays of zero's, one's and two's
Favicon
Implementing a Bingo Guessing Game: Finding the Most Efficient Way
Favicon
Prazer, Yuri.
Favicon
Is Coding For You? Four Simple Questions.
Favicon
Mastering logical thinking for programming
Favicon
Java Pattern Problem
Favicon
Leetcode Solutions: Count Asterisks
Favicon
Leetcode Solutions: Check If a Word Occurs As a Prefix of Any Word in a Sentence
Favicon
Open Source Adventures: Episode 64: How to write Z3 Solvers for Puzzle Games?
Favicon
Learning Pilog - 4: Recursion
Favicon
PROGRAMMING - A SEED OF CREATION
Favicon
JavaScript and Logic.
Favicon
The Most Unusual Programming Language
Favicon
Set theory(with PHP)
Favicon
My condition
Favicon
Understanding programming concepts for non-programmers
Favicon
100 Languages Speedrun: Episode 42: Prolog
Favicon
PAGINATION in pieces
Favicon
The hardest thing I ever did explained as simply as possible.

Featured ones: