JSON Config Parser

cdphu

CDPhu

Posted on October 6, 2022

JSON Config Parser

This week I decided to work on Eakam's rost_gen repository again because of the difficulty it gave me last time. This time I thought I would be prepared for it, and boy howdy, was that not the case. This time in fact I had much more difficulty because of the numerous changes they have made since the last cooperative lab.

NEW FEATURE
A user could run the SSG by doing either of the following:

# Option 1: use command line arguments:
ssg --input ./site --output ./build --stylesheet https://cdn.jsdelivr.net/npm/water.css@2/out/water.css --lang fr

# Option 2: use a config file
ssg --config ./ssg-config.json
Enter fullscreen mode Exit fullscreen mode

The config file option means we can have a much shorter command, and instead store our options in a file.

Writing & Testing code in RUST

After I forked the project, it took a while to write the code due to the new addition of the clap crate that was being used in Eakam's repository. So it was in a technical basis trying to relearn how options were selected and parsed. After wrapping my head around the command parsing I created a struct and function to handle config files.

use std::io::BufReader;

extern crate serde_with;
extern crate serde_json;
extern crate serde;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
#[serde_with::skip_serializing_none]
struct Config{
    input: Option<String>,
    output: Option<String>,
    lang: Option<String>
}
Enter fullscreen mode Exit fullscreen mode

I used serde in order to parse the JSON into the created struct and then it would call the struct and parse the details into the handle_conversion() function which was already made.

But this was before I was made aware that Option<String> could not be parsed as a normal string. So I had to find a way to convert it, which I did but it took a while.

Using Git

It was an interesting experience trying to figure out how to remotely use git. Though it was a lot more simple than I anticipated. I now know how to test pull requests code without clicking my mouse.

Final Thoughts

It was not easy to work with an unfamiliar language yet, working in open source helps you a lot in terms of learning new rules. I have learnt, the syntax is different in every language. The technique and ideas to build are the same.

💖 💪 🙅 🚩
cdphu
CDPhu

Posted on October 6, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

JSON Config Parser
rust JSON Config Parser

October 6, 2022