1

I'm using the crate toml = "0.5.8" to parse TOML files. My expected output is supposed to be stored in this struct:

#[derive(Deserialize, Debug)]
pub struct Config {
    pub watching: Vec<Stocks>,
}

#[derive(Deserialize, Debug)]
pub struct Stocks {
    pub symbol: String,
}

Here is the function where I parse the TOML file:

pub fn parse_toml_file(path: String) -> Config {
    let content = fs::read_to_string(path).expect("Failed to access file.");

    let toml_config: Config = toml::from_str(&content[..]).expect("Failed to parse file.");

    toml_config
}

Here is where I call it:

let home: String = format!("{:?}", home_dir().unwrap());
let path: String = format!(
    "{}/.config/stonks.toml",
    home[1..home.len() - 1].to_string()
);
let cli = Cli::new(parse_toml_file(path));

Here is the content variable's value:

"[watching]\nsymbol = [\"AAPL\", \"TSLA\"]\n"

Here is the error I'm getting at creating the toml_config variable:

thread 'main' panicked at 'Failed to parse file.: Error { inner: ErrorInner { kind: Custom, line: Some(0), col: 0, at: Some(0), message: "invalid type: map, expected a sequence", key: ["watching"] } }', src/parse.rs:19:60
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

How do I fix this error? The content is printing out as expected.

Henry
  • 1,311
  • 1
  • 9
  • 26
  • 2
    your structs seem to imply there is a vector of stocks (called "watching"), each of which contains a symbol. In the toml however, "symbol" is the actual vector... – Jussi Kukkonen Feb 03 '21 at 13:47

0 Answers0