summaryrefslogtreecommitdiffstats
path: root/src/parsing/clang/config.rs
blob: cb058763ee0e1883cca627112fda8eda62d270f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use anyhow::Result;
use serde_derive::{Deserialize, Serialize};
use structopt::StructOpt;

use std::path::PathBuf;

#[derive(Debug, Clone, StructOpt, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) struct Config {
    pub(crate) compile_commands_location: PathBuf,
    pub(crate) extra_args: Vec<String>,
}

#[derive(Debug, Clone, Default, StructOpt, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) struct ProvidedConfig {
    #[structopt(long = "clang-compile-commands-location")]
    pub(crate) compile_commands_location: Option<PathBuf>,
    #[structopt(long = "clang-extra-args", number_of_values = 1)]
    #[serde(default)]
    pub(crate) extra_args: Vec<String>,
}

impl Default for Config {
    fn default() -> Self {
        Config::from_merge(
            ProvidedConfig::default(),
            ProvidedConfig::default(),
        )
        // Currently errors out only on CLI parse fail with clang's extra args
        .unwrap()
    }
}

impl Config {
    pub(crate) fn from_merge(
        cli: ProvidedConfig,
        mut config: ProvidedConfig,
    ) -> Result<Self> {
        let mut extra_args = Vec::new();
        for args in cli.extra_args {
            extra_args.append(&mut ::shell_words::split(&args)?);
        }
        extra_args.append(&mut config.extra_args);
        Ok(Self {
            compile_commands_location: cli
                .compile_commands_location
                .or(config.compile_commands_location)
                .unwrap_or_else(|| PathBuf::from(r".")),
            extra_args,
        })
    }
}