summaryrefslogtreecommitdiff
path: root/src/repo/profile/make_defaults/mod.rs
blob: 334f165026ed3a06deec1286f926fa6551fd5c0b (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
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
use std::{
    collections::{HashMap, HashSet},
    fs, io,
    path::{Path, PathBuf},
};

use mon::{Parser, ParserIter, ascii_whitespace1, input::InputIter};

use crate::{
    Parseable,
    repo::profile::{LineBasedFileExpr, Profile},
};

mod parsers;

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("{0}: io error: {1}")]
    Io(PathBuf, io::Error),
    #[error("parser error: {0}")]
    Parser(String),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Key(String);

#[derive(Debug, Clone)]
struct Literal(String);

#[derive(Debug, Clone)]
struct Interpolation(String);

#[derive(Debug, Clone)]
enum Segment {
    Literal(Literal),
    Interpolation(Interpolation),
}

#[derive(Debug, Clone)]
struct Assignment(Key, Vec<Segment>);

pub(super) fn evaluate<P: AsRef<Path>>(
    parents: &[Profile],
    path: P,
) -> Result<HashMap<String, String>, Error> {
    let parsed = match fs::read_to_string(path.as_ref().join("make.defaults")) {
        Ok(contents) => parse(&contents)?,
        Err(e) if matches!(e.kind(), io::ErrorKind::NotFound) => HashMap::new(),
        Err(e) => return Err(Error::Io(path.as_ref().to_path_buf(), e)),
    };

    let mut vars = interpolate(parents, parsed);

    incremental(parents, &mut vars);

    Ok(vars)
}

fn incremental(parents: &[Profile], vars: &mut HashMap<String, String>) {
    for key in [
        "USE",
        "USE_EXPAND",
        "USE_EXPAND_HIDDEN",
        "CONFIG_PROTECT",
        "CONFIG_PROTECT_MASK",
    ] {
        let mut accumulated = Vec::new();

        for parent in parents {
            if let Some(values) = parent.make_defaults().get(key) {
                accumulated.extend(values.split_ascii_whitespace());
            }
        }

        if let Some(values) = vars.get(key) {
            accumulated.extend(values.split_ascii_whitespace());
        }

        let mut final_values = Vec::new();

        for var in accumulated {
            if var == "-*" {
                final_values.clear();
            } else if let Some(stripped) = var.strip_prefix("-") {
                final_values.retain(|v| *v != stripped);
            } else {
                final_values.push(var);
            }
        }

        let mut seen = HashSet::new();
        final_values.retain(|v| seen.insert(*v));

        if !final_values.is_empty() {
            vars.insert(key.to_string(), final_values.join(" "));
        }
    }
}

fn interpolate(parents: &[Profile], vars: HashMap<Key, Vec<Segment>>) -> HashMap<String, String> {
    let parent_vars = parents
        .iter()
        .flat_map(|parent| parent.make_defaults().clone().into_iter())
        .collect::<HashMap<_, _>>();

    vars.into_iter()
        .map(|(key, segments)| {
            let interpolated = segments
                .into_iter()
                .map(|segment| match segment {
                    Segment::Interpolation(i) => parent_vars.get(&i.0).cloned().unwrap_or_default(),
                    Segment::Literal(literal) => literal.0.trim().to_string(),
                })
                .collect::<Vec<_>>();

            let joined = interpolated.join("");

            (key.0, joined)
        })
        .collect::<HashMap<String, String>>()
}

fn parse(contents: &str) -> Result<HashMap<Key, Vec<Segment>>, Error> {
    Ok(LineBasedFileExpr::<Assignment>::parser()
        .separated_by_with_opt_trailing(ascii_whitespace1())
        .many()
        .parse_finished(InputIter::new(contents))
        .map_err(|e| Error::Parser(dbg!(e).rest().to_string()))?
        .into_iter()
        .filter_map(|expr| match expr {
            LineBasedFileExpr::Comment => None,
            LineBasedFileExpr::Expr(Assignment(key, value)) => Some((key, value)),
        })
        .collect())
}