summaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: 44634791c93fbf5d70712271df1a508d360483fa (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
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
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: String,
    #[structopt(long, default_value = ".")]
    pub(crate) compile_commands_location: PathBuf,
    #[structopt(skip = vec![])]
    pub(crate) extra_clang_args: Vec<String>,
}

impl Default for Config {
    fn default() -> Self {
        Config {
            name: "My Project".into(),
            compile_commands_location: PathBuf::from(r"."),
            extra_clang_args: Vec::new(),
        }
    }
}

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,
        }
    }
}