Wombat - Syntax Highlighter A Study for Using Rust from Crystal

Recently, I read an introduction to a Shard called Tartrazine for syntax highlighting, and I thought it was exactly what I needed. But I realized that writing all the logic in Crystal could be a lot of work to maintain. To make things easier, I decided, with the help of ChatGPT, to create a C wrapper for the Rust tool bat and link it to Crystal. I set up two repositories to try out this idea, and it worked.

  • bat-c - C API for bat
  • Wombat - Crystal API for bat’s pretty_print
require "wombat"

puts Wombat.pretty_string(%{puts "hello world"})

However, I found that adding and maintaining a C wrapper for a Rust library takes a lot of time. If you think of the Rust library as a big department store, the C wrapper should be kept small, like a vending machine with just a few buttons. Press one, and you get the most important items from the store.

Even so, I believe that if I want to keep using Crystal in the long run, it’s important to be able to take the necessary functions from Rust, create C-compatible libraries, and link them to Crystal.
I tried creating a C wrapper for Syntect as well, and while I had some success, I found it quite challenging with my current skills.

2 Likes

Just in case, the actual highlighting logic in tartrazine is about 250 lines of code :-)

1 Like

The pull request to add the print_with_writer function to bat was merged today.
Now, you can get highlighted strings without patching bat.

use bat::PrettyPrinter;

fn main() {
    let mut output_str = String::new();

        PrettyPrinter::new()
        .input_from_bytes(b"puts \"hello world\"\n")
        .language("crystal")
        .print_with_writer(Some(&mut output_str))
        .unwrap();

    println!("{}", output_str);
}

I no longer need to manage a fork of bat.
This should make it much easier to maintain bat-c and the wombat.