The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Load A JSON Config File In Rust With serde

TODO: Pull subtitle into page object
Code
//! ```cargo
//! [dependencies]
//! serde = { version = "1.0.193", features = ["derive"] }
//! serde_json = "1.0.108"
//! ```

#![allow(dead_code)]
use serde::Deserialize;
use serde_json;
// use std::fs;
// use std::path::PathBuf;

#[derive(Deserialize, Debug)]
pub struct Config {
    alfa: String,
    bravo: String,
}

fn main() {
    let config_file_stub = r#"{ "alfa": "a", "bravo": "b" }"#;
    match serde_json::from_str::<Config>(config_file_stub) {
        Ok(data) => {dbg!(data); ()},
        Err(e) => {dbg!(e); ()}
    };


//     if let Some(config) = load_config(PathBuf::from("config.json")) {
  //       dbg!(config);
    // } else {
      //  println!("Could not load config");
   // }

}

//pub fn load_config(path: PathBuf) -> Option<Config> {
 //    if let Ok(data) = fs::read_to_string(path) {
   //      if let Ok(config) = serde_json::from_str::<Config>(data.as_str()) {
     //       Some(config)
      //  } else {
       //     None
//         }
  //  } else {
   //      None
    // }
// }
Results
[_active_nvim_run:22] data = Config {
    alfa: "a",
    bravo: "b",
}

Notes

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Load A JSON Config File In Rust With serde

This is how I'm loading config options in from
a JSON file in Rust:

-- code
-- rust

//! ```cargo
//! [dependencies]
//! serde = { version = "1.0.193", features = ["derive"] }
//! serde_json = "1.0.108"
//! ```

#![allow(dead_code)]
use serde::Deserialize;
use serde_json;
// use std::fs;
// use std::path::PathBuf;

#[derive(Deserialize, Debug)]
pub struct Config {
    alfa: String,
    bravo: String,
}

fn main() {
    let config_file_stub = r#"{ "alfa": "a", "bravo": "b" }"#;
    match serde_json::from_str::<Config>(config_file_stub) {
        Ok(data) => {dbg!(data); ()},
        Err(e) => {dbg!(e); ()}
    };


//     if let Some(config) = load_config(PathBuf::from("config.json")) {
  //       dbg!(config);
    // } else {
      //  println!("Could not load config");
   // }

}

//pub fn load_config(path: PathBuf) -> Option<Config> {
 //    if let Ok(data) = fs::read_to_string(path) {
   //      if let Ok(config) = serde_json::from_str::<Config>(data.as_str()) {
     //       Some(config)
      //  } else {
       //     None
//         }
  //  } else {
   //      None
    // }
// }

-- results/

[_active_nvim_run:22] data = Config {
    alfa: "a",
    bravo: "b",
}

-- /results


-- notes

- This requires the crates:

cargo add serde --features "derive"
cargo add serde_json

- This approach requires defining the strut the
JSON gets loaded into in advance. I think there
are ways to do it without that. I prefer 
making the structure explicit.

- The value that comes back from `load_config()`` 
is an option. The code in `main()`` checks to 
see if it loaded and works from there.


-- categories
-- Rust 
-- serde 

-- metadata
-- date: 2023-09-05 14:43:19
-- id: 2uzpo211
-- site: aws
-- type: post
-- status: published