DEV Community

Harishankar Narayanan
Harishankar Narayanan

Posted on

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!

Top comments (0)