diff options
author | Minijackson <minijackson@riseup.net> | 2019-12-18 20:56:53 +0100 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2019-12-18 20:56:53 +0100 |
commit | de896baff7e97fac4dde79078c9a2fa1c652576b (patch) | |
tree | 512b67b91d64e51d63f7ac5ff925a5c96d9aaf3c /src/config.rs | |
parent | 860b73f1644ecd6548ae403ec483625fb7b625ea (diff) | |
download | poseidoc-de896baff7e97fac4dde79078c9a2fa1c652576b.tar.gz poseidoc-de896baff7e97fac4dde79078c9a2fa1c652576b.zip |
Big refactoring
- entities should be more coherent when parsing multiple files
- well defined, language agnostic entity tree
- each module has its own configuration
- less dead code
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 89 |
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 @@ | |||
1 | use crate::cli::Cli; | 1 | use crate::cli::Cli; |
2 | use crate::generator::config::{ | ||
3 | Config as GeneratorConfig, ProvidedConfig as ProvidedGeneratorConfig, | ||
4 | }; | ||
5 | use crate::parsing::clang::config::{Config as ClangConfig, ProvidedConfig as ProvidedClangConfig}; | ||
2 | 6 | ||
3 | use anyhow::{anyhow, Context, Result}; | 7 | use anyhow::{anyhow, Context, Result}; |
4 | use codemap::CodeMap; | 8 | use codemap::CodeMap; |
@@ -12,71 +16,56 @@ use std::sync::Arc; | |||
12 | 16 | ||
13 | pub(super) const DEFAULT_PROJECT_CONFIGURATION_FILE_NAME: &str = "poseidoc.toml"; | 17 | pub(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)] | ||
18 | pub(crate) struct Config { | 20 | pub(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 | |||
29 | impl 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 | ||
48 | impl Default for Config { | 26 | impl 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)] | 36 | impl 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 { |
62 | pub(crate) struct ClassConfig { | 39 | name: cli |
63 | } | 40 | .common_options |
64 | 41 | .name | |
65 | impl 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 | ||
72 | impl 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 | } | 54 | pub(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 | ||
79 | pub(super) fn load_config(location: impl AsRef<Path>, codemap: &mut CodeMap) -> Result<Config> { | 65 | pub(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() { |