From e773caea8010b87726ea524d31798fb2e43e12f4 Mon Sep 17 00:00:00 2001 From: Minijackson Date: Sat, 21 Dec 2019 12:13:21 +0100 Subject: newtype in types, more generator config, parsing -> parser --- src/parser/clang/config.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/parser/clang/config.rs (limited to 'src/parser/clang/config.rs') diff --git a/src/parser/clang/config.rs b/src/parser/clang/config.rs new file mode 100644 index 0000000..05fe3ab --- /dev/null +++ b/src/parser/clang/config.rs @@ -0,0 +1,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(super) compile_commands_location: PathBuf, + pub(super) extra_args: Vec, +} + +#[derive(Debug, Clone, Default, StructOpt, Deserialize, Serialize)] +#[serde(rename_all = "kebab-case")] +pub(crate) struct ProvidedConfig { + #[structopt(long = "clang-compile-commands-location")] + pub(super) compile_commands_location: Option, + #[structopt(long = "clang-extra-args", number_of_values = 1)] + #[serde(default)] + pub(super) extra_args: Vec, +} + +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 { + 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, + }) + } +} -- cgit v1.2.3