summaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs89
1 files changed, 39 insertions, 50 deletions
diff --git a/src/config.rs b/src/config.rs
index b448469..72a9ff6 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,4 +1,8 @@
1use crate::cli::Cli; 1use crate::cli::Cli;
2use crate::generator::config::{
3 Config as GeneratorConfig, ProvidedConfig as ProvidedGeneratorConfig,
4};
5use crate::parsing::clang::config::{Config as ClangConfig, ProvidedConfig as ProvidedClangConfig};
2 6
3use anyhow::{anyhow, Context, Result}; 7use anyhow::{anyhow, Context, Result};
4use codemap::CodeMap; 8use codemap::CodeMap;
@@ -12,71 +16,56 @@ use std::sync::Arc;
12 16
13pub(super) const DEFAULT_PROJECT_CONFIGURATION_FILE_NAME: &str = "poseidoc.toml"; 17pub(super) const DEFAULT_PROJECT_CONFIGURATION_FILE_NAME: &str = "poseidoc.toml";
14 18
15#[derive(Debug, Clone, StructOpt, Deserialize, Serialize)] 19#[derive(Debug, Clone, Serialize)]
16#[structopt(rename_all = "kebab-case")]
17#[serde(rename_all = "kebab-case", default)]
18pub(crate) struct Config { 20pub(crate) struct Config {
19 #[structopt(skip)] 21 pub(crate) name: String,
20 pub(crate) name: Option<String>, 22 pub(crate) clang: ClangConfig,
21 #[structopt(long)] 23 pub(crate) generator: GeneratorConfig,
22 pub(crate) compile_commands_location: Option<PathBuf>,
23 #[structopt(skip = vec![])]
24 pub(crate) extra_clang_args: Vec<String>,
25 #[structopt(flatten)]
26 pub(crate) class: ClassConfig,
27}
28
29impl Config {
30 pub(crate) fn merge_cli(self, cli: Cli) -> Self {
31 Config {
32 name: cli.common_options.name.or(self.name),
33 compile_commands_location: cli
34 .common_options
35 .compile_commands_location
36 .or(self.compile_commands_location),
37 extra_clang_args: cli
38 .extra_arg
39 .into_iter()
40 .flatten()
41 .chain(self.extra_clang_args)
42 .collect(),
43 class: self.class.merge_cli(cli.common_options.class),
44 }
45 }
46} 24}
47 25
48impl Default for Config { 26impl Default for Config {
49 fn default() -> Self { 27 fn default() -> Self {
50 Config { 28 Config {
51 name: Some(String::from("My Project")), 29 name: String::from("My Project"),
52 compile_commands_location: Some(PathBuf::from(r".")), 30 clang: ClangConfig::default(),
53 extra_clang_args: Vec::new(), 31 generator: GeneratorConfig::default(),
54 class: ClassConfig::default(),
55 } 32 }
56 } 33 }
57} 34}
58 35
59#[derive(Debug, Clone, StructOpt, Deserialize, Serialize)] 36impl Config {
60#[structopt(rename_all = "kebab-case")] 37 pub(crate) fn from_merge(cli: Cli, conf: ProvidedConfig) -> Result<Self> {
61#[serde(rename_all = "kebab-case", default)] 38 Ok(Config {
62pub(crate) struct ClassConfig { 39 name: cli
63} 40 .common_options
64 41 .name
65impl ClassConfig { 42 .or(conf.name)
66 fn merge_cli(self, _cli: ClassConfig) -> Self { 43 .unwrap_or_else(|| String::from("My Project")),
67 ClassConfig { 44 clang: ClangConfig::from_merge(cli.common_options.clang, conf.clang)?,
68 } 45 generator: GeneratorConfig::from_merge(cli.common_options.generator, conf.generator),
46 })
69 } 47 }
70} 48}
71 49
72impl Default for ClassConfig { 50/// Same as ProvidedConfig but with options for when not user-provided
73 fn default() -> Self { 51#[derive(Debug, Clone, Default, StructOpt, Deserialize)]
74 ClassConfig { 52#[structopt(rename_all = "kebab-case")]
75 } 53#[serde(rename_all = "kebab-case")]
76 } 54pub(crate) struct ProvidedConfig {
55 #[structopt(skip)]
56 pub(crate) name: Option<String>,
57 #[structopt(flatten)]
58 #[serde(default)]
59 pub(crate) clang: ProvidedClangConfig,
60 #[structopt(skip)]
61 #[serde(default)]
62 pub(crate) generator: ProvidedGeneratorConfig,
77} 63}
78 64
79pub(super) fn load_config(location: impl AsRef<Path>, codemap: &mut CodeMap) -> Result<Config> { 65pub(super) fn load_config(
66 location: impl AsRef<Path>,
67 codemap: &mut CodeMap,
68) -> Result<ProvidedConfig> {
80 let location = location.as_ref(); 69 let location = location.as_ref();
81 70
82 let final_path = if location.is_dir() { 71 let final_path = if location.is_dir() {