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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
use crate::cli::Cli;
use anyhow::{anyhow, Context, Result};
use codemap::CodeMap;
use codemap_diagnostic::Diagnostic;
use serde_derive::{Deserialize, Serialize};
use structopt::StructOpt;
use thiserror::Error;
use std::path::{Path, PathBuf};
use std::sync::Arc;
pub(super) const DEFAULT_PROJECT_CONFIGURATION_FILE_NAME: &str = "poseidoc.toml";
#[derive(Debug, Clone, StructOpt, Deserialize, Serialize)]
#[structopt(rename_all = "kebab-case")]
#[serde(rename_all = "kebab-case", default)]
pub(crate) struct Config {
#[structopt(skip)]
pub(crate) name: Option<String>,
#[structopt(long)]
pub(crate) compile_commands_location: Option<PathBuf>,
#[structopt(skip = vec![])]
pub(crate) extra_clang_args: Vec<String>,
#[structopt(flatten)]
pub(crate) class: ClassConfig,
}
impl Config {
pub(crate) fn merge_cli(self, cli: Cli) -> Self {
Config {
name: cli.common_options.name.or(self.name),
compile_commands_location: cli
.common_options
.compile_commands_location
.or(self.compile_commands_location),
extra_clang_args: cli
.extra_arg
.into_iter()
.flatten()
.chain(self.extra_clang_args)
.collect(),
class: self.class.merge_cli(cli.common_options.class),
}
}
}
impl Default for Config {
fn default() -> Self {
Config {
name: Some(String::from("My Project")),
compile_commands_location: Some(PathBuf::from(r".")),
extra_clang_args: Vec::new(),
class: ClassConfig::default(),
}
}
}
#[derive(Debug, Clone, StructOpt, Deserialize, Serialize)]
#[structopt(rename_all = "kebab-case")]
#[serde(rename_all = "kebab-case", default)]
pub(crate) struct ClassConfig {
}
impl ClassConfig {
fn merge_cli(self, _cli: ClassConfig) -> Self {
ClassConfig {
}
}
}
impl Default for ClassConfig {
fn default() -> Self {
ClassConfig {
}
}
}
pub(super) fn load_config(location: impl AsRef<Path>, codemap: &mut CodeMap) -> Result<Config> {
let location = location.as_ref();
let final_path = if location.is_dir() {
location.join(DEFAULT_PROJECT_CONFIGURATION_FILE_NAME)
} else if !location.exists() {
return Err(anyhow!("File {:?} does not exists", location))
.with_context(|| format!("Failed to open project configuration: {:?}", location));
} else {
location.to_owned()
};
let config_str =
std::fs::read_to_string(&final_path).map_err(|err| ConfigLoadError::IoError {
file_name: final_path.clone(),
source: err,
})?;
Ok(
toml::from_str(&config_str).map_err(|err| ConfigLoadError::TomlDeserialzation {
file: codemap.add_file(final_path.to_string_lossy().into(), config_str),
source: err,
})?,
)
}
#[derive(Debug, Error)]
#[error("Error loading project configuration")]
pub(crate) enum ConfigLoadError {
#[error("Failed to read project configuration: {:?}", file_name)]
IoError {
file_name: PathBuf,
source: std::io::Error,
},
#[error("Failed to parse project configuration: {:?}", file.name())]
TomlDeserialzation {
file: Arc<codemap::File>,
source: toml::de::Error,
},
}
impl From<&ConfigLoadError> for Diagnostic {
fn from(err: &ConfigLoadError) -> Diagnostic {
use codemap_diagnostic::{Level, SpanLabel, SpanStyle};
use std::convert::TryInto;
let message = err.to_string();
let spans = match err {
ConfigLoadError::IoError { .. } => vec![],
ConfigLoadError::TomlDeserialzation { file, source, .. } => {
if let Some((line, col)) = source.line_col() {
let line_span = file.line_span(line);
let col = col.try_into().unwrap();
let span = line_span.subspan(col, col);
vec![SpanLabel {
span,
label: None,
style: SpanStyle::Primary,
}]
} else {
vec![]
}
}
};
Diagnostic {
level: Level::Error,
message,
code: None,
spans,
}
}
}
|